How to Host Multiple Websites on One Server
Once you get into the world of online hosting, the chances are high that at one point in time you will be interested in hosting multiple websites on a single server. While this might seem complex at first, as you get a bit more into it, you will see that just about anyone with a little bit of technical know-how can actually do it.
That being said, Apache is free, open-source, and one of the most widely used web servers around the world. It is not only known for the power it has, but it also has multiple components that have their own specific functions. The one that we will be looking at today is the Apache Virtual Host, which allows you to host multiple websites on a single server, which is exactly what we want to do.
If you have one big server, with a lot of resources at your disposal, think along the lines of Hard Disk or Solid State Drive Space, a lot of Random Access Memory (RAM), and CPU power, then you might have an excellent time while taking advantage of all of these resources to their fullest, and host multiple websites using the virtual hosting solutions on offer.
Keep in mind that, assuming you have the resources that are up to snuff for the job, you can essentially host an unlimited number of websites on your Apache web server.
Now, when it comes to hosting multiple websites on a single server, you have Name-Based virtual hosting, IP-based virtual hosting, and Port-based virtual hosting.
If you are interested in learning how to create your own server at home, this is the perfect guide for you, and we would recommend that you use dedicated server hosting, especially if you want to setup your own email server.
For this guide specifically, we’ll be using Ubuntu 18.04 installed on a VPS on a ServerMania server. We will also be using two valid domain names or subdomain names that are pointed to our VPS IP address, and just as example’s let’s use s1.example.com and s2.example.com in the form of subdomains. Then we’ll need two static IP addresses that are configured in our VPS. Let’s use 192.168.0.1 and 192.168.0.2.
See Also: (Live Webinar) Meet ServerMania: Transform Your Server Hosting Experience
Getting Started with The Process
First, you need to create Cloud Server and log into it. Make sure that throughout the creation process, if you are following along with this guide specifically, you need to install ubuntu 18.04 as your server OS. When it comes to the minimum specifications or requirements that you need, you’ll need to worry about having at least 2GB of ram. Next, you need to connect to your Cloud Server through SSH and log in through the usage of the credentials that are provided to you.
Now, the first time you log into your Ubuntu 18.04 server, you need to run a command that will update the base system with the latest packages that are available.
apt-get update -y
The next thing we need to do is install the Apache Web Server.
First, we need to install the Apache web server by running this command:
apt-get install apache2 -y
Once the installation itself is completed, start the Apache service with the following command:
systemctl start apache2
Name-Based Virtual Hosting
This is by far one of the most commonly used methods when it comes to hosting multiple websites on the same IP address as well as port.
Here, you will need valid domain names to host multiple websites through the usage of name-based virtual hosting.
Here, we will use s1.example.com and s2.example.com to host two websites on a single server.
Here, we will need to create a document root directory for both of the websites.
mkdir /var/www/html/s1.example.commkdir
/var/www/html/s2.example.com
Going forward, you will need to create an index.html page for both websites.
You will need to create an index.html page for s1.example.com through this command:
nano /var/www/html/s1.example.com/index.html
Then add the following lines:
<html>
<title>s1.example.com</title>
<h1>Welcome to s1.example.com Website</h1>
<p>This is my 1st website hosted with ServerMania!</p>
</html>
Moving forward, we’ll need to create an index.html for s2.example.come as the following:
nano /var/www/html/s2.example.com/index.html
Here, you will need to add the following lines:
<html>
<title>s2.example.com</title>
<h1>Welcome to s2.example.com Website</h1>
<p>This is my 2nd website hosted with ServerMania!</p>
</html>
Here, it is important that we change the ownership of s1.example.com and s2.example.com directory to www-data:
chown -R www-data:www-data /var/www/html/s1.example.comchown -R www-data:www-data /var/www/html/s2.example.com
Creating a Virtual Host Configuration File
You will need to create an Apache virtual host configuration file that can serve both websites.
Here, you will need to create an Apache virtual host configuration file for s1.example.com.
You can do this through the following command:
nano /etc/apache2/sites-available/s1.example.com.conf
Here, you will need to add lines such as:
<VirtualHost *:80>ServerAdmin admin@s1.example.comServerName s1.example.comDocumentRoot /var/www/html/s1.example.comDirectoryIndex index.htmlErrorLog ${APACHE_LOG_DIR}/s1.example.com_error.logCustomLog ${APACHE_LOG_DIR}/s1.example.com_access.log combined</VirtualHost>
Here you will have to save and close the file.
Going forward, we’ll need to create an Apache virtual host configuration file for s2.example.com:
nano /etc/apache2/sites-available/s2.example.com.conf
Here, we can add the following lines:
<VirtualHost *:80>ServerAdmin admin@s2.example.comServerName s2.example.comDocumentRoot /var/www/html/s2.example.comDirectoryIndex index.htmlErrorLog ${APACHE_LOG_DIR}/s2.example.com_error.logCustomLog ${APACHE_LOG_DIR}/s2.example.com_access.log combined</VirtualHost>
Then just close the file and enable the virtual host configuration file with the following commands:
a2ensite s1.example.com
a2ensite s2.example.com
Finally, restart the Apache webserver to apply the configuration changes:
systemctl restart apache2
Congratulations, you have successfully set up and are hosting two websites on a single server, now you need to test them.
To test them, go to your web browser and type http://s1.example.com/, and you should see the website up and running.
To test the second one, type http://s2.example.com/.
Summing it Up
Name-based virtual hosting is quite possibly one of the easiest and simple methods through which you can set up two or more websites on a single server through using Apache on Ubuntu 18.04, and hopefully, now you have a deeper understanding as to how all of this works.