An easy way to configure a webserver with Debian GNU/Linux

This informal document reflects my opinion on how to setup a webserver that is easy to maintain and uses some of my favorite software.

I am about to install Apache, PHP, and MySQL on a new server. I am also going to include PHP's LDAP and GD modules. The LDAP module will allow my webserver to connect to the Rugters Directory Service, which is useful for retrieving adminstrative data. The GD module will allow my PHP scripts to create images dynamically. I am also going to install an implementation of JavaServer Pages just for fun to try it out with Debian. I actually think that PHP is much better than JSP and mixing both on the same server is a little odd (so you might want to skip this step).

I recomend the Debian GNU/Linux Operating System, particularly for this case because of its package management tools. If you would prefer to use the RPM package managment system there are similar apt utitlies avaialable. However, the package names won't be directly mapable.

Why am I using a Package Manager?

It is a good experience to install packages from source (downloading the source tree tarball and doing a make && make install), but there are problems with doing things that way.

The traditional way to install software on a Unix system is to download the source code, compile it, and then install it on your system. Installing software in this manner can be a good learning experience, but it introduces the following problems:

  1. Dependencies are not managed. For example a piece of software that you are trying to install could need another piece software, and in turn that piece of software could have the same problem.
  2. There is no clean way to uninstall the software. For example, installing your software could have put files in lots of places on your system, and it could be tricky to track down and remove those files.
  3. There is no consistent interface for installing the software. A lot of source based software packages have a consistent method of installation, but this is not necessarily the case, and you might have to take time to learn the installation process behind each piece of software that you are interested in.
  4. There is no automatic way to keep track of what software has been installed on your system.

One way to avoid the above problems is to use a package manager. A package is a collection of software, which usually contains all of the files necessary to implement a set of related commands or features. For example, if you want to install the apache web server on your system and if you are using a package manager, you would want to find the 'apache web server' package and then tell your package manager to install it, as opposed to downloading the source code, compiling it, and installing it.

What about using RPMs?

There are a variety of package managers available that run on Unix flavored operating systems, but not all of them solve the above problems. For example the RPM package manger manages RPM packages, but it does not solve the dependency problem. Thus, the process for installing a package called x.rpm could involve downloading x.rpm and attempting to install x.rpm only to find out that it needs a file called k.c. In order to figure out which package has the file called k.c, you might have to use something like rpm find to figure out that the file that you are interested in is contained in a package called y.rpm. You could spend a lot of time doing this, because y.rpm might have the same problem.

One package management tool that solves the dependency problem nicely is called apt-get, which was developed for the Debian GNU/Linux operating system. Debian has a tool called dpkg which is similar to RPM, in that it can be used to install binary ".deb" packages (as opposed to ".rpm" packages). However, Debian's package manager has a higher level command called apt-get, which manages dependencies based on packages, not files.

Some have said that apt-get is the greatest thing since sliced bread! If you want to install a package on a Debian system all you have to do (assuming that things are configured correctly), is figure out the package's name (this could be done using Debian's website) and type apt-get install x (where x is the package name). Apt will fetch the package (from a CD-ROM or across the internet over http or ftp, so that no special protocol is needed) as well as the packages that it depends on and make it work. In addition to solving all four of the above problems, apt can be used to upgrade all of your packages to the latest version with one command (apt-get upgrade).

This document will focus on using Debian packages, so that things are handled smoothly, and so that the system can be upgraded with one command. However, setting up this webserver won't be too easy, so don't worry, there will plenty of time for editing configuration files by hand.

