Andrew Tarry

Boring thoughts on software development

Apache 2.4 Configuration Changes

11 Oct 2014

With the release of the Apache 2.4 server you may have noticed a few changes in the way that virtual hosts are managed. These can trip you up if you are not careful about the way you configure your site.

This article will guide you on currently hosting a PHP site on Ubuntu. Other implementations will differ but hopefully this will point you in the right direction.

Site.conf file

If you look in the /etc/apache2/sites-available directory you will notice that all of the files now end in .conf. If you open them you will find they look at little different from the previous versions that you are used too but the principle remains the same. You should have one file per virtual host and you will need to enable before using the site.

Copy the default.conf so you can use it as the basis of your own site configuration.

Open the file and you will see something like this:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

I’ve removed the comments to make it a bit clearer but as you can see there are only 4 directives by default. They behave exactly has before but in case you are no used to Apache they work as follows:

  • ServerAdmin - This should be an admin email address, if it is on your local machine then ignore it.
  • DocumentRoot - The full path to your site on the system. If you are using a framework like Symfony then this should be the web directory in your project.
  • ErrorLog and CustomLog - These allow you to customise your logging. If you are on your local machine then the default logging should meet your needs.

Directives to add

As with pervious versions of Apache you need to add additional directives to handle your virtual host.

  • ServerName - The url of your site e.g. example.com
  • ServerAlias - (Optional) A secondary name of your site e.g www.example.com
  • DirectoryIndex - (Optional) The index file for your site, if it is not called index such as the Symfony app.php page. In most frameworks this is handled by the .htaccess so it’s not really needed.

Enabling your site

As with the older versions of Apache you will now need to enable the site. Fortunately the same Apache shortcuts do work

sudo a2ensite example.conf
sudo service apache2 reload

Your site will now be running on Apache.

I’m getting 403 Forbidden errors on my site

You might be getting errors on your site as a result of some new directives that have been added Apache 2.4 by the new mod_authz_host module. This new module replaces the old Apache access control and you must explicitly tell Apache to allow public access to the site.

You can set the changes in the global Apache configuration but I always prefer to make security changes to a single virtual host. Fortunately the configuration needed should be familiar to most Apache users:

<Directory "/var/www/">
    Require all granted
</Directory> 

The directory tag is added to the VirtualHost tag in the same way as in older versions of Apache. The new Require all granted is the shorter way of doing the older:

# No longer needed 
Order allow,deny
Allow from all

Using .htaccess

If your site uses .htaccess you will need to enable it in the same way as before:

<Directory "/var/www/">
    AllowOverride all
    Require all granted
</Directory> 

This will enable the .htaccess to work in exactly the same way as it always has done in previous Apache versions.

Conclusion

Apache 2.4 includes several changes to it’s configuration and for those of us who are normally developers rather than system admins it can appear a little daunting at first but actually very little has changed. There is a new file format for the configuration and a few new directives to be aware of but overall the Apache 2.4 continues to provide an excellent, and easy to use, server for your PHP applications.