DEV Community

Cover image for How to host several websites on a single Linux server
.
.

Posted on

How to host several websites on a single Linux server

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.

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
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 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

4.We will now edit the newly-copied file:

sudo nano new-website.com.conf

Enter fullscreen mode Exit fullscreen mode

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

Virtual Host

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
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. 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

Enter fullscreen mode Exit fullscreen mode

7.Restart Apache server:

sudo service apache2 restart

Enter fullscreen mode Exit fullscreen mode

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.
Simply go 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.

Top comments (0)