Debian is especially easy to install in this case, since a webserver does not need X. I am running the stable release of Debian since a web server does not need to be on the cutting edge with regard to its packages, instead it should be a reliable as possible. These instructions are to be followed after you have Debian installed. The entire webserver will only take up less than 150 MB (this indcludes the entire Debian GNU/Linux base operating system and all of the pakcages (with the exception of Java) whose installation I am documenting.

Enough Already, get on with it...

I am going to use the following Debian Packages:

  1. mysql-server
  2. apache
  3. php4
  4. php4-mysql-mod
  5. php4-ldap
  6. php4-gd
  7. jserv
  8. gnujsp

Here is the process:

  1. As root execute the command apt-get install mysql-server. The above should have installed the MySQL server on your system. It is important that you set a MySQL client password next. To do this:
    • Start a MySQL client with the command mysql -u root. This should give you the 'mysql>' prompt.
    • Execute the following command at the 'mysql>' prompt, such that the string 'new_password' is the password that you want to set:
      	
      	SET PASSWORD FOR root@localhost=PASSWORD('new_password');
              
    • As you create databases that you want to be manipulated via PHP scripts you will need to grant MySQL privileges. In general this will involve typing something along the lines of the following at the 'mysql>' prompt:

      • create database x;
      • use x;
      • grant select, insert, update, delete on x.* to 'www-data'@localhost;

      The above grants a lot of privileges liberally. You should try to be as conservative as possible given what the application needs. 'www-data' is the default apache daemon (instead of nobody) by Debian's apache configuration. Note the use of the quotes. Consult the MySQL Documentation to learn more.

    Apt should have also put symbolic links into the apprpriate /etc/rc* directory so that service will start and stop as the machine goes up and down.
  2. As root execute the command apt-get install apache. You can test the success of this by pointing a browser at 'yourhostname.yourdomain', where you should see the appropriate welcome page. Again, apt should have also put symbolic links into the apprpriate /etc/rc* directory so that service will start and stop as the machine goes up and down.
  3. As root execute the command apt-get install php4.
    • You will be prompted with a question about adding the following line:
        LoadModule php4_module /usr/lib/apache/1.3/libphp4.so
        to your /etc/apache/httpd.conf file.  Type y as in 'yes'.  
        
    • When you are returned to the root prompt, open your /etc/apache/httpd.conf file with your favorite editor and find the "LoadModule" line from above. If it is commented, remove the # symbol so that it will be executed.
    • You can make additonal configurations to your /etc/apache/srm.conf file according to your preferences. One thing that I like to do is to change this line:

      DirectoryIndex index.html

      to this:

      DirectoryIndex index.html index.php

      Which allows index.php to executed like index.html files.

      By default Debian should have given your users to ability to publish home pages with a public_html directory. You can verify this by seeing that the this file should contain this line:

      UserDir public_html

    • Restart apache with the command apachectl restart.

    • You can test the success of this by creating a tiny PHP script. I recomend that you create a file in /var/www/ called info.php whose content is the following line:

        <? phpinfo(); ?>
        
      and point your browser to 'yourhostname.yourdomain/info.php'. The phpinfo function should output information about your PHP configuration.
    • You could also become a normal user and create a public_html directory and see if an index.php file is able to be loaded by default.

  4. As root execute the command apt-get install php4-mysql. When asked if you want apt to update your /etc/php4/apache/php.ini file so that it has the line extension=mysql.so type y as in 'yes' and restart apache with the command apachectl restart. This is the module that allows PHP to talk to MySQL. To test to see if it worked you will have to create a MySQL database and a PHP script which attempts to talk to it. Don't forget to "grant privs to 'www-data'@localhost" when trying to use PHP/MySQL.
  5. As root execute the command apt-get install php4-ldap.

    When asked if you want apt to update your /etc/php4/apache/php.ini file so that it has the line extension=ldap.so type y as in 'yes' and restart apache with the command apachectl restart.

    To test if you LDAP module was installed corectly use this test code (Example 1 from the PHP Manual on LDAP functions). If you get an error message that says "Unable to connect to LDAP server" (assuming you are not running an LDAP server since it targets localhost), then it should be working. If you get an error message that says something along the lines of "Call to undefined function: ldap_connect()" then something is wrong with your PHP LDAP module.

  6. As root execute the command apt-get install php4-gd.

    When asked if you want apt to update your /etc/php4/apache/php.ini file so that it has the line extension=gd.so type y as in 'yes' and restart apache with the command apachectl restart.

    You might want to stop here. I am only installing jserv to show how it might be done. Running both PHP and JSP on the same machine might not be necessary.

  7. As root execute the command apt-get install jserv.

    As apt finishes its installation you should see the following:

    Setting up jserv (1.1-3) ...
    Starting JServ servlet engine: jserv.
    done.
    Reloading apache configuration.
    /usr/sbin/apachectl graceful: httpd gracefully restarted
    

    By default, the servlet repository should be in /usr/share/java/servlets and should include two .class files:

    $ ls -l
    total 8
    -rw-r--r--    1 root     root         1132 May 10  2000 Hello.class
    -rw-r--r--    1 root     root         2119 May 10  2000 IsItWorking.class
    $ 
    
    To get a feel for how to use servlets with HTML and test that JServ is working with the above files you can create a file that contains this code, which contains this:
    <form method=get action="/servlets/Hello">
    

    The above line of HTML causes the file Hello.class to be executed. The code should point your broswer at a page called 'yourhostname.yourdomain/servlets/Hello?' where you should see the output of Hello.class.

    The configuration files for the servlet engine can be found inside of /etc/jserv/. You can find information on how to change the configuration of the servlet engine here You can also find links on to use servlets here.

  8. As root execute the command apt-get install gnujsp.

    As apt finishes its installation you should see the following:

    Setting up gnujsp (1.0.0-5) ...
    Stopping JServ servlet engine: jserv.
    Starting JServ servlet engine: jserv.
    Reloading apache configuration.
    /usr/sbin/apachectl graceful: httpd gracefully restarted
    

    A directory called /usr/share/doc/gnujsp will be created which will contain a directory called examples. To test that JSP is working you can copy the examples to your own public_html directory:

    cp -r /usr/share/doc/gnujsp/examples/ ~/public_html/jsp/
    
    and point your browser at 'yourhostname.yourdomain/~username/jsp/' to run them.

    For more information you can see some JSP tutorials and the GNUJSP website is here.