Configure Apache to run CGI programs can be divided into two cases, one is the Scriptalias directory of CGI, and the second is scriptalias outside the directory of CGI.
CGI for the Scriptalias directory
The SCRIPTALIAS directive allows Apache to execute CGI programs in a specific directory. When a client requests a resource in this particular directory, Apache assumes that the file is a CGI program and tries to run it.
Scriptalias directive shape such as:
- scriptalias/cgi-bin//usr/local/apache/cgi-bin/
CGI outside the Scriptalias directory
For security reasons, CGI programs are typically limited to directories specified by Scriptalias, so administrators can strictly control who can use CGI programs. However, if appropriate security method measures are taken, there is no reason not to allow CGI programs in other directories to run. For example, you might want users to hold pages in the host directory specified by Userdir, and they have their own CGI programs, but have no access to the Cgi-bin directory, which creates the need to run CGI programs in other directories.
1, using the options to explicitly allow the execution of CGI
You can use the options directive to explicitly allow the execution of CGI in a particular directory in the primary server configuration file:
- <Directory/usr/local/apache/htdocs/somedir>
- Options +execcgi
- </Directory>
The above instructions enable Apache to allow CGI file execution. Also, you must tell the server which files are CGI files. The following AddHandler instruction tells the server that all files with CGI or PL suffixes are CGI programs:
- addhandler cgi-script CGI PL
. htaccess files are a way to configure a directory. When Apache provides a resource, it looks for the. htaccess file in the directory where the resource resides, and if so, makes the instruction in it effective. The allowoverride directive determines whether the. htaccess file is valid, specifies which directives can appear in it, or is not allowed at all. To do this, you need to configure this in the primary server configuration:
- AllowOverride Options
In the. htaccess file, this configuration is required:
- Options +execcgi
To enable Apache to allow the execution of CGI programs in this directory.
Finally, you can write a CGI program test, such as:
- #!/usr/bin/perl
- print "content-type:text/html\n\n";
- Print "Hello, world.";
Configuring Apache to support the operation of CGI processes