Setting up Virtual Hosts

Updated on 28 Dec 2018

Setting up virtual hosts in Apache is a fairly straight forward process, and these steps will help you get there.

Add the domain to the hosts file


An example of my hosts file is shown below.	localhost	application	yii

Create a new site file

In /etc/apache2/sites-available, create a new file with the same name as your host. I.e. yii.conf.

<VirtualHost *:80>
    ServerAdmin admin@localhost
    DocumentRoot "/var/www/yii"
    ServerName yii
    ErrorLog "/var/log/apache2/yii-error.log"
    CustomLog "/var/log/apache2/yii-access.log" common
    <Directory "/var/www/yii">
        Require all granted


At a minimum you might want to set up the following parameters.

Parameter Meaning
ServerAlias the email address for the server admin
DocumentRoot Typically the location of where your web pages are located
ServerName This will match one of the names in the hosts file
ErrorLog Where the log messages are written to
CustomLog Where normal access messages are written to

There are many more configuration options available, so by all means check them out here.

Set up the directory tag

You also need to set up the directory tag within your VirtualHost and specify how the directory can be accessed, and what privileges are available. At a basic level, it could look like this

<Directory "/var/www/yii">
    Require all granted

But we can over-ride many of the settings for individual directories if we want, i.e. log files, access etc. For a simple file, index.html, placed in the the /var/www/yii directory, I can expect an output similar to what is shown below.

ifModule dir_module

Various Apache modules provide additional functionality to the core http webserver. One of these is mod_dir which provides a DirectoryIndex directive which instructs Apache what to do when a client requests a directory without the file.

In the previous example I requested the file directly. http://yii/index.html. If I omit the filename and only request the directory http://yii, then I will get a directory listing of all the files and directories available.

turn off indexes

Inside your directory settings, you can remove this behaviour using the options directive.

<Directory "/var/www/yii">
    Options -Indexes
    Require all granted

In which case you end up with 404 page not found.

Or a forbidden message if you have autoindex enabled.

[Thu Jan 23 15:52:17.917514 2020] [autoindex:error] [pid 8771] [client] AH01276: Cannot serve directory /var/www/yii/: 
No matching DirectoryIndex (index.html,index.cgi,,index.php,index.xhtml,index.htm) found, and server-generated directory 
index forbidden by Options directive

There are many options you can set/turn off.

Enable mod_dir

Enabling mod_dir is done with the following command.

sudo a2enmod dir

In the /etc/apache2/mods-enabled we will see 2 files.

file purpose
dir.load Contains the command to the load the directory modules
dir.conf Contains the default configuration settings for the module

If we look inside the dir.conf file, we’ll see this.

What this means is that whenever a client requests a directory (no file), Apache will search for index.html, followed by index.cgi, etc. If Apache finds the file, it will automatically load it; and we can see an example of that below.

You can modify the dir.conf to provide additional files for the DirectoryIndex directive, or you can over-ride the default settings in the virtual hosts file.