Ramblings from MostlyChris

Tech stuff and a bit more

Browsing Posts in Apache

mod_deflate allows apache to compress files and deliver them to browsers that can use compressed files. This saves on bandwidth and renders page loads faster. It's a fairly simple process to do this. You need to have write access to the apache config files and need to be able to create a new conf file. I am doing this on a Redhat server with Apache 2.2.3.

The first step is to make sure that mod_deflate.so is loaded in apache. You should see a line in httpd.conf similar to the following:

LoadModule deflate_module modules/mod_deflate.so

If that line does not exist, then you'll need to get the mod_deflate object file. It is generally included with apache so all you need to do is locate it on your server and load it using the correct path for where it is.

The next step is to create a config file to house the mod_deflate configuration. On my server the conf.d directory is where all such files are stored. Apache then loads files in that directory if they end in '.conf'. I created a file called mod_deflate.conf.

In my configuration, I chose to compress all files except images, files such as gzip, tar, etc. that are already compressed and PDF files (they are already fairly compressed). The configuration for that looks like this:

SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ \
    no-gzip dont-vary
SetEnvIfNoCase Request_URI \
    \.(?:exe|t?gz|zip|bz2|sit|rar)$ \
    no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary

I then skip browsers that have problems with deflate.

BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

Finally, I set some logging to test with.

DeflateFilterNote Input input_info
DeflateFilterNote Output output_info
DeflateFilterNote Ratio ratio_info
LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
CustomLog /var/log/httpd/deflate_log deflate

Make sure you restart apache after this so the new configuration file is used. Check your deflate_log to see that it is working.

Installing APC

No comments

Follow these steps to install APC on a RHEL 5 server.

1. Install php-devel for phpize (if not installed).

yum install php-devel

2. Install the apache devel pkgs for apxs (if needed).

yum install httpd-devel

3. Make /tmp executable.

mount -o remount,exec /tmp

4. Set the memory limit in /usr/share/pear/pearcmd.php to 16M.

vi /usr/share/pear/pearcmd.php
@ini_set('memory_limit', '16M');

5. Install apc.

pecl install apc

6. Add a /etc/php.d/apc.ini with the following line:

extension=apc.so

7. Restart apache (check configuration first for errors).

service httpd configtest (watch for errors)
service httpd restart

Remove the ini_set and set /tmp back to noexec.

I recently ran across a request to place all vhost configurations in separate files. Here are the steps I followed to make this happen. Step 4 is only necessary if you have existing virtual hosts in the main or a single apache configuration file.

1. I created the virtual_sites directory under /etc/httpd/conf.d/

2. I set the "File or directory to add virtual servers to" option to /etc/httpd/conf.d/virtual_sites in Webmin->Servers->Apache Webserver->Module Config.

3. I added the following line to the apache configuration file (/etc/httpd/conf/httpd.conf)

Include conf.d/virtual_sites/*.conf

4. I created a .conf file for each of the virtual domains in the virtual_sites directory and I copied the virtual host configuration for each domain to the newly created files.

5. I tested the virtual domains in order to make sure they are working.

6. I created and deleted a test domain to make sure that Webmin was able to create and delete the new files–Success.

7. I did a final restart of apache to make sure it restarted without error.

This is a super simple site redirect. This, of course, can get much more complicated than what is shown here.

Place this in the apache configuration file (non-Plesk boxen) or in a .htaccess file.

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

—–
If you want to redirect a site using the httpd.conf file inside a VirtualHost container you can do it this way (requires mod_alias):

<VirtualHost *:80>
ServerName original.domain.name
Redirect permanent / http://redirected.site.com/
</VirtualHost>

—–
Here is a simple redirect to take http://domain.com to http://www.domain.com.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule (.*) http://www.domain.com$1 [R=301,L]

This needs to be placed in each VirtualHost container and modified for the domain you are serving in that VirtualHost.

——
You can add this rule to allow browsing to a page without using the page extension. Place this in a .htaccess file in the directory.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

Replace the .html extension with whatever extension you are trying to remove from the filename.

Use this little one liner to grab the number of hits from the apache access logs.

DATE=`date +%d.%b.%Y`':';for i in `lsof -p $(ps faxwwwu|awk '$1 ~ /^root$/ && $11 ~ /httpd$/ {print $2|"head -1"}' )| awk '$9 ~ /access_log$/ {print $9| "sort -u"}'`; do echo "——-"$i"——"$DATE;awk '$4 ~ /^.'$DATE'/ {hit[substr($4,2,2)"\t"substr($4,14,2)"."substr($4,17,1)]++;ip[$1]++} END { for (i in hit) { print hit[i]"\t"i|"sort -k 3 -g"}; print ""}' $i;done

You can also grab a sorted list of IP addresses accessing sites by doing using this one liner.

DATE=`date +%d.%b.%Y`;for i in `lsof -p $(ps faxwwwu|awk '$1 ~ /^root$/ && $11 ~ /httpd$/ {print $2|"head -1"}' )| awk '$9 ~ /access_log$/ {print $9| "sort -u"}'`; do echo "——-"$i"——"$DATE;awk '$4 ~ /^.'$DATE'/ {hit[substr($4,2,2)"\t"substr($4,14,2)"."substr($4,17,1)]++;ip[$1]++} END { for (i in hit) { print hit[i]"\t"i|"sort -k 3 -n"}; print "";  for (i in ip) {print ip[i]"\t"i|"sort -n"}}' $i;done