Note: Assuming your browser supports it, this list will not be printed out. Hide navigation

Setting Up An Apache Server For Testing

Wednesday 3rd August 2005 - Friday 9th September 2005

Introduction

When testing (X)HTML files, you can simply open them up in the browser of your choice and check if they work properly. However, if you use PHP or another method of changing the web page server side, you can't test your page locally, or at least not just with a web browser. You need to install a web server that can generate the pages for you - and Apache fits the bill.

This guide assumes you're on Debian - if you're not, many of the stages are still the same.

Apache 1

This page is for those of you wanting to install Apache 1 - those that want to install Apache 2 should go to the next page. If you're not sure which version you want, then you'll probably want to install Apache 2.

First of all, you need to install Apache itself. So, as root in a terminal, type apt-get install apache or use synaptic, or whatever you normally use to fetch packages. Next, we want Apache to be able to generate your pages - if we want to be able to generate PHP pages, we install the package php4.

Now, Apache should be running - test by going into a web browser and typing in localhost as a web address. If everything's working as it should, you'll get an Apache placeholder page.

Before we start configuring Apache to load your pages, we need to make sure that Apache can only be accessed by the machine it's on, since we're only using it to test web pages. We do this by editing /etc/apache/http.conf. You'll need root permissions to do this, so the easiest way is to open up a terminal, become root and type nano /etc/apache/http.conf. Once in this file, add the line BindAddress 127.0.0.1. For this change to take effect, you need to restart Apache by using the command /etc/init.d/apache restart. From now on, you should only be able to access Apache on this machine.

The next step is to point Apache in the direction of your web pages. By default, the location is /var/www. Although you can put your web pages in there, I find it easier to keep web pages in your Home directory so that it easier to edit. If we're going to do this, we need to change some Apache configuration files.

As root, access the file /etc/apache/httpd.conf. Find the line DocumentRoot /var/www/ (line 282 in my version of the file), and change /var/www/ to the path of your web page - this means the directory your index is stored in. If I have the file index.php in the directory /home/foobar/webediting/, I would change the line so it displays DocumentRoot /home/foobar/webediting/. You also need to change a line shortly afterwards - right now, it should display <Directory /var/www/>. Change it to match the previous line you changed. In my example, it would become <Directory /home/foobar/webediting/>. Finally, we need to restart Apache using the command /etc/init.d/apache restart. Visit localhost in your web browser, and you should see your web page in all it's glory!

Server Side Includes (SSI)

If you want to use Server Side Includes in your web page and test it using Apache, you will (or should!) find that it does not work straight away. In order to get it to work, you must enable the relevant module. Using the file browser, terminal or program of your choice, access the directory /usr/lib/apache/1.3. Here, you should see various files, including 090mod_include, or something similar. As root e.g. in a terminal, you need to type the command /usr/sbin/apache-modconf apache enable 090mod_include. If the name of the module is different on your computer, adjust the command accordingly. Restart apache using /etc/init.d/apache restart, and your server side includes should be working perfectly.

Adding a Markup Validator

If you create web pages, you should check that they are valid (X)HTML using the W3C Validator. If you're on Debian, then this is a very simple process. Simple obtain the package w3c-markup-validator e.g. by typing apt-get install w3c-markup-validatoras root in a terminal, and apt will fetch all the packages you need. Navigate, in your web browser, to localhost/w3c-markup-validator, and you should get your own personal copy of the validator.

Note: If you do not have Server Side Includes installed, you need to install them to get all pages of the Validator to display correctly. For instructions, see above.

Troubleshooting

Problem: I get a HTTP Error 403 Forbidden Page instead of my web page.

Solution: As root, access the file /var/log/apache/error.log, and try and find a line that contains something similar to (13)Permission denied: If the line continues: access to /foobar/baz.php failed because search permissions are missing on a component of the path, you need to sort out the permissions of your directories and files.

All the directories of the path need to be able to be read and executed by everyone, so the permissions need to be at least drwxr-xr-x, or 755. You can change this in Nautilus if you own the file, so that the Owner can read, write and execute, while the Group and Others can read and execute. For the file itself, everybody should be able to read i.e. at least -rw-r--r--, or 644. For example, if I get a 403 Error when accessing my page /home/foobar/webediting/index.php, I would check that the directories /home, /home/foobar, and /home/foobar/webediting have the right permissions (drwxr-xr-x), as does index.php (-rw-r--r--).

Apache 2

This page is for those of you wanting to install Apache 2. If you're not sure which version you want, then you'll probably want to install Apache 2. Since the two versions are fairly similar, many of the steps are similar, meaning most of this page is similar to the last.

