Apache HTTP Server Tutorial: Dynamic Content with CGI - Apache HTTP Server Version 2.4
Modules | Directives | FAQ | Glossary | Sitemap | Report a bug Apache HTTP Server Version 2.4 Apache > HTTP Server > Documentation > Version 2.4 > How-To / Tutorials Apache HTTP Server Tutorial: Dynamic Content with CGI Available Languages: en | es | fr | ja | ko Introduction Configuring httpd to permit CGI Writing a CGI program But it's still not working! What's going on behind the scenes? CGI modules/libraries For more information Introduction ¶ Related Modules Related Directives mod_alias mod_cgi mod_cgid AddHandler Options ScriptAlias The CGI (Common Gateway Interface) defines a way for a web server to interact with external content-generating programs, which are often referred to as CGI programs or CGI scripts.
It is a simple way to put dynamic content on your web site, using whatever programming language you're most familiar with. This document will be an introduction to setting up CGI on your httpd web server, and getting started writing CGI programs. Configuring httpd to permit CGI ¶ Your httpd configuration must permit CGI execution before CGI programs will work. Several ways to do this are described below. CGI support is provided by two modules: mod_cgid and mod_cgi . mod_cgid uses a dedicated external daemon to manage CGI processes and is required when httpd runs a threaded MPM (such as event or worker ). mod_cgi runs CGI programs directly from within the server process and is used with non-threaded MPMs like prefork , or on Windows.
From a configuration standpoint they are interchangeable — the directives are the same. See the mod_cgi and mod_cgid reference pages for implementation details. If httpd has been built with shared module support, you need to ensure that the appropriate module is loaded. In your httpd.conf you need to make sure the LoadModule directive has not been commented out. For a threaded MPM: LoadModule cgid_module modules/mod_cgid.so For Windows, or a non-threaded MPM like prefork: LoadModule cgi_module modules/mod_cgi.so ScriptAlias The ScriptAlias directive tells httpd that a particular directory is set aside for CGI programs. httpd 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 directive looks like: ScriptAlias "/cgi-bin/" "/usr/local/apache2/cgi-bin/" The example shown is from your default httpd.conf configuration file, if you installed httpd 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 httpd that any request for a resource beginning with /cgi-bin/ should be served from the directory /usr/local/apache2/cgi-bin/ , and should be treated as a CGI program. For example, if the URL http://www.example.com/cgi-bin/test.py is requested, httpd will attempt to execute the file /usr/local/apache2/cgi-bin/test.py and return the output.
The file must exist, be executable, and produce output in the expected format; otherwise httpd returns an error. CGI outside of ScriptAlias directories CGI programs are often restricted to ScriptAlias 'ed directories for security reasons. In this way, administrators can tightly control who is allowed to use CGI programs. However, if the proper security precautions are taken, there is no reason why CGI programs cannot be run from arbitrary directories.
For example, you may wish to let 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. Allowing CGI execution in an arbitrary directory requires two steps. First, the cgi-script handler must be activated using the AddHandler or SetHandler directive.
Second, ExecCGI must be specified in the Options directive. 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/apache2/htdocs/somedir"> Options +ExecCGI </Directory> The above directive tells httpd to permit the execution of CGI files. You will also need to tell the server what files are CGI files.
The following AddHandler directive tells the server to treat all files with the cgi or py extension as CGI programs: AddHandler cgi-script .cgi .py .htaccess files The .htaccess tutorial shows how to activate CGI programs if you do not have access to httpd.conf . User Directories To allow CGI program execution for any file ending in .cgi in users' directories, you can use the following configuration. <Directory "/home/*/public_html"> Options +ExecCGI AddHandler cgi-script .cgi </Directory> To designate a cgi-bin subdirectory of a user's directory where everything will be treated as a CGI program, you can use the following. <Directory "/home/*/public_html/cgi-bin"> Options ExecCGI SetHandler cgi-script </Directory> Writing a CGI program ¶ CGI programming differs from regular programming in two ways.
First, all output from your CGI program must be preceded 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.py , and put it in your cgi-bin directory. #!/usr/bin/env python3 print("Content-type: text/html\n") print("Hello, World.") The first line tells the operating system which interpreter to use.
The first print call outputs the content-type header followed by a blank line (the \n in the string plus the newline that print() adds), which marks the end of HTTP headers. The second print call outputs the body. That is all a CGI program needs to produce a response. If you open your favorite browser and tell it to get the address http://www.example.com/cgi-bin/first.py 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 about anything working. But it's still not working! ¶ Four basic things may appear in your browser when you try to access your CGI program from the web: The output of your CGI program Great! That means everything worked fine. If the output is correct, but the browser is not processing it correctly, make sure you have the correct Content-Type set in your CGI program.
The source code of your CGI program or a "POST Method Not Allowed" message That means that you have not properly configured httpd to process your CGI program. Reread the section on configuring httpd and try to find what you missed. A message starting with "Forbidden" That means that there is a permissions problem. Check the httpd error log and the section below on file permissions . A message saying "Internal Server Error" If you check the httpd error log , you will probably find that it says "Premature end of script headers", possibly along with an error message generated by your CGI program.
In this case, you will want to check each of the below sections to see what might be preventing your CGI program from emitting
The rest of this page is on the live site.