Configure Apache to run CGI program

Source: Internet
Author: User

 

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.
#
ScriptAlias/cgi-bin/"/usr/local/Apache/cgi-bin /"

#
# "/Usr/local/Apache/cgi-bin" shocould be changed to whatever your scriptaliased
# CGI directory exists, if you have that configured.
#
<Directory "/usr/local/Apache/cgi-bin">
AllowOverride none
Options none
Order allow, deny
Allow from all
</Directory>

#
# 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

 

Test:

1) Create the file/usr/local/Apache/cgi-bin/test. cgi, chmod A + x/usr/local/Apache/cgi-bin/test. cgi.

/Usr/local/Apache/cgi-bin/test. cgi

#! /Usr/bin/perl
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:

    1. <Directory/usr/local/Apache/htdocs/somedir>
    2. Options + execcgi
    3. </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:

    1. Addhandler CGI-script. cgi. pl

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:

    1. AllowOverride options

In the. htaccess file, you need to configure the following:

    1. Options + execcgi

To allow Apache to execute CGI programs in this directory.

Run CGI

The configuration is as follows:

<Directory/home/*/public_html>
Options + execcgi
Addhandler CGI-script. cgi
</Directory>

If it is CGI in cgi-bin, the configuration is as follows:

<Directory/home/*/public_html/cgi-bin>
Options execcgi
Sethandler CGI-script
</Directory>

Complete!

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.