How to set up Apache virtual hosts on Debian 8

Apache web server is one of the most popular ways to process web content on the Internet. It accounts for over half of all active websites on the Internet and is extremely powerful and flexible.

Apache breaks down its functionality and components into separate units that can be configured independently of each other. The basic unit that describes an individual site or domain is called a virtual host.

When using virtual hosts, you can use one server to host multiple domains or sites from the same interface or IP using a negotiation mechanism. Set up a domain query to direct the visitor to a specific directory containing information about the site. In other words, you can host more than one website on one server. This scheme can be extended without any software constraints as long as your server can handle the load.

In this tutorial, we will create two Apache virtual hosts on a Debian 8 server, with different content for visitors based on the domain they visit.

Prerequisites

To complete this tutorial, you will need:

  • Debian 8 server with non-root user with sudo privileges. You can set up a user with these privileges in our Debian 8 initial server setup guide.
  • Apache is installed and configured as shown in the How to Install Linux, Apache, MySQL, PHP (LAMP) on Debian 8 guide.

In this tutorial, we will create virtual hosts for example.ru and test.ru, but you can replace with your own domains or values ​​by following.

If you don’t have domains available, you can use example.ru and test.ru and follow Step 5 of this tutorial to configure the local hosts file to map these domains to your server’s IP address. This will allow you to check your configuration from your local computer.

Step 1 – creating the directory structure

The first step we’re going to take is to create a directory structure that will contain the site data that we will share with visitors.

Our document root, the top level directory that points to Apache, will be installed in separate directories under the directory /var/www… We will create a directory for each of the virtual hosts that we will configure.

In each of these directories, we will create a folder public_htmlwhich will contain the web page. This gives us a little more flexibility in how we apply more complex web applications in the future; folder public_html will hold web content and a parent folder may contain scripts or application code to support web content.

Create directories using the following commands:

sudo mkdir -p /var/www/example.ru/public_html

sudo mkdir -p /var/www/test.ru/public_html

Since we created directories with sudo, they are owned by our superuser. If we want our regular user to be able to modify files in our web directories, we’ll change the ownership, something like this:

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

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

Переменная $USER uses the value of the user that you logged in with when you clicked ENTER… By doing this, our regular user currently owns the subdirectory public_htmlwhere we will store our content.

We also need to change our permissions to ensure that read access is allowed on the shared web directory and all the files and folders it contains. Run this command to change folder permissions /var/www and its subfolders:

sudo chmod -R 755 /var/www

Your web server should now have the permissions required to serve content, and the user should be able to create content in the correct folders. Let’s create an HTML file for each site.

We have created our directory structure. Let’s create some content.

Step 2 – Creating Default Pages for Each Virtual Host

Let’s create a simple page index.html for each site. This will help us ensure that our virtual hosts are configured properly.

Let’s start with a page for example.ru… Let’s edit a new file index.html with the following command:

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

In this file, create a simple HTML document that points to the home page when a visitor lands on example.ru:

/var/www/example.ru/public_html/index.html

<html>
  <head>
    <title>Добро пожаловать Example.ru!</title>
  </head>
  <body>
    <h1>успех! Виртуальный хост example.ru работает!</h1>
  </body>
</html>

Save and close the file when you’re done.

Now copy this file to the site test.ru:

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

Then open the file in an editor:

nano /var/www/test.ru/public_html/index.html

Change the file so that it refers to test.ru instead example.ru:

/var/www/test.ru/public_html/index.html

<html>
  <head>
    <title>Добро пожаловать на Test.ru!</title>
  </head>
  <body> <h1>Успех! Виртуальный хост test.ru работает!</h1>
  </body>
</html>

Save and close this file. You now have the pages you need to check your virtual host configuration. Next, let’s set up virtual hosts.

Step 3 – Create a new virtual hosts file

The virtual hosts file points to the actual configuration of our virtual hosts and dictates how the Apache web server will respond to various domain requests.

Apache comes with a default virtual hosts file called 000-default.confwhich can be used as a starting point. Copy this file for the first domain:

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

Note : The default Apache configuration in Debian 8 requires every virtual host file to end with the extension .conf

Open a new file in an editor:

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

The file will look like the following example, with some additional comments:

/etc/apache2/sites-available/example.ru.conf

<VirtualHost *:80>

        ServerAdmin [email protected]
        DocumentRoot /var/www/html

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

</VirtualHost>

This virtual host handles any requests made to the port 80, the default HTTP port. Let’s make a few changes to this configuration, and add a few new directives.

First, let’s change the directive ServerAdmin by e-mail that the site administrator can receive messages via e-mail.