First of all, you need to install Apache itself. So, as root in a terminal, type apt-get install apache2 or use synaptic, or whatever you normally use to fetch packages. Next, we want Apache to be able to generate your pages - if we want to be able to generate PHP pages, we install the package php4. We also need to make PHP4 run with Apache 2 by installing the package libapache2-mod-php4.

Now, Apache should be running - test by going into a web browser and typing in localhost as a web address. If everything's working as it should, you'll get an Apache placeholder page.

Before we start configuring Apache to load your pages, we need to make sure that Apache can only be accessed by the machine it's on, since we're only using it to test web pages. We do this by editing /etc/apache2/ports.conf. You'll need root permissions to do this, so the easiest way is to open up a terminal, become root and type nano /etc/apache2/ports.conf. Once in this file, change the first line to Listen 127.0.0.1:80. For this change to take effect, you need to restart Apache by using the command /etc/init.d/apache2 restart. From now on, you should only be able to access Apache on this machine.

The next step is to point Apache in the direction of your web pages. By default, the location is /var/www. Although you can put your web pages in there, I find it easier to keep web pages in your Home directory so that it easier to edit. If we're going to do this, we need to change some Apache configuration files.

As root, access the file /etc/apache2/sites-available/default. You should already be able to see the two lines you need to change:

DocumentRoot /var/www/ and
<Directory /var/www/>

You need these to point to the directory with your index file.If I have the file index.php in the directory /home/foobar/webediting/, I would change the lines to this:

DocumentRoot /home/foobar/webediting/ and
<Directory /home/foobar/webediting/>

You should also comment out the line starting with RedirectMatch - otherwise, Apache will always display the placeholder page whenever you try to access your own page. The line should end up like this:

#RedirectMatch ^/$ /apache2-default/

We're finished with that file, so save and exit it. Finally, we need to restart Apache using the command /etc/init.d/apache2 restart. Visit localhost in your web browser, and you should see your web page in all it's glory!

Server Side Includes (SSI)

If you want to use Server Side Includes in your web page and test it using Apache, you will (or should!) find that it does not work straight away. In order to get it to work, you must enable the relevant module. First, we need to edit a file as root. We can do this by logging in as root in a terminal, and typing nano /etc/apache2/mods-available/userdir.conf. You need to add three lines (highlighted) so that the code looks like this:

<Directory /home/*/public_html>
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
AllowOverride FileInfo AuthConfig Limit
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
Options +Includes
</Directory>

The pages with server side includes should have the extension .shtml. If you wish, you can change this to .html so that all .html files are parsed for server side includes - however, while this may make management easier, don't forget that this will slow the delivery of pages down.

Next, we need to create a link to tell Apache to actually load the module - again, as root in a terminal, type in ln -s /etc/apache2/mods-available/include.load /etc/apache2/mods-enabled/include.load.

Restart apache using /etc/init.d/apache2 restart, and your server side includes should be working perfectly.

Adding a Markup Validator

If you create web pages, you should check that they are valid (X)HTML using the W3C Validator. If you're on Debian, then this is a very simple process. Simple obtain the package w3c-markup-validator e.g. by typing apt-get install w3c-markup-validator as root in a terminal, and apt will fetch all the packages you need. Now, we need to create a symbolic link to the setup file for Apache to load the Validator. As root in a terminal, type the following: ln -s /etc/w3c/apache.conf /etc/apache2/conf.d/w3c-markup-validator.conf.

Navigate, in your web browser, to localhost/w3c-markup-validator, and you should get your own personal copy of the validator.

Note: If you do not have Server Side Includes installed, you need to install them to get all pages of the Validator to display correctly. For instructions, see above.

Troubleshooting

Problem: I get a HTTP Error 403 Forbidden Page instead of my web page.

Solution: As root, access the file /var/log/apache/error.log, and try and find a line that contains something similar to (13)Permission denied: If the line continues: access to /foobar/baz.php failed because search permissions are missing on a component of the path, you need to sort out the permissions of your directories and files.

All the directories of the path need to be able to be read and executed by everyone, so the permissions need to be at least drwxr-xr-x, or 755. You can change this in Nautilus if you own the file, so that the Owner can read, write and execute, while the Group and Others can read and execute. For the file itself, everybody should be able to read i.e. at least -rw-r--r--, or 644. For example, if I get a 403 Error when accessing my page /home/foobar/webediting/index.php, I would check that the directories /home, /home/foobar, and /home/foobar/webediting have the right permissions (drwxr-xr-x), as does index.php (-rw-r--r--).

Useful Links