Last Updated:

Apache and Debian - Configuring Virtual Hosts

This article describes how to configure virtual hosts for Apache2 on the Debian8 Linux operating system.

But in general, this manual is suitable for other versions of Linux. Such as Linux Mint, Linux Ubuntu, Arch Linux and other OS with similar architecture.

 


This manual provides examples of running commands as root.
If you are not the root user, you must specify the "sudo" keyword at the very beginning of the command execution.

Prerequisites

First, you need to update the software and install Apache2.


In the example I gave, the root directory where the subdirectories containing the virtual host (Web Sites) files will be located is the directory:


Here is an example of creating a virtual host "test.com":
apt-get update
apt-get install apache2
/var/www


  1. You must go to the /var/www directory
    cd  /var/www
  2. Create test.com directory
    mkdir test.com
  3. Go to the created test.com directory
    cd test.com
  4. And again create the public directory
    mkdir public


In total, the created directory will have the following path:


Assigning rights

If you need to give rights to manage the directory to another user, then the following will tell you how to do this.

Let's say the user login qwerty.

The following command authorizes the user to manage this directory.


Now you need to give the necessary write and read permissions.
Grant read and write permissions:


Create a shared hosting file.

The final step in setting up virtual hosts will be to create a shared hosting file.

Change to /etc/apache2/sites-available.

In my case there was a file 000-default.conf

Copy the file 000-default.conf to a new file with your hostname.
According to this example, this would be test.conf



Open the generated test.conf file using a text editor (gedit or nano).


The contents of the virtual host settings file are approximately as follows:
/var/www/test.com/public
chown -R qwerty:users /var/www/test.com/public
chmod -R 755 /var/www/test.com
cd /etc/apache2/sites-available
cp 000-default.conf test.conf
nano test.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 webmaster@localhost
	DocumentRoot /var/www/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>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet


Specify the E-mail of the administrator:

It is necessary to add directives with the name of the virtual host and aliases:



The last directive remains.
Specifies the location of the root directory for the domain.


The host file should now look like this:
ServerAdmin webmaster@localhost
ServerName test.com
ServerAlias www.test.com
DocumentRoot /var/www/test.com/public
 
<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 webmaster@localhost
	DocumentRoot /var/www/test.com/public
	ServerName test.com
	ServerAlias www.test.com

	# 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>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet


Save the file and close (ctrl+o and ctrl+x).

  1. Next, go to /etc/apache2/sites-enabled
    cd /etc/apache2/sites-enabled
  2. And create a symbolic link to the file test.conf
    ln -s .. /sites-available/test.conf test.conf


Enable virtual hosts.

Virtual host files were created.
Now you need to enable them.
You can enable them as follows:


In order for the changes to take effect, you must restart apache.
This can be done as follows:


Configuring a host file for OS Linux

How do I open a virtual domain on a local OS Linux machine?
sudo a2ensite test.com
/etc/init.d/apache2 restart
 

If you used not real domain names, but test ones, then you can check them by specifying the addresses in the host file.


Add a string to the end of the hosts file with your IP address and test domain name.
For example:


Now you can check the operation of your test site by clicking on the link:
sudo nano /etc/hosts
127.0.0.1		test.com

http://test.com/


Configuring host file for OS Windows
How to open a test domain with OS Windows

 

You need to go to the following directory:

Next, open the hosts file with Notepad with administrator privileges.
Add a string to the end of the hosts file with your IP address and test domain name.

Instance:


You can now test your test site by following the link:
C:\Windows\System32\drivers\etc
127.0.0.1		test.com

http://test.com/