ServersApache Guide: Dynamic Content with CGI Page 2

Apache Guide: Dynamic Content with CGI Page 2

ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.




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

        
                Options +ExecCGI
        

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/htmlrnrn";

        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.

Get the Free Newsletter!

Subscribe to Daily Tech Insider for top news, trends & analysis

Latest Posts

Related Stories