Apache/Perl/DBI/CGI Programming FAQ


Getting started

To set up Apache, log into rack40 and run ~cps116/bin/setup-apache.sh. This script will install Apache 2.0.40 in ~/apache2 and automatically configure it to use Perl CGI and our DB2 installation. At the end of the installation process, a port number on rack40 will be assigned to you for running your Web server. Please take a note of this number, as you will need it to access Web site. If you forget this number, you can look in ~/apache2/conf/httpd.conf for the value of the Listen parameter. We ask you not to change the port number as it may conflict with your classmates' Web servers.

To start the Web server, use ~/apache2/bin/apachectl start. The URL of your Web server is http://rack40.cs.duke.edu:<port>/, where <port> is the port number you got at the end of the installation script. To stop the Web server, use ~/apache2/bin/apachectl stop. Complete Apache 2.0 documentation is available from the Apache Software Foundation.

You should put all static Web pages (e.g., HTML files) in the directory ~/apache2/htdocs/ and all CGI scripts in directory ~/apache2/cgi-bin/. Make sure you use the correct file permissions: chmod a+r <file> for static Web pages, and chmod a+rx <file> for CGI scripts. One the most common CGI errors is caused by non-executable CGI scripts. The file ~/apache2/logs/error_log records all server errors and should be especially useful in debugging your CGI scripts.

There is an example Web site for the beer drinkers' database, built using Perl CGI. To set up the example Web site, you need to create the beer drinkers' database first by running ~cps116/examples/db-beers/setup.sh. Next, run ~cps116/examples/perl/setup.sh to copy HTML and CGI files for the Web site. You can then access the beer drinker's database at http://rack40.cs.duke.edu:<port>/db-beers/.

Perl basics

The complete Perl documentation is available online. You may also find the CGI examples in ~cps116/examples/perl/cgi-bin/db-beers/ useful.

Perl DBI basics

Perl DBI is a database interface module for Perl. You can find its manual page and a gentler introduction online. A comprehensive collection of DBI documentation is also available.

You will find examples of using Perl DBI with DB2 in ~cps116/examples/perl/cgi-bin/db-beers/. The connection string for DB2 is 'dbi:DB2:cps116', where cps116 is the name of the database you normally connect to. Start with the file all-drinkers.pl, which illustrates how to handle errors, connect and disconnect the database, prepare and execute SQL queries, and retrieving results of the queries. The file edit-drinker.pl illustrates more powerful methods for querying the database. Finally, the file update-drinker.pl illustrates how to update the database and use its transaction processing features.

Perl CGI basics

The CGI concept is very simple: Instead of serving a static HTML page, the Web server runs a program (e.g., a C program or a Perl script) to generate the HTML output to serve to the client. You can find documentation and tutorial for CGI online.

You will find examples of using Perl for CGI programming in ~cps116/examples/perl/cgi-bin/db-beers/. The file all-drinkers.pl illustrates how to use bare-bone Perl to generate HTML output. Generating HTML output by hand is both tedious and error-prone. The other files in the directory use a convenient Perl module called CGI, which simplifies HTML generation a great deal. You can find its manual page online.