Your Daily Source for Apache News and Information  
Breaking News Preferences Contribute Triggers Link Us Search About
Apache Today [Your Apache News Source] To internet.com

Apache HTTPD Links
The Java Apache Project
Apache Project
The Jakarta Project
The Apache Software Foundation
Apache Module Registry
The Apache FAQ
Apache-Related Projects
Apache XML Project
PHP Server Side Scripting
ApacheCon
Apache-Perl Integration Project

  internet.com

Internet News
Internet Investing
Internet Technology
Windows Internet Tech.
Linux/Open Source
Web Developer
ECommerce/Marketing
ISP Resources
ASP Resources
Wireless Internet
Downloads
Internet Resources
Internet Lists
International
EarthWeb
Career Resources

Search internet.com
Advertising Info
Corporate Info
Apache Guide: Dynamic Content with CGI
Jun 5, 2000, 16 :25 UTC (39 Talkback[s]) (37958 reads) (Other stories by Rich Bowen)

With this column Rich Bowen begins his weekly look at basic tasks that every Apache Webmaster must face. Rich is the author of Apache Server Unleashed.

The CGI (Common Gateway Interface) is the simplest, and most common, way to put dynamic content on your web site. This week's column will be an introduction to setting up CGI on your Apache Web server and getting started writing CGI programs.

Configuring Apache to Permit CGI

In order to get your CGI programs to work properly, you'll need to have Apache configured to permit CGI execution. There are several ways to do this.

ScriptAlias

The ScriptAlias directive tells Apache that a particular directory is set aside for CGI programs. Apache will assume that every file in this directory is a CGI program and will attempt to execute it, when that particular resource is requested by a client.

The ScriptAlias direcive looks like:

        ScriptAlias /cgi-bin/ /usr/local/apache/cgi-bin/

The example shown is from your default httpd.conf configuration file, if you installed Apache in the default location. The ScriptAlias directive is much like the Alias directive, which defines a URL prefix that is to mapped to a particular directory. Alias and ScriptAlias are usually used for directories that are outside of the DocumentRoot directory. The difference between Alias and ScriptAlias is that ScriptAlias has the added meaning that everything under that URL prefix will be considered a CGI program. So, the example above tells Apache that any request for a resource beginning with /cgi-bin/ should be served from the directory /usr/local/apache/cgi-bin/, and should be treated as a CGI program.

For example, if the URL http://dev.rcbowen.com/cgi-bin/test.pl is requested, Apache will attempt to execute the file /usr/local/apache/cgi-bin/test.pl and return the output. Of course, the file will have to exist, and be executable, and return output in a particular way, or Apache will return an error message.

CGI Outside of ScriptAlias Directories

Occasionally you will want to have CGI programs outside of ScriptAlias'ed directories. Usually, this will be for the purpose of letting users have web content in their home directories with the UserDir directive. If they want to have their own CGI programs, but don't have access to the main cgi-bin directory, they will need to be able to run CGI programs elsewhere.

.htaccess Files

A .htaccess file is a way to set configuration directives on a per-directory basis. When Apache serves a resource, it looks in the directory from which it is serving a file for a file called .htaccess, and, if it finds it, it will apply directives found therein. .htaccess files can be permitted with the AllowOverride directive, which specifies what types of directives can appear in these files, or if they are not allowed at all. To permit the directive we will need for this purpose, the following configuration will be needed:

        AllowOverride Options

In the .htaccess file, you'll need the following directive:

        Options +ExecCGI

which tells Apache that execution of CGI programs is permitted in this directory.

Explicitly using Options to Permit CGI Execution

You could explicitly use the Options directive, inside your main server configuration file, to specify that CGI execution was permitted in a particular directory:

        <Directory /usr/local/apache/htdocs/somedir>
                Options +ExecCGI
        </Directory>

You might do this if, for some reason, you really wanted to serve CGI out of a document directory. The ScriptAlias directive is a combination of an Alias directive and an Options +ExecCGI directive, so this is just a ScriptAlias directive without the Alias part.

Writing a CGI Program

There are two main differences between regular programming, and CGI programming.

First, all output from your CGI program must be preceeded by a MIME-type header. This is HTTP header that tells the client what sort of content it is receiving. Most of the time, this will look like:

        Content-type: text/html

Secondly, your output needs to be in HTML, or some other format that a browser will be able to display. Most of the time, this will be HTML, but occasionally you might write a CGI program that outputs a gif image, or other non-HTML content.

Apart from those two things, writing a CGI program will look a lot like any other program that you might write.

Your First CGI Program

The following is an example CGI program that prints one line to your browser. Type in the following, save it to a file called first.pl, and put it in your cgi-bin directory.

        #!/usr/bin/perl
        print "Content-type: text/html\r\n\r\n";
        print "Hello, World.";

Even if you are not familiar with Perl, you should be able to see what is happening here. The first line tells Apache (or whatever shell you happen to be running under) that this program can be executed by feeding the file to the interpreter found at the location /usr/bin/perl. The second line prints the content-type declaration we talked about, followed by two carriage-return newline pairs. This puts a blank line after the header, to indicate the end of the HTTP headers, and the beginning of the body. The third line prints the string ``Hello, World.'' And that's the end of it.

If you open your favorite browser and tell it to get the address

        http://your.server.com/cgi-bin/first.pl

or wherever you put your file, you will see the one line Hello, World. appear in your browser window. It's not very exciting, but once you get that working, you'll have a good chance of getting just about anything working.

But it's Still Not Working!

If your program still is not working, here are some of the things that you need to look for in order to resolve your problem.

File Permissions

Remember that the server does not run as you. That is, when the server starts up, it is running with the permissions of an unpriveleged user--usually nobody or www--and so it will need extra permissions to execute files that are owned by you. Usually, the way to give a file sufficient permissions to be executed by nobody is to give everyone execute permission on the file:

        chmod a+x first.pl

Also, if your program reads from, or writes to, any other files, those files will need to have the correct permissions to permit this.

Path Information

When you run a program from your command line, you have certain information that is passed to the shell without you thinking about it. For example, you have a path, which tells the shell where it can look for files that you reference.

When a program runs through the web server as a CGI program, it does not have that path. Any programs that you invoke in your CGI program (like sendmail, for example) will need to be specified by a full path, so that the shell can find them when it attempts to execute your CGI program.

Syntax Errors

Most of the time when a CGI program fails, it's because of a problem with the program itself. This is particularly true once you get the hang of this CGI stuff, and no longer make the above two mistakes. Always attempt to run your program from the command line before you test if via a browser. This will elimate most of your problems, and will prevent having to look through cryptic error logs.

For More Information

There are a large number of CGI resources on the Web. You can discuss CGI problems with other users on the Usenet group comp.infosystems.www.authoring.cgi. And the -servers mailing list from the HTML Writers Guild is a great source of answers to your questions. You can find out more at http://www.hwg.org/lists/hwg-servers/.

When you post a question about a CGI problem that you're having, whether to a mailing list, or to a newsgroup, make sure you provide enough information about what happened, what you expected to happen, and how what actually happened was different, what server you're running, what language your CGI program was in, and, if possible, the offending code. This will make finding your problem much simpler.

Rich Bowen is the Director of Web Application Development at The Creative Group and the author of Apache Server Unleashed.

  Current Newswire:
Netcraft Web Server Survey for November is available

FoxServ 2.0 Released

Ace's Hardware: Building a Better Webserver in the 21st Century

Web Techniques: Customer Number One

Apache-Frontpage RPM project updated

CNet: Open-source approach fades in tough times

NewsForge: VA spin-off releases first product, aims for profit

Apache 2.0.28 Released as Beta

Covalent Technologies announces industry support for Enterprise Ready Server and Apache 2.0

developer.com: On the Security of PHP, Part 1

 Talkback(s) Name  Date
Hi. I&#96;m writing from Argentine. I am a wemaster of very famous website here. ...   Consulting   
  Jun 6, 2000, 14:06:53
Although this article was very nicely written, It lacked content, and was mislea ...   A little lean.   
  Jun 9, 2000, 19:46:08
Thanks for the feedback. I&#39;ll do a series on CGI in a few weeks. Is there an ...   Re: A little lean.   
  Jun 12, 2000, 19:36:31
I also thought the article was a bit short.A few topics that would like to see:- ...   Re: Re: A little lean.   
  Jun 17, 2000, 18:32:16
I had the same thing on my BSD box. You will need to install Apache and disable ...   Re: Consulting   
  Jun 21, 2000, 03:15:36
I couldn&#39;t find the subsequent articles on CGI so I&#39;ll present a questio ...   more articles?   
  Aug 23, 2000, 22:17:55
hi...,I&#39;ve installed apache web server and I want to add a perl under the ap ...   perl under apache web server   
  Sep 16, 2000, 21:20:31
Necesito saber si puedo correr aplicaciones basadas en ASP con el servidor Apach ...   Sirve Apache para ASP ?   
  Oct 5, 2000, 19:59:55
Hi, I have installed apache server and i want to wirte CGI using C-language, ...   C under apache web server   
  Oct 26, 2000, 19:20:40
