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.