配置apache運行cgi程序


 

配置apache運行cgi程序可分為兩種情況,一是ScriptAlias目錄的CGI,二是ScriptAlias以外目錄的CGI。

ScriptAlias目錄的CGI

ScriptAlias指令使Apache允許執行一個特定目錄中的CGI程序。當客戶端請求此特定目錄中的資源時,Apache假定其中文件都是CGI程序並試圖運行。
ScriptAlias指令形如:

#
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the target directory are treated as applications and
# run by the server when requested rather than as documents sent to the
# 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" should 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

 

測試:

1) 創建文件/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\n";
print "Hello, World.";

2)在瀏覽器中訪問http://192.168.0.120/cgi-bin/test.cgi

 

ScriptAlias目錄以外的CGI

由於安全原因,CGI程序通常被限制在ScriptAlias指定的目錄中,如此,管理員就可以嚴格地控制誰可以使用CGI程序。但是,如果采取了恰當的安全方法措施,則沒有理由不允許其他目錄中的CGI程序運行。比如,你可能希望用戶在UserDir指定的宿主目錄中存放頁面,而他們有自己的CGI程序,但無權存取cgi-bin目錄,這樣,就產生了運行其他目錄中CGI程序的需求。
1、用Options顯式地允許CGI的執行
可以在主服務器配置文件中,使用Options指令顯式地允許特定目錄中CGI的執行:

  1. <Directory /usr/local/apache/htdocs/somedir>
  2. Options +ExecCGI
  3. </Directory>

上述指令使Apache允許CGI文件的執行。另外,還必須告訴服務器哪些文件是CGI文件。下面的AddHandler指令告訴服務器所有帶有cgi或pl后綴的文件是CGI程序:

  1. AddHandler cgi-script .cgi .pl

2、.htaccess文件
.htaccess文件是針對目錄進行配置的一種方法。Apache在提供一個資源時,會在此資源所在目錄中尋找.htaccess文件,如果有,則使其中的指令生效。AllowOverride 指令決定了.htaccess文件是否有效,它指定了哪些指令可以出現在其中,或者根本不允許使用。為此,需要在主服務器配置中如此配置:

  1. AllowOverride Options

在.htaccess文件中,需要如此配置:

  1. Options +ExecCGI

以使Apache允許此目錄中CGI程序的執行。

運行訪問所有用戶home下的CGI

配置如下:

<Directory /home/*/public_html>
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>

如果只是cgi-bin下的為cgi,配置如下:

<Directory /home/*/public_html/cgi-bin>
Options ExecCGI
SetHandler cgi-script
</Directory>

完!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM