CGI (Common Gateway Interface) defines how web servers interact with external content generation programs, often called CGI programs or CGI scripts. It's a simple way to put dynamic content on your website using the programming language you're most familiar with. This document will introduce how to set up CGI on the Apache web server and how to write a simple CGI program.
1. Configure Apache to allow CGI
In order for CGI programs to work properly, Apache needs to be configured to allow CGI execution. There are several ways to do this.
Method 1: ScriptAlias directive
The ScriptAlias directive tells Apache to set aside a specific directory for CGI programs. Apache will assume that every file in this directory is a CGI program and attempt to execute it when a client requests that specific resource.
ScriptAlias
The instructions are as follows:
ScriptAlias "/cgi-bin/" "/usr/local/apache2/cgi-bin/"
Shell
If Apache is installed in the default location, the examples shown can httpd.conf
be found in the default configuration file. The ScriptAlias directive is much like the Alias directive in that it defines a URL prefix that maps to a specific directory. Alias and ScriptAlias are usually used in directories outside the DocumentRoot directory. The difference between Alias and ScriptAlias is that ScriptAlias has the additional meaning that everything under that URL prefix will be treated as a CGI program. So the above example tells Apache that /cgi-bin/
any request for a resource starting with should be /usr/local/apache2/cgi-bin/
served in the directory and should be treated as a CGI program.
For example, if a URL is requested http://www.example.com/cgi-bin/test.pl
, Apache will attempt to execute the file /usr/local/apache2/cgi-bin/test.pl
and return the output. Of course, the file must exist, be executable, and return output in a specific way, otherwise Apache will return an error message.
Way 2: CGI outside the ScriptAlias directory
For security reasons, CGI programs are usually restricted to directories of ScriptAlias. This way, administrators can tightly control who is allowed to use CGI programs. However, if proper security precautions are taken, there is no reason why CGI programs cannot be run from arbitrary directories. For example, you might want to use UserDir
a directive to let users have web content in their home directory. If they want to have their own CGI program but don't have access to the home cgi-bin
directory, they need to be able to run the CGI program somewhere else.
There are two steps to allowing CGI execution in any directory. First, cgi-script
the handler must be activated using the AddHandler or SetHandler directive. Second, it must Options
be specified in the directive ExecCGI
.
Method 3: Use Options to allow CGI execution.
You can explicitly use instructions in the main server configuration file Options
to specify that CGI execution is allowed in specific directories:
<Directory "/usr/local/apache2/htdocs/somedir">
Options +ExecCGI
</Directory>
Shell
The above directive tells Apache to allow execution of CGI files. You also need to tell the server which files are CGI files. The following AddHandler
directive tells the server to treat all files with a cgi or pl extension as CGI programs:
AddHandler cgi-script .cgi .pl
Shell
Method 4: User directory
To allow CGI programs to be executed on any file ending in the user directory .cgi
, you can use the following configuration.
<Directory "/home/*/public_html">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
Shell
If you wish to specify cgi-bin
a subdirectory of your user directory where everything will be treated as a CGI program, you can use the following command.
<Directory "/home/*/public_html/cgi-bin">
Options ExecCGI
SetHandler cgi-script
</Directory>
Shell
2. Write CGI programs
There are two main differences between "regular" programming and CGI programming.
First , all output from a CGI program must MIME
begin with a type header. This is an HTTP header that tells the client what type of content it is receiving. In most cases it looks like this:
Content-type: text/html
Shell
Second , the output needs to be in HTML or another format that the browser can display. Most of the time this will be HTML, but sometimes you might write a CGI program to output gif
images or other non-HTML content.
Other than these two things, writing a CGI program looks a lot like any other program you might write.
The first CGI program
The following is a sample CGI program that outputs a line of content to the browser. Enter the following, save it to first.pl
a file named and place it cgi-bin
in the directory.
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello, World.";
Perl
Even if you are not familiar with Perl, you should be able to figure out what the program is about. The first line tells Apache (or whatever shell you are running) that /usr/bin/perl
this program can be executed by feeding the file to the interpreter at location. The second line prints the content type declaration, followed by two carriage returns and line feeds. This adds an empty 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".
If you open your browser and enter the URL address -
http://www.example.com/cgi-bin/first.pl
Shell
Or wherever you put the file, you'll see a line Hello,World
. appears in the browser window.