Configure Apache to run CGIProgramThere are two possible cases: cgi in the ScriptAlias directory and CGI in a directory other than ScriptAlias.Cgi in the ScriptAlias directory
The ScriptAlias command allows Apache to execute CGI programs in a specific directory. When the client requests resources in a specific directory, Apache assumes that all files in the directory are CGI programs and tries to run them.
The ScriptAlias command is shown as follows:
# ScriptAlias: This controls which directories contain server scripts.
# Scriptaliases are essential the same as aliases, doesn't that
# Documents in the target directory are treated as applications and
# Run by the server when requested rather than as your ents sent to
# Client. The same rules about trailing "/" apply to ScriptAlias
# Directives as to alias.
# "/Usr/local/Apache/cgi-bin" shocould be changed to whatever your scriptaliased
# CGI directory exists, if you have that configured.
Order allow, deny
Allow from all
# Addhandler allows you to map certain file extensions to "handlers ":
# Actions unrelated to filetype. These can be either built into the server
# Or added with the Action Directive (see below)
# To use CGI scripts outside of scriptaliased directories:
# (You will also need to add "execcgi" to the "options" directive .)
Addhandler CGI-script. cgi
Addhandler CGI-script. pl
1) Create the file/usr/local/Apache/cgi-bin/test. cgi, chmod A + x/usr/local/Apache/cgi-bin/test. cgi.
Print "Content-Type: text/html \ n ";
Print "Hello, world .";
2) Access http: // 192.168.0.120/cgi-bin/test. cgi in the browser
CGI other than the ScriptAlias directory
For security reasons, CGI programs are usually restricted in the directory specified by ScriptAlias, so that the administrator can strictly control who can use CGI programs. However, if appropriate security measures are taken, there is no reason not to allow CGI programs in other directories to run. For example, you may want users to store pages in the home directory specified by userdir, but they have their own CGI programs, but do not have access to the cgi-bin directory, the requirement to run CGI programs in other directories is generated.
1. Use options to explicitly allow CGI execution
You can use the Options command in the configuration file of the master server to explicitly allow CGI execution in a specific directory:
The preceding command allows Apache to execute cgi files. In addition, you must tell the server which files are cgi files. The following addhandler Command tells the server that all the files with CGI or PL suffixes are CGI programs:
2.. htaccess File
The. htaccess file is a method for directory configuration. When apache provides a resource, it searches for the. htaccess file in the directory where the resource is located. If yes, the command takes effect. The AllowOverride command determines whether the. htaccess file is valid. It specifies which commands can appear or are not allowed at all. To do this, you need to configure it in the master server configuration:
In the. htaccess file, you need to configure the following:
To allow Apache to execute CGI programs in this directory.
The configuration is as follows:
Options + execcgi
Addhandler CGI-script. cgi
If it is CGI in cgi-bin, the configuration is as follows: