Skip to main content

Host Multiple Websites On One Apache Server On Ubuntu

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:

1
sudo mkdir /var/www/html/example.com
1
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.

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

Copy and paste below content to file.

1
2
3
4
5
6
7
8
<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.

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

Copy and paste below content to file.

1
2
3
4
5
6
7
8
<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:

1
chown -R www-data:www-data /var/www/html/example.com
1
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:

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

Add the following lines:

1
2
3
4
5
6
7
8
<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:

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

And add the following lines:

1
2
3
4
5
6
7
8
<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:

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

Restart Apache for the changes to take effect.

1
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

 

Thank you for reading!