Apache Guide: Dynamic Content with CGI Page 2
Optionsdirective, 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
Alias directive and an
Options +ExecCGI directive, so this is
ScriptAlias directive without the
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:
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
#!/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
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.
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
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
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.
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.
IT Solutions Builder TOP IT RESOURCES TO MOVE YOUR BUSINESS FORWARD
Which topic are you interested in?
What is your company size?
What is your job title?
What is your job function?
Searching our resource database to find your matches...