5 Easy Steps to Host Multiple Websites on an Apache Web Server

by

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

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.

First under /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 /var/www/site2.xyz/html.

<!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.

cd /etc/apache2/sites-available

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.

DNS A record settings for two domain names pointing to the same web host IP addressAs 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.


About The Author

With a strong software engineering background, Tony is determined to help as many people as possible start their online busines. Discover why Tony quit his hedge fund job to pursue this mission. You can send Tony a message here.

2 thoughts on “5 Easy Steps to Host Multiple Websites on an Apache Web Server”

  1. Bro please I have followed your tutorial even from youtube. This example yo gave here tried to explain what I want but can yo channel it to wordpress site on linode host like you did for one click app….but now for multiple wordpress on same linode like example1.com example2.com example3.com
    Currently my sites are offline because I used one click app and along the way am stuck that I dont know what to do and one code messed my site up.
    Can you really reply me? please

    Reply

Leave a Comment