Configure Apache With Multiple Vhost Files, and Enjoy!
How to configure Apache with Multiple Vhost files in Linux.
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: