Configure Apache Virtual Hosts on Debian 9 - Step by Step Process ?

Apache Virtual Hosts allows you to run more than one website on a single machine.

With Virtual Hosts, you can specify the site document root (the directory containing the website files), create a separate security policy for each site, use different SSL certificates, and much more.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Apache queries.

In this context, we shall look into how to configure Apache Virtual Hosts on Debian.


How to configure Apache Virtual Hosts on Debian ?

Before proceeding with this procedure, ensure that the following prerequisites are ready:


1. Create the Directory Structure

To begin, we will make a directory structure where website data will be stored and serving to visitors for a domain. 

You can set the document root to any location that you want but it's good practice to set in directory structure. 

Generally in all /var/www:

/var/www/
 ├── linuxapt.com
 │   └── public_html
 ├── example2.com
 │   └── public_html

Let's create document root directory for first domain. Execute following command to create directory :

$ sudo mkdir -p /var/www/linuxapt.com/public_html

We will also create a index.html file under the domain document root directory for testing purpose. This page will be show by default when visitors will visit your site.

Creating a new index.html file using your favorite text editor by typing :

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

Add the following lines into it:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Welcome!!</title>
  </head>
  <body>
    <h1>Success! linuxapt.com set up completed!</h1>
  </body>
</html>

Since we created the directory and files with sudo user so they are owned by our root user. 

We will change document root directories ownership to avoid permission issue for our regular user. 

Thus, regular user can modify files in our web directories without any issues:

$ sudo chown -R www-data: /var/www/linuxapt.com


2. Create Virtual Host Files

For a Debian machine, Apache Virtual Host configuration files are located at /etc/apache2/sites-available directory. So we will create Virtual Host configuration files at this location.

Create a new file using your choice text editor by typing :

$ sudo nano /etc/apache2/sites-available/linuxapt.com.conf
<VirtualHost *:80>
    ServerName linuxapt.com
    ServerAlias www.linuxapt.com
    DocumentRoot /var/www/linuxapt.com/public_html
    <Directory /var/www/linuxapt.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/linuxapt.com-error.log
    CustomLog ${APACHE_LOG_DIR}/linuxapt.com-access.log combined
</VirtualHost>

ServerName: This should be your domain name and match with virtual host configuration.

ServerAlias: All other domains or subdomains that should match for this virtual host as well, usually the www subdomain.

DocumentRoot: Path of virtual host directory that from which Apache will serve the domain files.

Options: This directive controls which server features are available in a specific directory.

-Indexes: It will prevent directory listings.

FollowSymLinks: Apache will follow the symbolic links if this option is enabled.

AllowOverride: Specifies which directives declared in the .htaccess file can override the configuration directives.

ErrorLog, CustomLog: Specifies the location for log files.

You can give any names to your configuration file but it's recommended to give file name same as domain name.

Now, We will enable virtual host file by creating symbolic links to the /etc/apache2/sites-enabled directory.

In Debian systems you can create symbolic links either by helper script or can create manually.

To create using a2ensite helper script execute following command :

$ sudo a2ensite linuxapt.com

To create symbolic link manually type :

$ sudo ln -s /etc/apache2/sites-available/linuxapt.com.conf /etc/apache2/sites-enabled/

Now check the syntax by type :

$ sudo apachectl configtest

It will show below output if there are no errors:

Output
Syntax OK

You must restart apache2 service to get changes effect by below command :

$ sudo systemctl restart apache2

You can verify by opening your http://linuxapt.com to your web browser and it will show you as following:

Success! linuxapt.com set up completed!

Finally, you created a virtual host for a single domain, like this you can repeat for multiple domains on a single server.


[Need urgent assistance in fixing Apache errors? We can help you. ]

This article covers how to Configure up Apache Virtual Hosts on a Debian 10 server. Using Apache Virtual Hosts you can host multiple domains on a single server. 

Apache will break its functionality and components into individual units so you can customize independently. The basic unit that describes an individual site or domain is called a virtual host.


How to configure Apache Virtual Hosts on Debian ?

Apache is a free and open source web server used web server in the world, and it is commonly used in Linux servers.

To install Apache:

1. Check whether apache is already installed and running on your server. You can do this with the following command:

$ dpkg -l apache2

2. If apache is not installed, you can do this by running the following commands. First, make sure that the system repositories are up to date:

$ apt-get update

3. To install the Apache web server, execute the following:

$ apt-get install apache2

4. After the installation is complete, you should enable Apache to start automatically upon server reboot with:

$ systemctl enable apache2

5. You can also check the status of your Apache service with the following command:

$ systemctl status apache2

If you want to secure your website with an SSL certificate, you can generate and Install a free Letsencrypt SSL certificate.

Related Posts