/etc/apache2/sites-available/example.ru.conf

ServerAdmin [email protected]

Next, we need to add two new directives. The first of them is called ServerName, sets the base domain for this virtual host. The second is called ServerAlias, specifies further names to match as if they were the base name. This is useful for matching additional nodes that you have defined, since example.ru and www.example.ru both work, provided both of these hosts point to the IP address of that server.

Add these two directives to the config file, right after the line ServerAdmin:

/etc/apache2/sites-available/example.ru.conf

<VirtualHost *:80>

        ServerAdmin [email protected]
        ServerName example.ru
        ServerAlias www.example.ru
        DocumentRoot /var/www/html
...

Next, change the location of the document root for this domain by changing the directive DocumentRootto point to the directory created for this host:

DocumentRoot /var/www/example.ru/public_html

After you’ve made these changes, your file should look like this:

/etc/apache2/sites-available/example.ru.conf

<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName example.ru
        ServerAlias www.example.ru
        DocumentRoot /var/www/example.ru/public_html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file.

Then create a second config file by making a copy of this file:

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

Open a new file in an editor:

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

Then change the appropriate settings to link to your second domain. When you’re done, your file will look like this:

/etc/apache2/sites-available/test.ru.conf

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName test.ru
    ServerAlias www.test.ru
    DocumentRoot /var/www/test.ru/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file.

Now that we have created the virtual host files, we can enable them.

Step 4 – Including New Virtual Host Files

You have created folders and configuration files for your virtual hosts, but Apache will not use them until you enable them. You can use the tool a2ensite in order for each of your sites to work.

Activate the first site:

sudo a2ensite example.ru.conf

You will see the following output if there were no syntax errors or typos in your file:

Output

Enabling site example.ru.
To activate the new configuration, you need to run:
  service apache2 reload

You must restart Apache for your changes to take effect. But before you do, enable another site:

sudo a2ensite test.ru.conf

You will see a similar message indicating that the site has been enabled:

Output

Enabling site test.ru.
To activate the new configuration, you need to run:
  service apache2 reload

Then disable the default node defined in 000-default.conf using the command a2dissite:

sudo a2dissite 000-default.conf

Now restart Apache:

sudo systemctl restart apache2

The sites are now configured. Let’s check them out. If you are using real domains configured to point to your server’s IP address, you can skip the next step. But if your domains haven’t been configured yet, or if you’ve just done testing, read on to learn how to test our installation using your local computer.

Step 5 – Configuring Local Hosts File (Optional)

If you did not use the actual domain names you own to test this procedure using some sample domains instead of the real ones, you can at least test the functionality of this process by temporarily modifying the file hosts on the local computer.

This will allow you to intercept any requests for domains, configure and forward them to your VPS server, just like the DNS system does if you were using registered domains. This will only work from your computer, though, and is only useful for testing purposes.

Make sure you have completed the following steps on your local computer and not on the VPS server. You also need to know the local computer administrator password or be a member of the Administrators group.

If you are on a Mac or Linux computer, edit the local file as an administrator by typing:

sudo nano /etc/hosts

If you are on Windows, open a command prompt with administrator rights and type:

notepad %windir%system32driversetchosts

After you open the file, add a line that displays your server’s public IP address for each domain name, as shown in the following example:

/ etc / hosts

127.0.0.1   localhost
...

111.111.111.111 example.ru
111.111.111.111 test.ru

This will allow any requests to be directed to example.ru and test.ru on your computer and send them to the server at 111.111.111.111

Save and close the file. You can now check your installation. When you are sure everything is working, remove two lines from the file.

Step 6 – testing your results

Now that you have your virtual hosts configured, you can test the installation easily by going to the domains configured in your web browser. Visit the first site at http://example.ru and you will see a page that looks like this:

Success! The virtual host example.ru is working!

Likewise, if you visit your second host at http://test.ru, you will see the file you created for your second site:

Success! The test.ru virtual host is working!

If both of these sites are working, you have successfully configured two virtual hosts on the same server.

Note: If you configured the hosts file on your home computer as shown in step 5, then you can now remove these lines that you added, since you have already verified that your configuration works. This will prevent your hosts from filling up the file with entries that are not really needed.

Output

You now have one server with two separate domain names. You can extend this process by following these steps to add additional virtual hosts.

There is no software limit on the number of domain names that Apache can handle, so feel free to do as much as your server is capable of handling.

To use Apache to serve secure content, follow the tutorial on How to Install SSL and SPDY Certificate on Ubuntu Using Let’s Encrypt. To use Apache in front of your web application, read the guide on how to use Apache as a reverse proxy with mod_proxy on Debian 8.

Sidebar