Check file execute permission.Next, start output with:printf("Content-type: text ...   CGI/C   
  Oct 27, 2000, 13:14:29
Hi could anyone tell me how to set up apache.I have a login.exe that is made in ...   apache setup for sgi   
  Nov 23, 2000, 10:17:30
HiI have an Apache installation (1.3.12) that came with IndigoPerl running under ...   Apache and CGI   
  Dec 30, 2000, 02:35:33
como configuro mi servidor para ejecutar Servlets ...   pregunta   
  Jan 12, 2001, 19:09:37
hai there ,till now i searched how to run a servlet on Apachi webserver in this ...   didn't get servlet out put how?   
  Feb 2, 2001, 12:44:33
 I want to learn CGI Programming, but my OS is Windows 98 and I have a Personal ...   Cgi and Windows 98?   
  Feb 6, 2001, 17:56:19
Hii got one problem during setting up my web page in intranet(local Lan - 3 comp ...   Premature end of the script header????   
  Feb 24, 2001, 03:08:09
Hasta donde yo sé ASP es propietaria de Microsoft. Además se basa en VBScript ra ...   Re: Sirve Apache para ASP ?   
  Mar 6, 2001, 00:52:30
I have the same problem. When I click submit on my form it says...Netsape errorN ...   I have the same problem. PLEASE HELP!!!   
  Mar 6, 2001, 08:17:12
X is probably using port 80.Change the server you don&#39;t wish to use to 81 an ...   Re: Consulting   
  Mar 14, 2001, 20:14:53
hi download and install the apache web-server and configure it running on port81 ...   Re: Cgi and Windows 98?   
  Mar 19, 2001, 16:45:04
access.log --l71014668.msd.ray.com - - [26/Mar/2001:08:05:09 -0500] "GET /epats/ ...   CGI outputing errors that cause cgi process to queue up and then fail   
  Mar 26, 2001, 15:40:47
When I attempt to post html->CGI I get an APACHE error...&#39;The requested meth ...   CGI posting   
  Mar 27, 2001, 20:47:12
Bonjour, essaye ça !( Hello, try it ! )http://homepages.paradise.net.nz/milhous/ ...   Re: Cgi and Windows 98?   
  Apr 20, 2001, 18:21:49
Please help:I&#39;m using apache on windows 2000. I cannot execute any cgi appli ...   windows 2000, apache cgi file permission   
  Apr 29, 2001, 22:43:09
i have intalled apache on windows 98. the cgi does not work. please let me know ...   windows 98 and cgi   
  May 10, 2001, 20:06:16
OK now I know this sounds lame BUT. I running RedHat7 with Apache. Ive gotten it ...   UMMMM It does not work.   
  May 21, 2001, 22:54:55
When I use the IIS www server, we can insert the program modules using This not ...   *** NO SUBJECT ***   
  Jun 2, 2001, 12:21:41
How work SSI from CGI? (Linux)SSI code isn&#39;t executed.It&#39;s ignored.My Be ...   SSI from CGI   
  Jun 8, 2001, 09:34:14
HiI&#39;m new with Apache server.I need to keep track of some user choises in di ...   How to implement session concept for Appache?   
  Jul 17, 2001, 09:01:23
> How work SSI from CGI? (Linux)SSI code isn&#39;t executed.It&#39;s ignored.My ...   Re: SSI from CGI   
  Aug 6, 2001, 07:06:22
I got a small PERL CGI script and if I try to run this my browser always tells m ...   my CGI won't work   
  Aug 16, 2001, 12:27:39
I have the same problem .I was on Windows 2000 professional.I followed the instr ...   Re: my CGI won't work   
  Aug 25, 2001, 23:19:42
I was having the same problem. Couldn&#39;t get ANY Perl scripts to run under Wi ...   Re: Re: my CGI won't work   
  Aug 30, 2001, 13:09:18
Hello all!I have to configure my Apache on Win32 to run Win32 console executable ...   Config Apache/Win32 for running .EXE files as CGI from /cgi-bin/   
  Sep 18, 2001, 07:59:34
I have active Perl installed (5.6) and still get the "could not spawn child proc ...   Re: Re: Re: my CGI won't work   
  Nov 8, 2001, 17:48:19
I am running on a Windows NT Server with Apache 1.3.2. I am currently porting a ...   Weird CGI Problem (POST vs GET)   
  Nov 9, 2001, 01:32:56
hello,I have a strange problem with my script...When I run my CGI Perl script... ...   Apache returns "file not file" when the file is there   
  Nov 28, 2001, 00:47:41
you must include this Top Of Program printf("Content-type: text/html\r\n\r\n");T ...   Re: C under apache web server   
  Nov 28, 2001, 03:42:58
I am having the same problem with the same error message and am wondering if you ...   Re: CGI posting   
  Nov 28, 2001, 23:25:17
Enter your comments below.
Your Name: Your Email Address:


Subject: CC: [will also send this talkback to an E-Mail address]
Comments:

See our talkback-policy for or guidelines on talkback content.

About Triggers Media Kit Security Triggers Login


All times are recorded in UTC.
Linux is a trademark of Linus Torvalds.
Powered by Linux 2.4, Apache 1.3, and PHP 4
Copyright INT Media Group, Incorporated All Rights Reserved.
Legal Notices,  Licensing, Reprints, & Permissions,  Privacy Policy.
http://www.internet.com/