Configuring virtualhost(s) in Apache

Virtualhosts allow you to host multiple websites on a single webserver. Shared hosting is based on this idea as this allows multiple customers to use a single server. Cheap hosters usually put a lot of sites on a single server. The resources of the server (CPU and MEM) are shared with all sites which might have an impact on the speed and realiability. It’s better to use something like container hosting, which puts every user in a separate container. Cyberfusion offers high quality container hosting.

In this blog post, adding, enabling or deactivating virtualhosts will be covered.


In /etc/apache2/apache2.conf the virtual host configuration is included:

# Include the virtual host configurations:
Include sites-enabled/

This will include all configuration files in the /etc/apache2/sites-enabled/ directory. But there aren’t any files actually there, those are symlinks to the files in the /etc/apache2/sites-available/ directory:

$ ls -l /etc/apache2/sites-enabled/
lrwxrwxrwx 1 root root 28 2011-08-23 14:54 -> ../sites-available/

Apache save all available virtualhost in /etc/apache2/sites-available/ and when enabled, it will create a symlink in /etc/apache2/sites-enabled/ which points to the config file in sites-available.

Add virtual host

To configure a virtualhost, you should create a config files in the sites-available directory. You can use the template below. Replace [host] with the domain you want and adjust the directories to your liking/situation:

# [* (all) or specific ip-adres]:[port]
<VirtualHost *:80>
    ServerName [host]
    DocumentRoot "/var/www/[host]/public"
    ServerName www.[host]
    ServerAlias [host], *.[host]

    ErrorLog /var/www/[host]/logs/error.log
    CustomLog /var/www/[host]/logs/access.log common
    <Directory "/var/www/[host]/public/">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
         AllowOverride All
        Order allow,deny
        allow from all

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

(De)Activate virtualhost

Activating or deactivating a virtualhost can be done with two commands:


sudo a2ensite


sudo a2dissite

The name is the same as the config file in: /etc/apache2/sites-available/.

You should check your configuration first. Apache provides an easy helper which checks the syntax of your config files:

sudo apachectl configtest

When the config is correct, the following will be shown:

Syntax OK

To publish your changes, you need to reload apache:

sudo /etc/init.d/apache2 graceful

Access your local virtualhost

The virtualhost can be used online with a real domain, or locally with a “virtual” domain.

To allow access to the virtualhost locally, you need to adjust your hosts file. The hosts file is located in /etc/hosts (Linux) or %windows%/system32/drivers/etc/hosts (Windows). Add:

[ip_server] [host]

Example: dvdheiden.local

This makes it possible to visit the address in your browser: http://dvdheiden.local/.

Be aware: Apache support an unlimited amount of virtualhosts, but if you have more then 64 virtualhosts and they have their own log files a problem with the File Descriptor Limits could occur. This can be solved with this Apache guide.