Menu

Initial web server setup with Apache, mod_rewrite and Virtual Host.

14 December 2013

Apache is the most popular httpd/web server. It is Open Source and known for its easy-to-configure core. Apache is originally created for UNIX/Linux systems, but has also been brought to Windows. We will use Apache version 2.x.

We will use the domain website.tld as a placeholder for the domain you will use for your virtual host. Replace website.tld with your own domain.

Create a new directory in your www-folder, to keep your Virtual Hosts in the same directory. You can called it whatever you want, because you define a document root in your configuration file. I prefer to call it what the domain is called, as i find this most logical: $ mkdir /var/www/website.tld

Make sure that the directories can be accessed, with chmod recursively: $ chmod -R 755 /var/www/

Create a html-document in your new virtual host's directory, so you have some demo content: $ vim /var/www/website.tld/index.html

Create your new Virtual Host by copying the default (root) Virtual Host file called "default", to the same directory with the available virtual hosts/sites: $ cp /etc/apache2/sites-available/default /etc/apache2/sites-available/website.tld/

Go and edit your new virtual host file with your prefered editor, so it matches the new virtual host's domain (website.tld). I like Vim: $ vim /etc/apache2/sites-available/website.tld

Correct the string "/var/www" to your directories path "/var/www/website.tld" everywhere in the configuration, eg. in the DocumentRoot and in the Directory-element. It is a good idea to correct AllowOverride None to AllowOveride All in the Directory-element for your Virtual Host, if you would like to use .htaccess configuration. Insert ServerName and ServerAlias under ServerAdmin webmaster@localhost in the top of the file:

Apache got a few built in tools for enabling/disabling modules and websites/virutal hosts, here we will use the a2ensite shortcut, to enable our new virtual host: $ sudo a2ensite website.tld

At the same time it is great idea to enable the mod_rewrite module from Apache, as most web applications (including all the biggest CMS-systems) have this as a requirement. You can do this with the following shortcut: $ sudo a2enmod rewrite