The likelihood that you will become interested in hosting several websites on a single server once you enter the realm of online hosting is considerable. Although this could appear difficult at first, as you learn more about it, you will realize that pretty much anyone with a bit of technological know-how can truly do it.
Having said that, Apache is one of the most extensively used web servers globally and is free and open-source. It is renowned for more than just its strength; in addition, it has numerous parts, each of which serves a distinct purpose. We shall examine the Apache Virtual Host, which enables you to host multiple websites, in this article.
Suppose you have a large server with plenty of RAM, CPU power, hard drive, or solid-state drive space at your disposal. In that case, you might have a great time exploiting all of these resources to their greatest potential and hosting several websites using the virtual hosting services available.
Remember that you can theoretically host an infinite number of websites on your Apache web server, provided you have the resources necessary for the job.
Even if you get reliable shared hosting with good storage and security, you can do this. So, come, let us have a look at how you can host multiple sites on the same server.
Start the process
Now, you have Name-Based Virtual Hosting, IP-Based Virtual Hosting, and Port-Based Virtual Hosting when it comes to hosting several websites on a single server.
So first, you need to create a cloud server and log in. If you follow this guide clearly, make sure to install Ubuntu 18.04 as your server’s operating system during the build process. You should know about having at least 2GB of RAM when it comes to the minimum specifications or criteria you need. The next step is to create an SSH connection to your cloud server and log in using the credentials provided. Now you need to run a command to update the system settings with the latest packages available before installing them on your Ubuntu server.
Apt-get update- y
The Apache Web Server installation is the next thing we must perform.
Installing the Apache web server first requires performing the following command:
Apt- get install Apache 2 update- y
Once the installation is complete, start the Apache server with the following command:
systemctl start apache 2
Using Names for Virtual Hosting
When it comes to hosting numerous websites on the same IP address and port, this is by far one of the most widely utilized techniques.
In this case, you will require active domain names in order to use name-based virtual hosting to host several websites. Here, we’ll host two websites on one server using s1.example.com and s2.example.com.
We must now construct a document root directory for both of the websites.
mkdir/var/www/html/s1.example.commkdir
/var/www/html/s2.example.com
The next step is to make an index.html page for each website. You must use the following command to construct an index.html page for s1.example.com:
nano/var/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 BluehostIndia</p>
</html>
Moving forward, we will have to create an index.html for s2.example in the following way:
nano/var/www/html/s2.example.com/index.html
In this case, it is important that we change the ownership of the s1.example.com and s2.example.com directory to www-data:
<html>
<title>s2.example.com</title>
<h1>Welcome to s2.example.com Website</h1>
<p>This is my 2nd website hosted with BluehostIndia</p>
</html>
Here is it essential 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
It is important to create a virtual host configuration file
An apache virtual host configuration file that can serve both websites must be created. For s1.example.com, you must now construct an Apache virtual host configuration file.
Do this with the following command:
nano/etc/apache2/sites-available/s1/example.com.conf
Here you may possibly add lines like:
<VirtualHost *:80>ServerAdmin [email protected] s2.example.comDocumentRoot/var/www/html/s2.example.comDirectoryIndex.htmlErrorLog${APACHE_LOG_DIR}/s2.example.com_error.logCustomLog${APACHE_LOG_DIR}/s2.example.com_access.logcombined</VirtualHost>
Once this is done, close this 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
Congrats! Now you have set up two websites on a single server!
To test these sites, go to the web browser and type http://s1.example.com/ , for the first website and http://s2.example.com/.
Conclusion
Hopefully, you now have a better grasp of how name-based virtual hosting works, which is one of the easiest and most straightforward ways to host two or more websites on a single server with Apache on Ubuntu 18.04.