Host Multiple Websites On One Apache Server On Ubuntu

In this post, I will guide you on Host Multiple Websites On One Apache Server On Ubuntu. Use Apache Virtual Host to configure different domains on your Apache Server.

If you have a server with large hardware resources, then you can host multiple websites using virtual hosting to make full use of it. Apache Virtual Host is one of the useful functions for you to host multiple websites on a single server.

We choose to use VPS provided by Vultr to bring speed and stability to the website. If you have not used any VPS service, please click here to sign up for an account and get $100 for free.

Steps To Configure Multiple Websites On A Single Apache Server

1. Install Apache Web Server

If you don’t have an Apache server installed refer to post: How To Install Apache On Ubuntu 20.04.

2. Create the Directory Structure

First, create a document root directory for both websites by commands:

sudo mkdir /var/www/html/example.com
sudo mkdir /var/www/html/example1.com

Next, create an index.html page to be track the results.

Create an index.html page for site example.com.

sudo nano /var/www/html/example.com/index.html

Copy and paste below content to file.

<html>
<head>
    <title>example.com</title>
</head>
<body>
    <h1>Welcome to example.com website</h1>
</body>
</html>

Next, create an index.html page for site example1.com.

sudo nano /var/www/html/example1.com/index.html

Copy and paste below content to file.

<html>
<head>
    <title>example1.com</title>
</head>
<body>
    <h1>Welcome to example1.com website</h1>
</body>
</html>

3. Grant Permissions

Grant permissions of example.com and example1.com directory by commands:

chown -R www-data:www-data /var/www/html/example.com
chown -R www-data:www-data /var/www/html/example1.com

4. Create a Virtual Host Configuration File

Create an Apache virtual host configuration file for example.com:

sudo nano /etc/apache2/sites-available/example.com.conf

Add the following lines:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html/example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Create an Apache virtual host configuration file for example1.com:

sudo nano /etc/apache2/sites-available/example1.com.conf

And add the following lines:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName example1.com
    ServerAlias www.example1.com
    DocumentRoot /var/www/html/example1.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Then, enable the virtual host configuration file with the following commands:

sudo a2ensite example.com.conf
sudo a2ensite example1.com.conf

Restart Apache for the changes to take effect.

sudo systemctl restart apache2

5. Test your Results

Visit the website with the path http://example.com you will see results like this:

Result of example

And visit the website with the path http://example1.com you will see results like this:

Result of example1

This is the end of the post.

Follow us for the more helpful posts!

We hope this is a useful post for you.

You can read more useful posts like How To Add Swap Space On Ubuntu.

Thank you for reading!

3.4 5 votes
Article Rating

Aaron LX

Aaron is a passionate writer, crazy about shopping, eCommerce and trends. Besides his outstanding research skills and a positive mind, Aaron eagerly shares his experience with the readers.

Leave a Reply or put your Question here

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Tester
Tester
November 22, 2022 6:08 pm

Didn’t work.

TK
TK
March 21, 2023 10:14 pm

This only works for http domain requests. It won’t work for https requests for the same domain until you configure for https.
I think this is what the other commenter (Tester) has experienced.

2
0
Would love your thoughts, please comment.x
()
x