Generic Apache configuration snippets

Today I'm changing server for this blog for the (maybe) fifth time. I'm doing some server reorganization and I decided to move this site to a small dedicated VPS at VPS.net. So, if you are reading this message, welcome to the new server!

The most annoying activity, each time I have to configure a clean Linux installation, is to deal with all the small tweak and improvements I applied since the server was originally created. I have more than one server and I like to reuse successful scripts in order to save time and leverage productivity. I often end up improving one script at time and I need the changes to propagate to all the other existing servers.

For this reason, I started to keep track of all those configurations in several different Git repositories. Each time a script is updated, the changes are pushed to the main repository. All the servers are configured to periodically pull changes from the master repository to keep local versions up-to-date.

Because cloning private repositories is almost annoying as copying content by hand, I decided to convert some of my Git repositories into a Gist. Here's my Generic Apache configuration snippet repository. It contains some generic configuration snippets for various topics, from Rails to security.

How to use the snippet repository

All the Apache configurations in the repository are self-contained. It means, each configuration is created to be independent from the hostname or other configurations, to make it more flexible and reusable.

To apply a configuration, just Include the file in your Apache or Vhost configuration.

# /etc/apache2/apache.conf
# Enable compression for all configured vhosts
Include /path/to/compression-gzip.conf

You can even enable the configuration in a single vhost.

# /etc/apache2/sites-available/simonecarletti_production.conf
# Enable Rails cache for this vhost
<VirtualHost *:80>
  Include /path/to/compression-gzip.conf
</VirtualHost>

Depending on your needs, you might want to clone the Git repository on your local machine and selectively include the configuration snippets or just grab the configuration you need for your single virtual host.

Snippets

Apache Gzip Compression

The following snippet enables the Gzip compression for everything except compressed images. Gzipping your content usually reduces the response size by about 70%. Do you want to learn more? Watch how Gzip compression works and why it's so important.

Rails Cache

The following two snippets are meant to be used in combination with Rails page caching. They instruct Apache to look for a cached resource in the /cache folder and serve the content from the directory if the file exists.

Redirect Not-WWW to WWW

This is a really generic Not-WWW to WWW configuration script. Unlike most existing scripts, it doesn't force you to manually inject the server name. Just include the snippets and you're ready. If you are looking for a less exotic configuration, have a look at the How to 301 redirect Not-WWW to WWW version question at Search Marketing Arena.

Security Rules

In the attempt to reduce the amount of spam comments sent to my blogs, I extracted some recurrent patterns from my server logs and created some super-simple rules to kick out automated script. Basically, this is the same idea behind mod_security, with the (huge) difference my set of rules it much more simple but much more lightweight.

All configuration snippets are available free of charge and without any warranty. Use them at your own risk.