Adding a Virtual Host to Apache

Preamble

The beauty of hosting your own web server and having root access to your machine is the ability to run whatever you want on it. One great example of this is running virtual hosts in apache, which allows you to run multiple websites on the same server. What you can run as a virtual host is fairly unlimited so long as the clients can resolve the address to your web server, be it URL, IP address or even special use domain names if you so desire (though the last one I do not recommend).

When setting up Virtual Hosts it is important to realize that as websites will run on a single server, If one website goes down, its very likely that the others will go with it. Additionally, when a server is not configured correctly or securely when a client connects to the server they may not see what you intend or may see the default apache page.

Place your website’s files in the folder /var/www/root/infinityflame.co.uk replacing my domain name with yours and setting the correct permission on the directory.

Method

There are multiple ways of configuring apache virtual hosts, such as IP-Based Virtual Hosts, which allow you to apply different rules to connections based on the originating or outgoing interface. And the most popular and most used method of Name-Based Virtual Hosts.

Adding a virtual host to apache is very simple once you get used to it. In the folder /etc/apache2 , there are the following files, or there should be; sites-enabled, sites-available, mods-enabled, mods-available, conf-enabled and conf-available. The folder for adding virtual hosts in is sites-available. You can also add .conf files to sites-enabled, but in doing so using a2ensite and a2dissite (commands used to enable and disable virtual hosts) no longer work as intended.

Navigate to the folder /etc/apache2/sites-available then make a file called the name of your first website, such as infinityflame.co.uk.conf or dashboard.infinityflame.co.uk.conf as an example, you can technically call these files whatever you desire, but as you add more websites this folder can become cluttered and hard to navigate if not named efficiently. You can make these files using the touch command or nano. Once you have created the file, open it with your desired text editor and enter the following, replacing the necessary options with the ones you need.

<VirtualHost *:80>

DocumentRoot /var/www/root/infinityflame.co.uk
<Directory "/var/www/root/infinityflame.co.uk">
Options FollowSymLinks
AllowOverride All
allow from all
Options +Indexes
</Directory>
ServerName infinityflame.co.uk
ServerAdmin [email protected]
ServerAlias www.infinityflame.co.uk

CustomLog /var/log/apache2/infinityflame.co.uk-access.log combined
ErrorLog /var/log/apache2/infinityflame.co.uk-error.log

</VirtualHost>

Once you have replaced the options with the ones you used, such as the DocumentRoot, ServerName, ServerAdmin, ServerAlias, CustomLog and ErrorLog. You can save it and start to enable it by doing a2ensite infinityflame.co.uk.conf and then service apache2 reload to reload apache, you should then be able to navigate to your website in a browser, so long as the registered address is pointing to the correct server.

Adding More Virtual Hosts

Once you have added one, its time to add another, then enable that one using the same command, you should now be able to navigate to the appropriate website from the browser automatically. If the server doesn’t have an appropriate virtual host server name or a user connects via an IP Address that hasn’t been defined they will connect to the first one defined in the output of apachectl -S . Additionally, if a user connects to a port that isn’t 80 they will not be able to connect to the Apache server as the server won’t be listening on the port.

Note that there should now be symlinks to the websites you added in the sites-enabled folder that go back to the files we made earlier.

Adding SSL

As websites move to secure areas it is important to have SSL on most modern websites. Fear not, although this requires some extra configuration it is nothing that we can’t handle. Open your website’s configuration file again and add a new virtual host below,

<VirtualHost *:443>

SSLEngine on
SSLProtocol all -SSLv2 -SSLv3
SSLHonorCipherOrder on
SSLCipherSuite "ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS"
SSLCertificateFile /etc/letsencrypt/live/infinityflame.co.uk/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/infinityflame.co.uk/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/infinityflame.co.uk/fullchain.pem
DocumentRoot /var/www/root/infinityflame.co.uk
<Directory "/var/www/root/infinityflame.co.uk">
Options FollowSymLinks
AllowOverride All
allow from all
Options +Indexes
</Directory>
ServerName infinityflame.co.uk
ServerAdmin [email protected]
ServerAlias www.infinityflame.co.uk

CustomLog /var/log/apache2/infinityflame.co.uk-access.log combined
ErrorLog /var/log/apache2/infinityflame.co.uk-error.log

</VirtualHost>

Replacing the configuration options relevant to the ones you use, as I use LetsEncrypt’s certbot the configuration is made easier by having a standard location for all of the Certificate and Key files that you can copy if you do too, replacing the domain with your own.

Removing or Disabling a Website

To remove a website or disable it use a2dissite infinityflame.co.uk.conf and then reload apache using service apache2 reload . This removes the symlink from sites-enabled so that the website is not loaded on startup.