Initial web server setup with Apache, mod_rewrite and Virtual Host.
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.
Use the apt-get package manager to install Apache 2:
$ sudo apt-get install apache2
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
Insert some random HTML to test. I prefer a valid HTML 5 document:
<!DOCTYPE html>
<html>
<head>
<title>Website</title>
</head>
<body>
Hello world
</body>
</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
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName website.tld
ServerAlias www.website.tld
DocumentRoot /var/www/website.tld
...
<Directory /var/www/website.tld>
...
AllowOverride All
...
</Directory>
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:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName website.tld
ServerAlias www.website.tld
...
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
Now we should be ready with our new Virtual Host.
Restart Apache to activate the new configurations:
$ sudo service apache2 restart
or $ sudo /etc/init.d/apache2 restart
If you got the domain pointed at your IP-address already, just access that to see the results, otherwise change your local computers hosts-file:
$ vim /etc/hosts
On a new line write the ip-address of your server followed by the domain you have created a virutal host for:
127.0.0.1 localhost.localdomain localhost
...
109.202.159.44 website.tld
See more at http://httpd.apache.org/