I finally got an apache2 server working on my laptop, after hours of (almost pointless) editing of configuration files, server restarts, reading Ubuntu forums[1] and googling. It was lack of all three, information, insight and luck that made this what-should-have-been simple process drawn out and painful.
1) Install apache2
sudo apt-get install apache2 apache<version>-common apache2-mpm-worker
The version I’m using is 2.2. To not bother about version numbers and dependencies, install apache2 using Synaptic Package Manager
2) Open your browser and type
http://localhost/
It should display the page index.html located in /var/www/, with a message “It works!” Proceed only this step is successful.
3) Create a new user, say, myserver
sudo adduser myserver
This step is necessary because you have to give apache2 access to folders /home/, /home/user/ and a folder called /home/user/public_html/. It is prudent to use a dummy user like myserver instead of yourself.
4) Create a folder called public_html/ in /home/myserver/
sudo mkdir /home/myserver/public_html/
Create your web page and put it in this folder under the name index.html (apache2 searches for an .html file with this name). If you want to host a directory, simply put the directory here.
5) Give apache access permissions to this folder hierarchy
sudo chmod -R 775 /home/myserver/
To make editing of the .html files easier (so that you don’t need to use sudo everytime) change the myuser/ hierarchy in your group, before running the above sudo chmod command. (I’m not sure how safe this is, though)
sudo chgrp -R your_group_name /home/myserver/
sudo chmod -R 775 /home/myserver/
6) Enable user directory module in apache
sudo a2enmod userdir
Now the files userdir.conf and userdir.load will be created in /etc/apache2/mods-enabled/ folder. Some websites suggest creating a link in the mods-enabled/ folder to the same files kept in /etc/apache2/mods-available/ folder (instead of using a2enmod) but I read that this method could be less safe.
7) Open the file /etc/apache2/httpd.conf and add the following line to it
ServerName localhost
Later on, ‘localhost’ can be replaced with the actual name of your site.
8) Restart the server
sudo /etc/init.d/apache2 restart
sudo /etc/init.d/apache2 force-reload
(I don’t know if this is a bug, but sometimes, just restarting or force-reloading doesn’t cause the changes made to be registered)
9) Open your browser and test the server with:
http://localhost/~myserver/
It displays either the /home/myserver/public_html/index.html file, if it exists, or displays the public_html/ folder hierarchy.[2]
10) Next steps include port forwarding in your router, changing listening port (if required), registering in a DNS site like dyndns.com, fiddling around with a few more settings and you have a web site! This, for later, in this post.
[1] This was done on Ubuntu 8.04 LTS Hardy Heron
[2] In case its still not working, go to the folder /etc/apache2/sites-available/, make a backup of the file default. Open this file with a text editor with root permissions.
sudo cp default default.OLD
sudo gedit default &
The default directory where apache searches for your web page is in /var/www/. We change this default directory to /home/myserver/public_html/ at two places, as highlighted below:
NameVirtualHost *
<VirtualHost *>
ServerAdmin webmaster@localhostDocumentRoot /home/myserver/public_html/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/myserver/public_html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
Now restart the server as in step 8), open your browser and test the server with:
http://localhost/