Articles

The Apache 2 Web-Server

Introduction

These notes describe how to install the Apache 2 Web-Server and configure it to enable users to have their own web sites, with

http://yourdoman/~user
as the url. We will look at installations for both Ubuntu and Cygwin. The Ubuntu installation of Apache 2 is generic to all Linux distributions.

The procedures outline on this page work for me. However, depending on your system configuration, they may not work for you without modification.

Apache remains a key component in the vast majority of networks on the Web, currently running more than half of the world's sites. The latest version, Apache 2.2, is a major enhancement over the earlier versions of 2.0 and 2.1. In particular, the default configuration layout has been simplified and modularised.

Get and Install Apache 2

Ubuntu (compiling from source)

I recommend downloading the gzip tarball and compiling from source. Do not forget to read all the INSTALL and README files that come with the source. I prefer unzip the tarball in stages

$ gunzip file.tar.gz
$ tar -xf file.tar

rather than use the xvvzf option for the tar command.

Compiling from source gets over the major problem with Ubuntu in that its package manager (synaptic) never seems to have the latest versions of software. I had no problems installing Apache 2.2.15 on Ubuntu 7.04 using:

$ ./configure --prefix=/usr/local/apache2 
$ make
$ sudo make install

The sudo command is necessary since you need administrator rights. Notice, Apache2 has been installed in the root directory:

/usr/local/apache

This is Apache's suggested location. It may also be the default location for your source files, making the specific specification of the prefix switch unnecessary. Read the INSTALL file. Of course, should you wish, you can choose a different location.

Ubuntu (download manager)

Getting Apache 2 is straightforward using the download manager (synaptic). It is the only option if you are unhappy about installing from the source code, but you will probably only get the older Apache 2.1. Also, synaptic usually installs Apache 2 into the directory

/usr/share/apache2

So beware!

Cygwin

Fortunately, the Cygwin package manager has Apache 2.2 available so it is therefore easiest to use this route rather than download the source code. However, for Apache 2 to work in Cygwin, the Cygwin server (see below) must also be installed and running. The Cygwin package manager installs Apache 2.2 into the root directory

/usr/share/apache2

The next section below summarises the locations of the files for the different locations of the root directory.

Installation Directories for Root Locations

ROOT: /usr/local/apache2
binaries: /usr/local/apache2/bin
www: /usr/local/apache2/htdocs
cgi-bin: /usr/local/apache2/cgi-bin
configs: /usr/local/apache2/conf
ROOT: /usr/share/apache2
binaries: /usr/sbin    /usr/bin
www: /usr/share/apache2/htdocs
cgi-bin: /usr/share/apache2/cgi-bin
configs: /etc/apache2

Starting Apache 2

Depending on the root location, you start Apache 2 by entering either the command

$ /usr/local/apache2/bin/apachectl2 start

or the command

$ /usr/sbin/apachectl2 start

In Ubuntu, you will probably also have to use sudo. Confirm all is working normally by pointing a browser to

http://localhost

or to

http://127.0.0.1

You should then see the message

It Works!

It may also be possible to use

http://your_host

if the name of your host is in the host file.

In Cygwin, you might get a Windows Security Alert when you attempt to start Apache 2. The Alert comes from your Firewall, with the message:

Name of program: httpd2
Click Unblock to continue.

Just click unblock to continue.

Local-User Web Site

First, we make the directory for the html pages and the local-user cgi-bin directory. It is easier to keep the default setting of

/home/user/public_html

for the local-user directory.

Ubuntu

1. Enter the following commands:

$ cd
$ mkdir public_html 
$ mkdir public_html/cgi-bin

to make the local-user web-page and cgi-bin directories. Notice again that they are in the home directory of the user.

2. Now enter the following commands:

$ cd /etc/apache2/mods-enabled
$ sudo ln -s ../mods-available/userdir.conf userdir.conf
$ sudo ln -s ../mods-available/userdir.load userdir.load

with the second and third commands as administrator. The soft links made by ln -s have the effects of activating the userdir module. Look at the Apache 2 documentation if you would like to know more about modules and directives.

3. Open the file /etc/apache2/mods-enabled/user.conf with a text editor and enter the following lines:

<Directory /home/*/public_html/cgi-bin/>
Option ExecCGI
SetHandler cgi-script
</Directory>

The file user.conf should look something like this:

<IfModule mod_user.c>
UserDir public_html
UserDir disabled root

<Directory /home/*/public_html>
AllowOverride FileInfo AuthConfig Limit
Options Multiviews Indexes SymLinksIfOwnerMatch IncludesNoExec
</Directory>

<Directory /home/*/public_html/cgi-bin/>
Option ExecCGI
SetHandler cgi-script
</Directory>

</IfModule>

(4) Restart the Apache 2 server. The simplest way is to enter

$ sudo /etc/init.d/apache2 restart

on the command line.

Cygwin

Unfortunately, the Apache 2 server on Cygwin has its files arranged differently from the Linux case.

1. Do the same as (1) in the Ubuntu case above, in order make the public_html and cgi-bin directories.

2. Edit the file /etc/apache2/httpd.conf. It would be wise first to make a copy of the file.

3. Now insert the following about two-thirds of the way down:

<IfModule userdir_module>
UserDir public_html
UserDir disable root

<Directory /home/*/public_html>
AllowOverride FileInfo AuthConfig Limit
Options Multiviews Indexes SymLinksIfOwnerMatch IncludesNoExec
Order allow,deny
Allow from all
</Directory>

<Directory /home/*/public_html/cgi-bin/>
Options ExecCGI
SetHandler cgi-script
</Directory>

</IfModule>

4. Restart the Apache 2 server. Remember that the Cygwin server (see below) must also be running for Apache 2 to function. When you start Apache 2, you may receive a Windows Security Alert (as mentioned above) from your Firewall asking if you want to keep blocking the program httpd2. Just click to unblock.

Permissions

The public_html directory and all its sub-directories should be set to 755 or "a+xr". This is also the case for cgi scripts you may have installed.

Cygwin Server

To ensure you have a Cygwin server installed, enter

$ cygwin-config

in the Cygwin bash shell. You will then be asked:

Do you want to install cygserver as service?
(Say "no" if it's already installed as service) (yes/no)

Enter "yes" to install the Cygwin server. I have never found it necessary to change the cygserver configuration options from their default settings.

In the past, you had to set the system environment CYGWIN to server. This is no longer necessary with later versions of Cygwin.

In order to start the Cygwin server, enter

net start cygserver

Remember that this is a

C:\WINDOWS\system32

program so that directory has to be in the path if you enter net start in a bash shell. Alternatively, you can start the Cygwin server with the bash program

$ cygrunsrv -S cygserver

To stop the cygwin server, use either

net stop cygserver

or

cygrunsrv -E cygserver

Happy Apache!


Books

Ideal for handy on-the-job reminders and essential information. Order from Amazon:  amazon.com  Amazon.co.uk
Teach yourself all you need to know about Apache 1.3 and Apache 2. Order from Amazon:  amazon.com  Amazon.co.uk