Facebook PixelHow to Host Multiple Websites on Ubuntu VPS? | Blog | CodeWithHarry
How to Host Multiple Websites on Ubuntu VPS?

How to Host Multiple Websites on Ubuntu VPS?

"We can configure apache2 to host multiple websites on the same VPS. To achieve this using apache, we create virtual hosts corresponding to each domain or individual site."

By CodeWithHarry

Updated: 5 April 2025

An apache2 web server provides robustness and scalability for hosting multiple websites on your Ubuntu VPS. This means you can utilize the power of your VPS hardware without any downtime on your website.

We can configure apache2 to host multiple websites on the same VPS. To achieve this using apache, we create virtual hosts corresponding to each domain or individual site. A virtual host is a way to direct traffic corresponding to an IP to a specific directory on our VPS. This way we will be able to host multiple websites on a single Ubuntu server.

How to setup a virtual host in apache2

If you haven't already installed apache2, you can do so by entering the below command in the ubuntu terminal:

sudo apt install apache2

Apache2 Installation

Assuming you have apache2 installed on your machine, all you need to do now is to follow the steps outlined in the next section to host multiple websites. For the sake of this tutorial, I will assume that we have to host 2 websites codewithharry.com and programmingwithharry.com on our ubuntu VPS. We will point the domains to the IP address of our droplets. Here is how A record is set up in GoDaddy.

A Record Setup

The process is pretty similar for other domain providers.

Let's set up our virtual hosts now.

Step 1 - Creating Directories for individual sites

Let's create individual directories to store the contents of codewithharry.com and programmingwithharry.com. Execute the commands below to create these directories inside the /var/www folder:

sudo mkdir -p /var/www/codewithharry.com/
sudo mkdir -p /var/www/programmingwithharry.com/

Directory Creation

Step 2 - Transfer the site contents

Transfer the index.html and related files to the individual site directories using Filezilla.

Step 3 - Creating the VirtualHost files

Create a new file inside the /etc/apache2/sites-available/ directory by firing the following commands below:

sudo vim /etc/apache2/sites-available/codewithharry.com.conf

VirtualHost File Creation

Once the codewithharry.com.conf file is created, paste the below contents inside it:

<VirtualHost *:80>
    ServerName codewithharry.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/codewithharry.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

VirtualHost Configuration

Repeat the same for programmingwithharry.com by executing the command below:

vim /etc/apache2/sites-available/programmingwithharry.com.conf

and paste the below contents:

<VirtualHost *:80>
    ServerName programmingwithharry.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/programmingwithharry.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 4 - Enable the VirtualHosts

In order for these virtual host files to function correctly, we need to enable them.

Enter the directory where we have created virtual hosts:

cd /etc/apache2/sites-available/

Enable VirtualHosts

Execute the following commands to enable the virtual hosts:

sudo a2ensite codewithharry.com.conf
sudo a2ensite programmingwithharry.com.conf

Finally, you will have to restart the apache server:

sudo service apache2 restart

Apache Restart

Step 5 - Test the configuration

Test the configuration of these virtual hosts by visiting your domains. You can configure as many virtual hosts as you want for your domains and this technique can help you power many websites on a single VPS.

Hope this will help you host your websites and save some money on VPS. Happy Coding!

Tags

hostmultiplewebsitesubuntuvps