Setup Virtual Host

Posted by Stefan Kecskes on Friday, July 3, 2015

Create Virtual Host conf

Today we will be creating virtual host on Apache2 web server. As one Apache2 web server can serve multiple websites we can later add more configs and tell Apache2 web server to serve all the websites from single unix instance. I will assume that the name of our webserver is

First create a config file:

sudo nano /etc/apache2/conf.d/sites-available/

and paste the following configuration into that config file.

<VirtualHost *:80>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride ALL
        Order allow,deny
        allow from all

    DocumentRoot /var/www/

	# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
	# error, crit, alert, emerg.
	#LogLevel info ssl:warn

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

Save. This is one of the most basic configurations where we are telling apache to use this config if it catches any request coming to ServerName or on port 80. It also tells apache to serve files from folder /var/www/ and who is the admin of this server. Apache2 on its own creates a bunch of logs that could be helpful in the future, and therefore we are telling it to store this website specific error logs and access logs in specific locations.

Activate Virtual Host conf

Now the apache might have many predefined available config in etc/apache2/sites-available folder, but not all of them might be active at the moment. Therefore, we create a symlink into /etc/apache2/sites-enabled

sudo ln -s /etc/apache2/sites-available/ /etc/apache2/sites-enabled/

Add new Virtual Host to systems hosts file

We will add the following line to the bottom of the /etc/hosts file

Update apache default conf file

The location of apache2 conf file depends on the system you are using. In debians like Ubuntu it is usually in /etc/apache2/apache2.conf file but in Centos systems it resides at /etc/httpd/httpd.conf file. Alternatively, if you installed apache in unstandard way it may live in different location. Therefore, make sure to edit the correct apache2 conf file.

Open the apache2 default conf file:

    sudo nano /etc/apache2/apache2.conf

and add the following to the bottom of it:

ServerName localhost
ServerSignature Off
ServerTokens Prod
IncludeOptional conf.d/sites-enabled/*.conf

The first line makes the current folder settings default to localhost only.

The second line will hide the apache version the operating system and installed extensions in response headers, which you want to do so that hackers will not know what software you are using to run your webserver.

The third line will set tokens to production level security.

Finally, the fourth line will add our sites-enabled folder full of our virtual hosts to this main default config.

Activate changes on Apache

and to take effect of all these changes we will restart apache2 webserver

service apache2 restart

Enjoy your server serving your websites!