So you have built your first web server, complete with a basic website, and everything is running smoothly. Ideally the website is already accessible online via a hostname and domain (if not you can read this guide to get it sorted).
But now your friend needs a new blog and you want to help out. Or you’ve just started to work on this fabulous web application and would like to migrate it to your own server to save the cost of running it on a professional web host.
If you have connected your dDNS domain to your home IP (as explained in this post), you will quickly notice that this option will only give you access to the first website. Luckily, Apache has a very helpful workaround for this type of situation.
Virtual Hosts
You can set up so-called virtual hosts, which will allow you to run multiple web sites on a single server, via name-based or IP-based virtual hosts. There are many different options to choose from and the official documentation provides a number of useful examples.
I will focus on one particular example that is relevant for the setup from my previous tutorials. But you should not find it difficult to adapt it to your needs.
What you need
To simplify matters I’m assuming that you already have the following items ready:
- Website 1
- A dedicated subdomain for Website 1, called one.website.com
- Website 2
- A dedicated subdomain for Website 2, called two.website.com
Step 1. Prepare your directories
First, ensure on the account page of your chosen DNS Service that both of your subdomains one.website.com and two.website.com link to the home IP of your web server.
On your web server, the two websites should be placed into two different folders in the default DocumentRoot directory /var/www/html for Apache. Let’s call them website1 and website2.
sudo cp -R home/pi/website1 /var/www/html/website1
Step 2. Configure the Virtual Hosts
Once that step is complete, navigate into the sites-available directory of your Apache server. This directory will show you a list of all currently available sites on your web server. Please note that not all of these sites might be live.
If you have tried the WordPress tutorial, your WordPress site should already be listed there. In that case, it might, in fact, be easiest if you move it into a separate folder in the /var/www/html directory to keep a clean environment.
cd /etc/apache2/sites-available
For each website you will now have to configure a separate config file. Let’s call them website1.conf and website2.conf. I will only go over the first, as you can just repeat the procedure for the second website… and the third and so on.
Ensure that you are still in the sites-available directory and open a new text document.
sudo nano website1.conf
Now enter the following details for the virtual host, following the specifications on the Apache website.
Using the code below, you can create virtual hosts for ports 80 (HTTP) and 443 (HTTPS), if you are following on from my previous tutorial in which we established an SSL connection for the WordPress blog using a letsencrypt certificate.
If you have not yet enabled HTTPS on your website, you only need to include the VirtualHost for port 80 in this file.
Note that you can specify different subdomains for the same website using the ServerAlias tag. For instance, if you want to be able to reach Website1 with the URLs “https://one.website.com”, “https://website.com” and “https://www.website.com”, you can specify this as below.
Don’t forget to include the certificate details for your SSL connection in the vHost for port 443. Check out this guide if you still need to obtain a browser-trusted certificate. In my case I’ve used the free and automated service provided by letsencrypt.
<VirtualHost *:80>
DocumentRoot /var/www/website1
ServerName one.website.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>
DocumentRoot /var/www/website1
ServerName one.website.com
ServerAlias website.com
ServerAlias www.website.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/one.website.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/one.website.com/privkey.pem
</VirtualHost>
Now repeat the same process with Website 2 and any other sites you might want to add.
Step 3. Enable the websites
Finally, you will have to enable the website on Apache. For this navigate into the sites-enabled directory. This directory manages which of your virtual hosts will be booted into Apache when it starts up.
cd /etc/apache2/sites-enabled
You can copy and activatey our config file with a simple command.
sudo ln -s ../sites-available/website1.conf website1.conf
Restart apache and you’re done. Both websites should now be accessible online via their specified subdomains.
sudo service apache2 restart