How to Setup Apache Virtual Hosts on Ubuntu 18.04 and 16.04

0
125
How to Setup Apache Virtual Hosts on Ubuntu 18.04 & 16.04

Apache web server is the most popular and widely used way of serving web content on the internet. More half of the active web server is using Apache in the backend as it is extremely powerful and flexible.

Apache breaks its functionality and components into different individual units that can be customized and configured according to individuals needs. And the basic unit that describes a single site on the Apache web server is called Virtual Host.

What is Apache Virtual Host?

This term refers to a method of running multiple sites on a single server, for example, www.mysite1.com, www.mysite2.com, etc. There are two basic types of Virtual hosts in Apache:

  • IP-based Virtual Hosting
  • Name-based Virtual Hosting

With IP-based virtual hosting, you can host multiple websites or domains on the same system, but each website/domain has a different IP address. With name-based virtual hosting, you can host multiple websites/domains on the same IP address.

Virtual hosting can be useful if you want to host multiple websites and domains from a single physical server or VPS.

Each domain in the case of Virtual hosts in configured in their specific directory having all of their files and never indicating that the same server is responsible for hosting other sites. This can be used for as many Apache Virtual hosts as you want the only limitation is that your server must be able to handle the load generated by the sites.

I hope that by now you have got some basic idea about what Apache Virtual hosts are. Today we are going to look at how we can configure Apache Virtual hosts in Ubuntu 18.04 and 16.04 as the process is same for both.

Note that this is not limited to the VPS you can also set this up on your local network.

Why we need Apache Virtual Host?

It really helps developers that are working on their projects to have more control over the web server and can easily manage multiple projects at a time and this is cost effective for small projects where you have a small budget and you have limited resources in place.

Now that we have the basics covered let’s move on to the next step of actually setting it up on our server.

Prerequisites for setting up Apache Virtual Hosts:

It is recommended that you install Apache as a Non-root User one you do that we can start the configuration settings that are needed. You can set up a non-root user with sudo privileges by following these steps.

Creating User:

Once you have logged in to the server as root run the following commands.

# adduser <username>

You can replace the username with anything that you like. It will ask you a few questions like account password and name.

Set up a strong password and other information is completely optional you can simply hit enter to skip those fields.

Giving Root Privileges:

Now that we have created the user we need to give appropriate permission to that user so that they can perform administrative tasks when needed.

So now we will add the newly created user into sudo group so that it can run commands as admin using “sudo”:

# usermod -aG sudo <username>

Now you can run commands with superuser privileges.

Video Instructions:

If you don’t feel like reading the article you can watch the video in stead:

Installing Apache:

In order to set up Apache Virtual hosts, we need to first install Apache on our system it is installed by default in Ubuntu if haven’t installed already don you can do that by running the following commands:

$ sudo apt-get update
$ sudo apt-get install apache2

sudo apt-get update

Once you have installed Apache test if its working or not just open your browser and type the IP address of your server to verify.

In my case, the server IP address is 192.168.0.107

Apache default page

Great Apache server is up and running if it doesn’t start by default try running the following command.

$ sudo systemctl start apache2
OR
$ sudo service apache2 start

And then try visiting your server IP.

Now that’s out of the way we can focus on how to setup Apache Virtual Hosts on Ubuntu.

For the purpose of this guide, we are going to create two Apache Virtual Hosts mysite1.com and mysite2.net as we don’t have a valid domain and also that we are doing this on our local environment we will also cover how you can edit your hosts file so that you can test your configuration.

Creating the Directory Structure:

The first step is to create different directories for our Virtual Hosts that will hold the site data for each site.

The root directory for our Apache server from where the content is served to the users is at /var/www/ directory. We will be creating both directories for our Virtual Hosts here.

Within each of these directories, we will create a public_html folder that will hold our actual files. This gives us some flexibility in our hosting.

For our Hosts we are going to be creating directories like this:

$ sudo mkdir -p /var/www/mysite1.com/public_html
$ sudo mkdir -p /var/www/mysite2.com/public_html

Virtual hosts directory

Changing Owner Ship:

Now we have created the directories but those directories are owned by the root user. If we wanted to modify files or add a file to our web directories we will not be able to do that as we don’t have the permission to do so as you can see in the screenshot below:

Directory Ownership

So to change ownership run the following commands:

$ sudo chown -R $USER:$USER /var/www/mysite1.com/public_html
$ sudo chown -R $USER:$USER /var/www/mysite2.com/public_html

The $USER variable will take the value of the user you are currently logged in as when you press Enter. By doing this, our regular user now owns the public_htmlsubdirectories where we will be storing our content.

Now the next step is to modify the permission of the Apache root directory for security reasons we will change the root directory permission to read-only so that the apache root directory as read-only for all users except the root user:

$ sudo chmod -R 755 /var/www/

readonly

Now that we have setup proper permissions for all the directories now we can add test pages in our virtual host directory.

