Apache is a popular web server which has grabbed a major slice of the web server market. What is interesting about Apache is its stability and scalability which makes it possible to serve even very high traffic web sites without any hitch. It also helps that it comes with an unbeatable price (free). It is bundled by default with most Linux distributions. And in cases where it is not included in the CD, it is a simple case of downloading the package and installing it.
Here I will explain how one can set up Apache web server to serve ones web pages from your machine.
Packages to be installed
apache2 , apache2-common , apache2-mpm-prefork
And optionally ...
apache2-utils , php4 (If you need PHP support) , php4-common
Apache2 has a very modular structure. In fact if you see the directory structure of the configuration files (see figure below), you will realise that it is designed to avoid clutter. For serving web pages on our machine, we need be concerned with just two directories - them being, /etc/apache2/sites-available/ and /etc/apache2/sites-enabled/ .
In the sites-available directory you create the files (can be any name) containing the configuration details of the sites you aim to host - one file per site. And in the sites-enabled directory, you create a soft link to the previously created file.
Let us assume for the sake of this tutorial that I have decided to host two websites on my local machine. All the files related to the two websites have already been created and saved in two separate directories by name websiteA and websiteB . The default location for serving the files in apache is usually in the /var/www location. So I move the two directories websiteA and websiteB to this location.
The -p option preserves the ownership of the files and directories while copying.
$ sudo cp -R -p websiteA /var/www/.
$ sudo cp -R -p websiteB /var/www/.
Next I move into the directory /etc/apache2/sites-available in order to configure apache web server to recognize my sites. In this directory, there is a file called default which contains the configuration parameters. It is meant to be a skeleton file which can be used as a base for additional configuration.
I made two copies of this file in the same directory and renamed them as websiteA and websiteB. You can give any name really but for clarity it is prudent to give them the name of your site.
Now I opened up the file /etc/apache2/sites-available/websiteA in my favourite editor (vi) and made changes to the following portions (shown in bold):
Options Indexes FollowSymLinks MultiViews
allow from all
In the above listing, ServerName indicates the name (or alias) with which your site has to be recognised. Usually a webserver will be hosting multiple sites with different domain names on the same machine. At such times, the webserver distinguishes between different websites through the ServerName directive. Since this is a local machine, I have given it the name websiteA. But in actual hosting where you want to make the website available on the net, you will be giving it your domain name say like www.mysite.com or something.
The DocumentRoot option denotes which files to be served when the server name is entered in the web browser. This usually points to the place where you have stored your website files.
You can have multiple Directory tags but one of them should point to the location of the website files.
I made similar changes to the file websiteB where the ServerName was given a unique name websiteB instead of websiteA as given above. Also the Directory tag contained the path /var/www/websiteB/ instead of websiteA.
Finally, because I was hosting this on my local machine and since I had not configured DNS, I had to edit the /etc/hosts file and include the name given in the ServerName portion of the configuration file. After the inclusion, my machine's /etc/hosts file looked as follows:
#FILE : /etc/hosts
127.0.0.1 localhost.localdomain localhost websiteA websiteB
That was it. Now in order to enable the websites, all I had to do was create a symbolic link to these files in the /etc/apache2/sites-enabled/ directory and restart the web server. It is quite clear that to disable a website, all you need to do is remove the symlink in the sites-enabled directory.
Restart the apache web server
$ cd /etc/apache2/sites-enabled
$ sudo ln -s /etc/apache2/sites-available/websiteA .
$ sudo ln -s /etc/apache2/sites-available/websiteB .
$ sudo apache2ctl restart
$ sudo /etc/init.d/apache2 restart
Now I tested it by opening up the web browser and typing http://websiteA to get the first website and http://websiteB to get the second website. Voila! Success!!.
Note: In previous versions of Apache, all these configuration parameters were inserted in the /etc/httpd/httpd.conf file. But I feel, apache2's configuration file layout is much more intutive and easier to manage.