Setting up Apache Virtual Hosts

To enable the use of multiple virtual hosts, we use name-based as well as ip-based virtual hosts. To setup the virtual host, we use the following template.

    ServerName sitename
    ServerAdmin webmaster@sitename

    AccessFileName .htaccess
    DocumentRoot /home/www-data/sitename
    <Directory /home/www-data/sitename>
        Options SymLinksIfOwnerMatch
        AllowOverride AuthConfig
        Order allow,deny
        Allow from all

    DirectoryIndex index.html
    ErrorDocument 404 /404.html

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

From the above template, we set sitename to the URL of the virtual host and create a directory for it, setting the ownership of it to www-data:www-data as well as granting the owner and group full access to it. We also create a directory under the main apache log files for the virtual host’s log files.

We create a phpinfo.php file in the root directory of the virtual host to ensure the availability and configuration of PHP on it. The content of the phpinfo.php file is the following.


To enable the virtual host, we execute the following command in a “root” shell.

a2ensite sitename

To reload the configuration, we reload the apache2 service by executing the following command in a “root” shell.

service apache2 reload

One thought on “Setting up Apache Virtual Hosts

Comments are closed.