Configure Apache With Multiple Vhost Files, and Enjoy!

How to configure Apache with Multiple Vhost files in Linux.

3678 views

Edited: 2020-01-26 20:06

When configuring Apache with multiple vhost files rather than just having a single file (I.e. 000-default.conf), you may run into a problem causing Apache not to start when either starting or restarting the apache2 service in Linux. The error may look similar to the below:

Job for apache2.service failed because the control process exited with error code. See "systemctl status apache2.service" and "journalctl -xe" for details.

This is a general error that might be caused by many different configuration errors, so it is important that you carefully note what you were doing that might have caused the problem. Doing a systemctl... as suggested will not be of much help, but it does tell us that the configtest failed.

To the point

In this case, we know that the problem is caused by changing from one VHOST file (000-default.conf), to multiple files. To solve the problem, we simply need to create a symbolic link in /etc/apache2/sites-enabled for each virtual host. To do this, we type the following commands in a terminal:

cd /etc/apache2/sites-enabled
ln -s /etc/apache2/sites-available/[site-name1].conf
ln -s /etc/apache2/sites-available/[site-name2].conf

Note. Replace [site-name1] with the name of your own .conf file'(s).

If you have not done so already, also remember to remove the symbolic link for the original 000-default.conf file, as it is no longer needed.

After creating the symlinks we may restart apache.

sudo service apache2 restart

Multiple VHOST files vs one big file

Sometimes you may prefer to have multiple files, lets say one for each website your server is hosting. This way it gets easier to configure, as you do not have to scroll through one huge file.

Another reason is that the apache plugin for lets encrypt does not support multiple virtual hosts in the same file. This may change, however, but it is a significant reason to keep your sites in seperate .conf files.

Each site should still have a dedicated VirtualHost block in the individual conf files, looking like this:

# ***********
# mysite.com
# ***********
<VirtualHost *:80>
    DocumentRoot "/var/www/mysite/"
    ServerName mysite.com
    ErrorLog ${APACHE_LOG_DIR}/mysite.com-error.log
    CustomLog ${APACHE_LOG_DIR}/mysite.com-access.log common
    <Directory "/var/www/mysite/">
        Options FollowSymLinks
        AllowOverride All

        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Tell us what you think:

  1. Understanding file permissions in Unix / Linux based systems, and how to make files immutable.
  2. In this article I will explain how to enable a swapfile on small instances, and why it might be useful, even if you do have enough physical memory.
  3. How to determine an optimal value for pm.max_children and related php-fpm settings for your server and web applications.
  4. Tutorial showing how to configure a VirtualBox Guest VM with HOST-only and NAT adapter, while using the WWW folder from the HOST OS.
  5. You may have wondered what the /etc/php/8.0/conf.d/ directory is for in Debian and Ubuntu, and whether it is better to edit the conf.d files than editing php.ini directly; find out in this Tutorial.

More in: Linux servers