Sunday, November 18, 2007

Modularize your apache configuration

First symptoms

The very first time I had to modify something into Apache's configuration, the documentation tells me I had to do so in a file named httpd.conf. With the time, this file became more and more unmanageable: tons of unsorted VirtualHost and Directory blocks mixed with custom changes and Linux distribution specific configuration.

First aid

Everything became cleaner when I modularized the configuration into separate files thanks to the Include directive where every virtual hosts were defined into separate files. The only change made to the httpd.conf was to append:

Include /etc/apache2/vhosts.d/*.conf

Every sites can then be configured into their own vhosts.d/$FullyQualifiedDomainName.conf file.

Example with vhosts.d/wiki.example.org:

<VirtualHost *:80>
    ServerName wiki.example.org
    DocumentRoot /var/www/wiki.example.org

    <Directory /var/www/wiki.example.org>
        AllowOverride None
    </Directory>

    ErrorLog /var/log/apache2/wiki.example.org/error_log
    CustomLog /var/log/apache2/wiki.example.org/access_log common
    php_admin_value error_log "/var/log/apache2/wiki.example.org/php_error_log"
</VirtualHost>

Health check

After this reorganization I was up to disable a site simply by renaming the related .conf file without editing it, every virtual hosts had their own file and, the most important IMO, sites configuration were not mixed with the rest! This is how Gentoo Linux organize its apache's configuration by default. My /etc/apache2 directory now looks like the image on the right.

No comments: