Linux

How To Configure Apache Virtual Hosts on Ubuntu 16.04 LTS

The Apache Web Server is the most common and popular server used for hosting web content on the Internet. In this post, I’ll show you how to configure Apache Virtual Hosts on Ubuntu 16.04 LTS. Using this guide, you’ll be able to host multiple websites or domain names from a single server or an IP.

This will not be reflected back to the end user who will never know if other sites are being hosted on the same server. For this guide, I’ll be using two dummy domain names called “example1.com” and “example2.com“. You can replace them with your own dummy names or use other dummy values of your choice.

We’ll simply create a directory structure with a root directory and then individual directories that will hold the data of designated sites or domains.

Let’s proceed with the requirements first.

Prerequisites To Set Up Apache Virtual Host on Ubuntu 16.04

  1. Ubuntu 16.04 LTS installed on your device
  2. A user account on Ubuntu with sudo privileges
  3. Apache installed on your server

Before you set up an Apache VPS, you’d require Apache installed on your device.

If you haven’t done so already, follow the below commands to install Apache using apt-get command on Ubuntu 16.04

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

Now, let’s get started with Apache Virtual Host Configuration on Ubuntu 16.04

How to Configure Apache Virtual Host on Ubuntu 16.04

Below, you’ll find six steps outlined to configure Apache Virtual Host on Ubuntu 16.04

Let me share what all steps you’ll have to follow first:

  1. Create a Directory Structure
  2. Grant Permissions for the Directories
  3. Creating Demo or Test Pages
  4. Creating Virtual Host File
  5. Enable New Virtual Host File
  6. Test the Virtual Host Files

Now, let’s begin…

Step 1: Create a Directory Structure

Foremost, you’ll have to create a directory structure which will hold the site content that we’ll be serving to the visitors.

Our top-level directory or the root directory will be set to individual directories under the /var/www directory. In this example, I’ll be creating two directories for the domain names discussed above.

$ sudo mkdir -p /var/www/example1.com/public_html
$ sudo mkdir -p /var/www/example2.com/public_html

Further, I’ll create a folder in both sub-directories called public_html which will hold the actual site files.

Step 2: Grant Permissions For The Directories

Now that you’ve created the directories, it is time to modify the permissions for the directory to allow regular user to create or make changes to the web directories.

Use the below commands for granting permission to a user.

$ sudo chown -R $USER:$USER /var/www/example1.com/public_html
$ sudo chown -R $USER:$USER /var/www/example2.com/public_html

Here, the $USER is a variable which will automatically be replaced by the username you have currently logged in as and grant permission to the regular user to make modifications in the subdirectories.

$ sudo chmod -R 755 /var/www

The above command will modify the permissions for the general web directories and allow the users to read the content contained in the folders and subfolders. Now, you’re ready to move to the next step.

Step 3: Create Demo or Test Pages For Each Domain

As we’ll need some content to represent the demo, we’ll be creating simple demo pages for both domains in this guide. I’ll create index.html pages for each website here.

Let’s start with example1.com first. Open the editor with the following command:

$ nano /var/www/example1.com/public_html/index.html

Now, enter the following text to create a simple index file.


<html>
  <head>
    <title>Welcome to Example1.com</title>
  </head>
  <body>
    <h1>Hooray!  The example1.com Apache virtual host is working!</h1>
  </body>
</html>

Save and close this file when done. Now, using similar method you can open a file for example2.com and create its index.html or copy the file and make necessary modifications after opening it.

For copying the file, use the below command:

$cp  /var/www/example1.com/public_html/index.html 
/var/www/example2.com/public_html/index.html

Now, open the file using the following command:

$ nano /var/www/example2.com/public_html/index.html

Here, you can make the necessary changes as follows:


<html>
  <head>
    <title>Welcome to Example2.com</title>
  </head>
  <body>
    <h1>Hooray!  The example2.com Apache virtual host is working!</h1>
  </body>
</html>

Here, I’ve only changed the domain name in the index file.

Now, let’s move to our next step.

Step 4: Creating Virtual Host File

Virtual Host files are the files that hold the necessary configuration of virtual hosts and determine how Apache Web Server will respond to the domain requests. By default, Apache comes with its own virtual host file called 000-default.conf.

In this post, I’ll copy this file for creating my first virtual host file and configure it as per the need. Further, I’ll repeat the process for the second virtual host file.

Let’s start…

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

Now, open this new file example1.com.conf with root privileges. You can use shorthand sudo fo the purpose.

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

The file content will look similar to something like below:


    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Now, you’ll customize the file content and add few new directives for the purpose. The virtual host section here matches any requests on the default HTTP port 80.

First, replace the Server Admin email to the domain’s admin email where he can receive the messages.

ServerAdmin admin@example1.com

After all the necessary changes, this is how the file will look:


    ServerAdmin admin@example1.com
    ServerName example1.com
    ServerAlias www.example1.com
    DocumentRoot /var/www/example1.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

The added directives are Server Name which is the domain name and Server Alias has a matching domain most commonly preceded by www. DocumentRoot directive will also be modified with the location of Document Root for your Virtual host.

For the second Virtual host file, follow the same steps.

First, copy the example1.com.conf file and then make the necessary changes as follows.

$ sudo cp /etc/apache2/sites-available/example1.com.conf /etc/apache2/sites-available/example2.com.conf

    ServerAdmin admin@example2.com
    ServerName example2.com
    ServerAlias www.example2.com
    DocumentRoot /var/www/example2.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Now, Save and close this file and move to the next step.

Step 5: Enable New Virtual Host File

After creating the virtual host files for both domains, it is time to enable them using the a2ensite tool from Apache.

Below commands will enable the Virtual host files:

$ sudo a2ensite example1.com.conf
$ sudo a2ensite example2.com.conf

Further, disable the default configuration file of Apache.

$ sudo a2dissite 000-default.conf

Now, you just have to restart Apache for all the changes to be reflected using below command:

$ sudo systemctl restart apache2

Step 6: Test the Virtual Host Files

Now, that you’ve successfully configured two Apache virtual hosts on Ubuntu 16.04, you may test the results to see if everything works correctly. Just go to your web browser and type in the following:

http://example1.com

You’ll see a page with the heading mentioned in the index file:

Hooray! The example1.com Apache virtual host is working!

Perform the same step to test your second virtual host by entering http://example2.com

The results will be displayed on your browser:

Hooray! The example2.com Apache virtual host is working!

How To Configure Apache Virtual Hosts on Ubuntu 16.04: Wrap Up

If you’ve followed the guide correctly, Congrats. You’ve successfully added two new Apache Virtual Hosts on Ubuntu 16.04. Similarly, you can add more hosts for your other domains. As long as the server can handle the load, you should be doing just fine.

Let me know if you faced any problem during the installation or found something important missing. I’d love to hear from you. So, drop a comment below.

For more useful guides and articles, subscribe to Freaksense.

Related Articles

2 Comments

  1. Thanks for any other informative blog. Where else may just I am getting that kind of information written in such a perfect manner? I’ve a challenge that I am just now operating on, and I’ve been on the look out for such info.

  2. Usually I don’t learn article on blogs, but I would like to say that this write-up very compelled me to take a look at and do so! Your writing style has been surprised me. Thanks, very nice article.

Back to top button