Demo Web Pages:

First, let’s create the sample page for our host mysite1.com to do so run the following command:

$ sudo nano /var/www/mysite1.com/public_html/index.html

Add the following HTML code:

<html>
 <head>
 <title>Welcome to Mysite1</title>
 </head>
 <body>
 <h1>Hello, This is a test page for mysite1.com website</h1>
 </body>
</html>

and save the file. Do the same for the other virtual host.

$ sudo nano /var/www/mysite2.com/public_html/index.html
<html>
 <head>
 <title>Welcome to Mysite2</title>
 </head>
 <body>
 <h1>Hello, This is a test page for mysite2.com website</h1>
 </body>
</html>

Save and close the file.

Also Read: How to Install Django on Ubuntu Linux

Creating Virtual Host configurations:

There files are the actual Apache web server files that will respond to various domain (Virtual host) requests.

As Apache comes with a default configuration file we will use them as a starting point and then edit those account to our virtual hosts lets start by copying the configuration file to our Virtual Host directory:

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mysite1.com.conf
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mysite2.com.conf

Note that you have to save the file with .conf extension otherwise it will not work.

Copy default config

Now we have to edit these configuration files according to our virtual hosts.

$ sudo nano /etc/apache2/sites-available/mysite1.com.conf

Edit/modify ServerAdmin, ServerName, ServerAlias and DocumentRoot values match to the virtual host.

<VirtualHost *:80>
 # The ServerName directive sets the request scheme, hostname and port that
 # the server uses to identify itself. This is used when creating
 # redirection URLs. In the context of virtual hosts, the ServerName
 # specifies what hostname must appear in the request's Host: header to
 # match this virtual host. For the default virtual host (this file) this
 # value is not decisive as it is used as a last resort host regardless.
 # However, you must set it for any further virtual host explicitly.
 #ServerName www.example.com

   ServerAdmin [email protected]
   ServerName mysite1.com
   ServerAlias www.mysite1.com
   DocumentRoot /var/www/mysite1.com/public_html

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
 # error, crit, alert, emerg.
 # It is also possible to configure the loglevel for particular
 # modules, e.g.
 #LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
 # enabled or disabled at a global level, it is possible to
 # include a line for only one particular virtual host. For example the
 # following line enables the CGI configuration for this host only
 # after it has been globally disabled with "a2disconf".
 #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

Follow the same steps for the Configuration of the other Virtual Host:

$ sudo nano /etc/apache2/sites-available/mysite2.com.conf
<VirtualHost *:80>
 # The ServerName directive sets the request scheme, hostname and port that
 # the server uses to identify itself. This is used when creating
 # redirection URLs. In the context of virtual hosts, the ServerName
 # specifies what hostname must appear in the request's Host: header to
 # match this virtual host. For the default virtual host (this file) this
 # value is not decisive as it is used as a last resort host regardless.
 # However, you must set it for any further virtual host explicitly.
 #ServerName www.example.com

   ServerAdmin [email protected]  
   ServerName mysite2.com  
   ServerAlias www.mysite2.com  
   DocumentRoot /var/www/mysite2.com/public_html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
 # error, crit, alert, emerg.
 # It is also possible to configure the loglevel for particular
 # modules, e.g.
 #LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
 # enabled or disabled at a global level, it is possible to
 # include a line for only one particular virtual host. For example the
 # following line enables the CGI configuration for this host only
 # after it has been globally disabled with "a2disconf".
 #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

 

Now that we have saved both configurations we have to enable both Virtual hosts configuration files.

Enable Virtual Host Configuration:

Now that we have created our virtual host files, we must enable them. Apache includes some tools that allow us to do this.

We have to use a2ensite tool to enable each of our site like this:

$ sudo a2ensite mysite1.com.conf
$ sudo a2ensite mysite2.com.conf

Next, we need to disable the Default site configuration:

$ sudo a2dissite 000-default.conf

After that, we just need to restart the Apache server.

$ sudo systemctl restart apache2
OR 
$ sudo service apache2 restart

That’s it. We have successfully configured virtual hosts in Apache. Let us go ahead and check whether they are working or not.

Testing Virtual Hosts:

Now let’s test our virtual hosts we need to edit our /etc/hosts file

$ sudo nano /etc/hosts

Add the following Lines:

192.168.0.107   mysite1.com
192.168.0.107   mysite2.com

Now save and exit.

Note: This is a local environment so in order to access the virtual hosts on other systems on your local network we will have to add the above line in the hosts file on those systems too, for example for Windows clients we will have to add these lines in C:\Windows\System32\drivers\etc\hosts

And if the Client systems are running Linux just add the above line in /etc/hosts file.

Now if your Access your Virtual Hosts using CURL you can see that both hosts are working correctly.

access via curl

On the Client System:

mysite1.com mysite2.com

As you can see the Apache Virtual Hosts are now working thanks for reading the article if you have any question do comment down below like and share to help others.