Apache and CGI hints

PHW03 is already released. You can access a sample from Fall 2003 by the link "PHW03 Sample" in TA's Corner. You will encounter a lot of technical problems in this homework. Here are some hints for you:

Setting up Apache web server

  • How should I begin?
  • Copy the whole directory tree /afs/csic.umd.edu/data/http to your home directory and then build your server there. You should first modify the file conf/httpd.conf to make the httpd works.
  • conf/httpd.conf
  • The httpd.conf file is very long (>1000 lines) because Apache has a lot of features. Your first objective is to setup your server, such that it can display some static contents (e.g. some html files). Read Apache HTTP Server Version 2.0 Documentation in http://httpd.apache.org/docs-2.0/configuring.html
  • User/Group
  • In lines 251 and 252, you can see User derek and Group derek. Replace derek by your username.
  • Edit path
  • You should search all the paths begin with /afs/csic/data/http/... and replace them to the paths to your httpd directory. Use the command pwd to check the full paths.
  • Choose a port number
  • The Listen directive instructs Apache to listen to only specific IP addresses or ports. So, setting it to 'Listen 80' results the server listen to the port 80. You should choose some port number n with 5000 < n < 65535. Try some large and random n. See http://httpd.apache.org/docs-2.0/mod/mpm_common.html#listen
  • What are the commands to start and stop httpd?
  • For starting,

    /usr/sbin/httpd -f /path/to/your/httpd.conf

    For stopping,

    /usr/sbin/httpd -f /path/to/your/httpd.conf -k stop

  • How to check the httpd processes?
  • Use the command

    ps -aux | grep `whoami`

    You will see several httpd processes when your httpd is running. If you still see some httpd processes. Kill it by the command

    kill PID

    where PID is the second column shown in the ps command. Remember to kill all httpd processes each time before you logout.
  • What is the URL for your web server?
  • If you start the httpd in bilious (one of the machines in linuxlab), the URL is

    http://bilious.csic.umd.edu:PORT_NUMBER/

    where PORT_NUMBER is the port you using. Try to work your project on the same machine everytime. You can connect to a machine in linuxlab directly by

    ssh MACHINE_NAME.csic.umd.edu


    CGI programming

  • CGI programming language
  • You should think about which language would you use to implement phw03. You may use C, C++, Perl, PHP, etc. If you don't have any ideal, I suggest you to try Perl.
  • Perl
  • For using Perl, you should add

    LoadModule perl_module modules/mod_perl.so

    to httpd.conf.
  • W3C Validation
  • It may be troublesome for you to write all the detail in a html file to meet W3C's requirements. However, it is relatively easy to do it by using build-in library. e.g. In Perl, there is a modules for cgi. You can call the functions inside to generate all the web content (tables, forms, etc.)
  • Design issue
  • Try to have some designs before you write the codes. All commands have common parts, e.g. html header, input form, output, etc. Write some classes or functions instead of puting all codes in every command. In last year, I saw a lot of students kept using same codes for each command. Why don't write a function?
  • cgi path
  • In httpd.conf, you should edit the line for ScriptAlias

    ScriptAlias /cgi-bin/ "/path/to/your/http/cgi-bin/"

    The /path/to/your/http/cgi-bin/ is for the executable files while /path/to/your/http/www/ is for html files. So, don't put cgi-bin under www. Then, edit the corresponding directory path

    <Directory "/path/to/your/http/cgi-bin/">

  • AddHandler
  • In httpd.conf, unmark the comment

    AddHandler cgi-script cgi

    You may want to add more extensions, e.g. pl.
  • Testing
  • You can create a script file /path/to/your/http/cgi-bin/time.cgi
    *** the content in time.cgi ***
    #!/bin/sh
    echo "Content-type: text/html"
    echo
    echo '<h1>Current server time is</h1>'
    date
    ******* end of time.cgi *******
    
    You should add executable permission to time.cgi. Then, check the URL

    http://HOSTNAME.csic.umd.edu:PORT/cgi-bin/time.cgi

    The output should be as same as the output of my server

    http://squeamish.csic.umd.edu:18099/cgi-bin/time.cgi

  • How to store username and password?
  • It can be done by several ways:

    Miscellaneous hints

  • Reuse PHW02
  • Try to use your PHW02 as a command. Your PHW03 only need to
    1. retrieve user input from web pages.
    2. convert the input to PHW02 input format.
    3. run your PHW02.
    4. convert the output to web pages.
  • Perl CGI module
  • For Perl CGI module, I posted a Perl CGI example in TA's corner. Please read the detail in

    http://www.perldoc.com/perl5.6/lib/CGI.html

  • How to set port number dynamically?
  • You can generate httpd.conf by the startserver script. For example, you can do it by following steps:
    1. rename httpd.conf to my.conf.
    2. In my.conf, modify the Listen directive to

      Listen MY_SERVER_PORT

    3. In startserver, replace MY_SERVER_PORT by the actual port number (e.g. 18099). You can do it by the command sed.

      cat my.conf | sed -e s/MY_SERVER_PORT/18099/g > httpd.conf

    4. Run httpd as usual
  • Username & httpd path in httpd.conf
  • We also have to set the username and httpd path dynamically. You can do it by the command sed. In my.conf, you may use MY_USERNAME for username and MY_HTTPD_ROOT for httpd path. Then, you can replace everything in startserver script by:
      cat my.conf | sed -e s/MY_USERNAME/`whoami`/g
                  | sed -e s/MY_SERVER_PORT/18099/g
                  | sed -e s\%MY_HTTPD_ROOT%`pwd`%g > httpd.conf
    
    The command above must be in one line. As you know, you can get current directory by pwd and current username by whoami.
  • hostname
  • You can get system's host name by the command hostname.
  • Final testing
  • Supposed you are going to submit foo-bar.tar.gz. You should try the following:
    1. create a tmp directory
    2. copy the gz file to the tmp directory
    3. assumed you are in tmp directory, unzip and untar foo-bar.tar.gz
    4. cd phw03
    5. make
    6. ./startserver 23564
    7. copy the URL output by startserver to a browser
    8. test your web pages
    9. ./stopserver
    I will grade your program by the steps above.