Host Multiple Sites on Apache Server

Page content

How to host multiple sites on a Single Apache Server

The Apache web server is the most popular server offerign a high level of flexibility and functionality that can be further extended with the help of Apache modules. Apache’s latest available stable version is Apache 2.4.57, which was released in April 2023. We have discussed how to install Apache web server on Ubuntu 22.04 in a previous post.

In this post, we will discuss how to host multiple websites on a single Apache web server and enable the use of .htaccess file for your websites. you can host many websites on the same Apache server using separate virtual hosts files for each one of your websites. You can also use the virtual hosts file for hosting subdomains on the same server.

However, to run your websites, you will first need Apache server installed on your virtual machine. Following the installation of the Apache server, you can move on to createing virtual hosts files for your websites. A separate virtual hosts file is required for each one of the websites that contains the basic configurations for running the website including server name, alias, root folder and otehr details.

It is easy to set up a virtual hosts file for your websites. These files will be set up inside the /etc/apache2/sites-available/ directory and linked to etc/apache2/sites-enabled/ directory.

How to Set Up a Virtual Hosts file on Apache Server

Create a root folder for each of your sites

We will start by creating a root folder for our first domain. The root folder is the folder that holds all your website files. You need to create separate root folders for your websites so that you can easily recognize them. Later, you can add the root folders to the respective virtual hosts files. Create the root directory for your first domain in the following manner.

sudo mkdir /var/www/domain1

We have created a new directory that will serve as the root folder for the first domain. Now, before moving ahead, we will need to assign its ownership and make the folder writable by setting the right permissions.

sudo chown -R $USER:$USER /var/www/domain1

With the following command, you can set the right permissions to make the folder writable by the owner while only giving read and execute permissions to group and others.

sudo chmod -R 755 /var/www/domain1

After these changes, your root folder is ready to use. If you want to test, you can add an index.html file and after enablign the virtual hosts file for the respective domain, you will be able to load it in a browser. In the same manner as detailed above, you can also create a root folder for your second and more domains.

Create a Virtual Hosts File for Your Domain

The next step to enabling your website on Apache web server is to create the virtual hosst file or the configuration file for your website. you can create the file inside the /etc/apache2/sites-available/ directory. Suppose, you are planning to host two websites, domain1 and We will start by creating a virtual hosts file for each one of them.

sudo nano /etc/apache2/sites-available/

This command will open a new file for editing inside the /etc/apache2/sites-available/ directory. Now, paste the following content inside this file.

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

Save the file after you have made the necessary modifications and exit the folder. You will need to modify the ServerName and ServerAlias variables as well as the Document Root for your website. In the above file, we have updated the root folder to the folder we just created to serve as root directory for the first domain.

The access.log file records every request made to your Apache server whereas the error.log file records all the errors.

Enable Website Virtual Hosts file

Now, it is time to finally bring your website onle by enabling the virtual hosts file. The default website configurations are defined inside the 000-default.conf file and we will need to disable it before enabling the new virtual hosts file. The a2ensite and a2 dissite commands can be used to enable and disable the virtual hosts files.

sudo a2ensite domain1.conf
sudo a2dissite 000-default.conf

Before, we move further, let’s test our configuration for errors with the following command:

sudo apache2ctl configtest

If the output is Syntax Ok, we can move ahead. Now, it is time to restart the Apache server to enable the changes we made.

sudo systemctl restart apache2

Apache will now start serving your website. To test it youc an add a simple index.html file to your root folder and then try loading the website. Create an index.html file inside the root folder with the following command:

sudo nano /var/www/domain1/index.html

Paste the following content inside the file and close it.

        <title>Welcome to My Domain!</title>
        <h1>Hi, This is my first domain. Thanks for visiting.</h1>

Save the file and exit. Now, if you try loading your domain in the browser, it will load the contents of the inex.html file. You can add your own website content to the root folder. Repeat the same process for domain2 and further domains to run multiple webbsites on a single Apache web server.

Enable .htaccess file for each domain

You will need a .htaccess file for each domain, which can include security and performance related configurations as well as redirects. The .htaccess file is a very important file and needs to be edited with caution since it can break the site if anything is wrong inside it. To enable the use of .htaccess file for a website, you will need to add some configurations to the site’s virtual hosts file. for example, we just created a vhosts file for domain 1 inside the /etc/apache2/sites-available/ directory and brought it online using the a2ensite command. We will edit the vhosts file and add some more configurations.

Open the vhosts file for the concerned website:

sudo nano /etc/apache2/sites-available/

Now, under the line that includes the DocumentRoot, add the following:

<Directory /var/www/domain1>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted

As you can see, we have included the root directory in the first line. So, replace it with the root directory or the location in DocumentRoot. After having made the changes, save the file and check for configuration errors.

sudo apache2ctl configtest

Again if it gives the output Syntax OK, it means everything is fine and you can restart the Apache server to enable the changes we just made.

sudo systemctl restart apache2

Now, you have enabled the .htaccess file for your website and you can enable it for each one by making similar changes to the Vhosts (virtual hosts file) for the respective website.