In this tutorial, you’ll learn how to host multiple websites on a single Apache web server. I personally found most other tutorials on this topic to be confusing, so my goal with this is to get rid of all the fluff and make it as straightforward and intuitive as possible.
On that note, let’s dive right into the tutorial.
1) Create a Document Root for Your Websites
By default, websites are served out of the
/var/www/ directory. Under here, create a subdirectory with the name of your domain name. For example, my first domain name is site1.xyz, so my directory is called site1.xzy. Within site1.xyz, create another subdirectory called html.
You can do this with the mkdir -p command like this.
mkdir -p /var/www/site1.xyz/html
Repeat this step for additional websites that you want to host on this web server. For this tutorial, my second website is site2.xyz.
mkdir -p /var/www/site2.xyz/html
2) Create Your Websites’ Content
To keep things simple, we’ll be creating a basic HTML page called index.html for each of our websites. This is just for demonstration purposes. In reality, your actual website will go here.
/var/www/site1.xyz/html, create a file called index.html with the following content.
<!DOCTYPE html> <html> <body> <h1>Welcome to site1.xyz</h1> <p>This is the first website on the Apache web server</p> </body> </html>
Repeat this step and create a similar file under
<!DOCTYPE html> <html> <body> <h1>Welcome to site2.xyz</h1> <p>This is the second website on the Apache web server</p> </body> </html>
3) Create Apache Configuration Files
Next, navigate over to the sites available folder in the apache2 directory.
In here, create a configuration file called site1.conf containing a virtual host block like this. Please note that the ServerName is the domain name of the website.
<VirtualHost *:80> ServerName site1.xyz DocumentRoot /var/www/site1.xyz/html </VirtualHost>
Repeat this step for additional websites and update references of your website’s name accordingly. For example, my second configuration file is called site2.conf and looks like this.
<VirtualHost *:80> ServerName site2.xyz DocumentRoot /var/www/site2.xyz/html </VirtualHost>
4) Enable Your Websites
Now we have to tell Apache to start hosting our multiple websites. For each configuration file you created in the previous step, use the a2ensite command to enable the websites.
a2ensite site1.conf a2ensite site2.conf
We need to reload the Apache web server in order for our changes to be recognized.
systemctl reload apache2
5) Update DNS Settings
Finally, we must update our DNS settings to associate the IP address of our web host with our domain names. Wherever you bought your domain names, add an A record with the value of the IP address for your web host.
As always, DNS changes may take some time (up to 48 hours, but typically less) to propagate. What this means is that you might have to wait a bit before your domain name resolves to your website.
But otherwise, that’s it! The only thing left to do is to test everything out.
Open a web browser and load your websites. At this point, Apache is now serving multiple websites from a single web host.
If you have any question or run into any hiccups, let me know in the comments below. Also to show your support, I’d be very appreciative if you subscribe to my YouTube channel which features loads of video tutorials on topics like this one.