Another easy Apache tweak

I ran my site through Google Page Speed on Tuesday, and scored a surprising 88 out of 100–higher than I expected. Getting above 90 is going to take some optimizations on files that WordPress updates may change, so I’m hesitant to do that, but one thing it told me to do was to cache more aggressively. That’s pretty easy, as it turns out, and I could definitely feel a difference afterward.

Here’s the trick.

First, install the Apache module “expires” with this command (become root first, either using sudo or su):

a2enmod expires

Next, edit your .htaccess file in your root directory (/var/www/.htaccess in my case; yours may vary) and add these lines:

ExpiresActive on
ExpiresByType application/javascript "access plus 1 weeks"
ExpiresByType image/jpg "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 week"
ExpiresByType image/gif "access plus 1 week"
ExpiresByType image/png "access plus 1 week"
ExpiresByType text/css "access plus 1 weeks"

Then restart Apache with this command:

/etc/init.d/apache2 restart

That got me another point in Google’s grading scale. But I don’t care about that as much as I care about the increased speed. I thought caching happened automatically. Evidently not. I wish I’d discovered this years ago. I even see the difference accessing the server locally over the same gigabit network.

I could shave about 1.5 KB off my typical page load by minifying some CSS and Javascript and optimizing a PNG image, but caching gets me a much bigger bang for my buck; now regular visitors will only download those files once a week anyway. The best minifier runs under Java, which I’m not installing on my web server for any reason.

I was able to shave 200 bytes off the PNG image that Google wanted me to optimize by using a utility called pngcrush, installable on Debian with this line:

apt-get install pngcrush

I then did this:

pngcrush -brute wordpress_icon.png wordpress_icon2.png
mv wordpress_icon.png wordpress_icon3.png
mv wordpress_icon2.png wordpress_icon.png

I diddled with the pngcrush options a bit but nothing got me anything better than a plain -brute option did. Even after doing that, Google thought I could do better, but I don’t see any point in fretting any more over what’s now a 2.1KB file.

If you found this post informative or helpful, please share it!
%d bloggers like this: