skip to content

Re-naming vhost files to *.conf for Apache 2.4

 Tweet Share0 Tweets

In preparation for an upgrade from Apache 2.2. to 2.4 we're re-naming our vhost configuration files from to so that they're still recognised after the upgrade.

Otherwise, when we restart Apache after the upgrade, all our vhosts will disappear and we'll be in a mad panic getting them back.

The following examples are specific to a "Jessie" upgrade on Debian, but should be applicable or adaptable to other platforms.

What needs to be done

Essentially we're just appending .conf to the file names in /etc/apache2/sites-available, as well as to the corresponding symlinks in /etc/apache2/sites-enabled, and then reloading the Apache configuration to make sure everything still works.

For example, to update a single vhost we would run the following commands:

a2dissite mv /etc/apache2/sites-available/ /etc/apache2/sites-available/ a2ensite

And then a reload:

apache2ctl configtest && apache2ctl graceful

Neither a2dissite nor a2ensite will affect a live web server until you initiate a reload or restart.

If you use a different naming convention or directory structure, you may need to customise these instructions as well as the script below.

If you're not running as root all administrator commands will need to be prefixed with sudo.

First make a backup

Before making any changes, you should back up your Apache configuration:

tar cvzf /var/tmp/apache2.tgz /etc/apache2/

This will create a backup file at /var/tmp/apache2.tgz which you can use to restore settings in case of anything untoward on restarting Apache.

Automated file re-naming

On some of our web servers we have a lot of active vhosts so it makes sense to automate the process of renaming files. We've come up with a simple bash script to do this:

#!/bin/bash # script filename: # rename apache vhost conf files to *.conf APACHE=/etc/apache2 AVAILABLE=$APACHE/sites-available ENABLED=$APACHE/sites-enabled for file in $ENABLED/* do host=$(basename $file) [[ $host =~ \.conf$ ]] || { (mv $AVAILABLE/$host $AVAILABLE/$host.conf) && (a2ensite $host.conf) && (a2dissite $host) } done

The script loops through all the vhost files in /etc/apache2/sites-enabled (vhosts that are currently active), skips any that already have the .conf suffix, and applies the commands that we previously ran manually.

You may see errors relating to the "000-default" symlink as it points back to "../sites-available/default" (a different file name), as well as the usual messages from the a2ensite and a2dissite command calls which can be ignored.

To reactivate the 'default' vhost if needed, use:

a2ensite default

When you do upgrade Apache it should take care of the 'default' vhost configuration files for you - re-naming them as well as updating their authorization directives to use the new Require syntax.

Finally, you'll need to reload or restart Apache for the changes to take affect:

apache2ctl configtest && apache2ctl graceful

If the configuration file updates were successful then nothing will have changed, just that you're now ready (more or less) to upgrade to Apache 2.4.

If you're already upgraded

If you've upgraded already to Apache 2.4 and seen all your vhosts vanish you will need something slightly different:

#!/bin/bash # script filename: # rename apache vhost conf files to *.conf AVAILABLE=/etc/apache2/sites-available for file in $AVAILABLE/* do host=$(basename $file) [[ $host =~ \.conf$ ]] || { (mv $AVAILABLE/$host $AVAILABLE/$host.conf) && (a2ensite $host.conf) } done

Here we're assuming that there are no active vhosts, so we just rename the files in /etc/apache2/sites-available and activate them one by one.


< System

Send a message to The Art of Web:

used only for us to reply, and to display your gravatar.

<- copy the digits from the image into this box

press <Esc> or click outside this box to close

Post your comment or question