How to host several websites on a single Linux server
Tags: programming, webdev, linux, devops
Published: 2022-02-08 17:59:40

Most of the time, we deploy web applications on popular hosting sites like Hostinger, whogohost, etc., who give us spaces to host our application files (known as shared hosting). Depending on our subscription model however, we may find that we are limited in what we can actually do by way of SSH access, number of cronjobs, database limits and so on. 

We can go the way of getting a dedicated cloud hosting from AWS, Google or Azure, where we can provision virtual Linux machines 
 on which we can deploy our applications while maintaining full control. I explained how to do that with [Azure here](https://dev.to/dreysongz/how-to-deploy-a-laravel-app-on-an-azure-ubuntu-server-1fl).

Of course, it is normal that we might have several websites/applications with different domain names all consolidated on the same server instance, this means we can self-host as many websites as we have on the same server as long as space allows for it. This is possible by the Virtual Hosting feature of Apache.

Steps: 

1.SSH to the Linux server using Putty

![putty](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ikb0ivvzk68wrl3v7p74.png)
2.We need to create a separate directory in the web directory that Apache uses to serve web pages (/var/www/html). Create a new folder thus: /var/www/html/new-website. This is the folder all requests to our website will be routed to. You can upload all your website files in this directory.

3.We want to copy and modify the default web config file in the apache config directory. Copy the following code:

```
sudo cd /etc/apache2/sites-available 
```
 Copy the default web config file (000-default.conf) and paste with a name for our new website we want to add.
```
sudo cp 000-default.conf new-website.com.conf
```

4.We will now edit the newly-copied file:
```
sudo nano new-website.com.conf

```
5.Once the file opens, you should see something like this, with some lines commented out:

![Virtual Host](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dax6trrp8rfk6is2jvft.png)

Edit the file by coming down to the **ServerName**, uncomment it by removing the # character, remove www.example.com and type the address of our website, new-website.com.
You can also set alias for the website to capture different wildcards by adding **ServerAlias** wwww.my-website.com immediately after the ServerName directive, you can add as many as you want.
Come down to **DocumentRoot**, remove /var/www/html, replace with the directory that you have created for the new site under (/var/www/html), something like /var/www/html/new-website.. this is the directory that visitors will be re-directed to whenever they type in the web address. 
Your changes should look like this: 

![final](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/39dijox3za2j4t9tyl5a.png). Save and exit.

6.After doing this we need to tell Apache to activate this particular setting, only then the configuration can be complete:
```
sudo a2ensite new-website.com.conf

```
7.Restart Apache server:
```
sudo service apache2 restart

```

8.We have taken care of the Server end, we need to point the DNS records for our domain to the Linux server IP address in case we have not done so before.
Navigate to the DNS settings of your domain, create a new A record with the value of the server's IP address. 

Wait for 1-2 hours to propagate. All done. Repeat the entire process for as many sites we wish to configure on the server.