Skip to content

How To Set Up Apache Virtual Hosts on Ububntu 16.04

Posted in Hosting tips

Apache is one of the most popular web servers in the world.

Components and functions of Apache are divided into separate elements that can be individually installed and configured. One of the basic elements responsible for individual site or domain settings is a virtual host.

Virtual hosts allow an administrator to use a single server to host multiple sites (within the same interface or IP address). Each domain configured in this way will direct the visitor to the appropriate site. The number of virtual hosts per server is limited solely by the resources of that server.

This guide will help you set up virtual hosts on a dedicated Ubuntu 16.04 server to serve different content depending on the requested domain.

Requirements

  • Pre-configured server Ubuntu 16.04.
  • Non-root user with extended privileges.
  • Apache web server installed:

sudo apt-get update
sudo apt-get install apache2

Note: conditional domains are used In this guide example.com and test.com; don’t forget to replace them with your domain name.

If you do not have a domain name, the end of the guide shows you how to test a setting with dummy values.

1: Сreate a directory structure

First, you need to create a directory structure in which to store site data.

By default, the document root directory (the directory that contains content for Apache) is in /var/www and you must create a separate directory for each virtual host. In each directory will be placed in the public_html directory, which will contain the files you need. Thanks to this hosting becomes more flexible.

You can create the required directories by using the following commands:

sudo mkdir -p /var/www/example.com/public_html
sudo mkdir -p /var/www/test.com/public_html

Note: be sure to specify your domain names in the commands.

2:Grant Permissions

The directory structure is ready, but they all belong to the root user. To enable a non-root user to make changes to web catalog files, you must change the permissions on those files by using the following command:

sudo chown -R $USER:$USER /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/test.com/public_html

The $ user variable takes the name of the current user. Then the subdirectories of the public_html directory that contains the content of the sites will be owned by the current user.

We also need to change privileges and access to the web directory and all its contents are read, otherwise the pages will not display correctly.

sudo chmod -R 755 /var/www

Now all rights are set correctly; you can start creating content in special directories.

3: Create demo pages for virtual hosts

Create test content; simple pages will suffice for trial host configuration. Just create the index page.the html for each site.

Let’s start with example.com; open the file index.html in a text editor:

nano /var/www/example.com/public_html/index.html

In this file, create a simple HTML document that specifies which site the page is connected to. In this case, this file looks like this:
<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Success!  The example.com virtual host is working!</h1>
</body>
</html>

Save and close the file.

You can then copy the file and use it as a template for the second site:

cp /var/www/example.com/public_html/index.html
/var/www/test.com/public_html/index.html

Open the copied file and make the appropriate adjustments:

nano /var/www/test.com/public_html/index.html
<html>
<head>
<title>Welcome to Test.com!</title>
</head>
<body> <h1>Success!  The test.com virtual host is working!</h1>
</body>
</html>

Save and close the file. The test pages are now ready.

4: Create virtual host files

Files, virtual hosts specify site-specific settings and help Apache to correctly answer the queries.

Apache comes with a standard host file named 000-default.conf, which can be used as a template. Copy it to create a virtual host for each domain name.

Note: by default, Ubuntu settings require virtual host files to end on .conf.

First, copy the file for the first domain:

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

Open the file in a text editor with root privileges:

sudo nano /etc/apache2/sites-available/example.com.conf

With the comments, the file will look something like this:

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

As you can see, the file is not so voluminous. Now you need to enter data on the first domain, and add a few directives. This virtual host will respond to requests on a standard HTTP port 80.

First, you need to change the ServerAdmin Directive by specifying the site administrator email address.

ServerAdmin admin@example.com

After that, you need to add two directives. The first is ServerName, which defines the base domain for which the host is intended. The second is ServerAlias, which defines domain aliases (for example, a domain prefixed with www):

ServerName example.com
ServerAlias www.example.com

It remains to make the last change to the host file-specify the location of the document root of this domain. Specify the previously created directory in DocumentRoot:

DocumentRoot /var/www/example.com/public_html

As a result, the host file looks like this:

<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file.

Copy the newly created file and use it as a template for the virtual host of the second site by editing the appropriate data.

sudo cp /etc/apache2/sites-available/example.com.conf
/etc/apache2/sites-available/test.com.conf

Open a new file with root privileges in a text editor:

sudo nano /etc/apache2/sites-available/test.com.conf

Now we need to edit the corresponding data so that the file can serve the second domain. As a result, the second virtual host file looks like this:

<VirtualHost *:80>
ServerAdmin admin@test.com
ServerName test.com
ServerAlias www.test.com
DocumentRoot /var/www/test.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file.

5: Enable virtual hosts

After creating virtual host files, you must enable them. Apache provides special tools for this.

For example, you can use the a2ensite:

sudo a2ensite example.com.conf
sudo a2ensite test.com.conf

Then disable the default virtual host 000-default.conf:

sudo a2dissite 000-default.conf

Restart Apache to activate the changes:

sudo systemctl restart apache2

Note: the system uses systemctl Ubuntu 16.04.

You can also use the command:

sudo service apache2 restart

This command is still supported, but it can return a nonstandard result.

6: Setting up local hosts (optional)

If you do not have a domain name and have used a conditional domain instead, you can test the settings by temporarily editing the hosts file on your local computer. it will intercept requests to previously configured domains and route them to VPS (that is, it will run DNS). But this method works only on the local machine and is only suitable for testing.

Note: make Sure you went to your local machine. You must have administrator credentials to run this topic.

On Mac or Linux systems, edit the hosts file with administrator privileges.

sudo nano /etc/hosts

Note: Windows Users can find the necessary instructions at this link.

In this file, you specify the IP address of the server, and then the domain name that you want to use to access the server.

For example, if the server IP address is 123.123.123.123, the following lines should be entered at the end of the host file

127.0.0.1 localhost
127.0.1.1 guest-desktop
123.123.123.123 example.com
123.123.123.123 test.com

Now all requests to example.com and test.com will be sent to the local computer, and from there — to the IP address of the server.

Save and close the file.

7: Results

To test the setting up virtual hosts, just open the domain in a web browser:

http://example.com

In this case, a message appears on the screen:

Success! The example.com virtual host is working!

Note: the Result that appears on the screen depends on the content of the file created in section 3.

Similarly, you need to check the second site:

http://test.com

The following result appears on the screen:

Success! The test.com virtual host is working!

If both sites are running, then the virtual hosts are successfully configured. If the hosts file on the local computer has been modified, after testing, delete the lines entered into it, so as not to clog the file with unnecessary entries.

Conclusion

The server is now Ubuntu 16.04 maintains two websites on separate domains. In General, the number of hosts that Apache can support depends only on the resources of the virtual dedicated server. To add a new virtual host to the server, simply repeat the above process.

Please follow and like us:

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *