簡體   English   中英

如何配置 Apache 2 以運行 Perl CGI 腳本?

[英]How do I configure Apache 2 to run Perl CGI scripts?

我想配置運行在Kubuntu上的Apache 2來執行Perl CGI腳本。 我已經嘗試了通過谷歌搜索遇到的一些步驟,但似乎沒有任何效果。

實現這一目標的正確方法是什么?

這篇文章旨在拯救遭受*無法在 Ubuntu 上為 Perl 正確設置 Apache2 的人們。(特定於您的 Linux 機器的系統配置將在方括號中提及,例如 [this])。

設置不當的可能結果 Apache 2:

  1. 瀏覽器嘗試下載 .pl 文件而不是執行並給出結果。
  2. 禁止。
  3. 內部服務器錯誤。

如果一個人以合理的智慧遵循下面描述的步驟,他/她就可以克服上面提到的錯誤。

在開始這些步驟之前。 Go 到/etc/hosts文件並添加 IP 地址/域名`例如:

127.0.0.1 www.BECK.com

第一步:安裝apache2第二步:安裝mod_perl第三步:配置apache2

打開sites-available/default並添加以下內容,

<Files ~ "\.(pl|cgi)$">
    SetHandler perl-script
    PerlResponseHandler ModPerl::PerlRun
    Options +ExecCGI
    PerlSendHeader On
</Files>

<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory [path-to-store-your-website-files-like-.html-(perl-scripts-should-be-stored-in-cgi-bin] >
####(The Perl/CGI scripts can be stored out of the cgi-bin directory, but that's a story for another day. Let's concentrate on washing out the issue at hand)
####
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

ScriptAlias /cgi-bin/ [path-where-you-want-your-.pl-and-.cgi-files]

<Directory [path-where-you-want-your-.pl-and-.cgi-files]>
    AllowOverride None
    Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
    AddHandler cgi-script .pl
    Order allow,deny
    allow from all
</Directory>
<Files ~ "\.(pl|cgi)$">
    SetHandler perl-script
    PerlResponseHandler ModPerl::PerlRun
    Options +ExecCGI
    PerlSendHeader On
</Files>

<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory [path-to-store-your-website-files-like-.html-(perl-scripts-should-be-stored-in-cgi-bin] >
####(The Perl/CGI scripts can be stored out of the cgi-bin directory, but that's a story for another day. Let's concentrate on washing out the issue at hand)
####
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

ScriptAlias /cgi-bin/ [path-where-you-want-your-.pl-and-.cgi-files]

<Directory [path-where-you-want-your-.pl-and-.cgi-files]>
    AllowOverride None
    Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
    AddHandler cgi-script .pl
    Order allow,deny
    allow from all
</Directory>

第4步:

將以下行添加到您的/etc/apache2/apache2.conf文件中。

AddHandler cgi-script .cgi .pl
<Files ~ "\.pl$">
Options +ExecCGI
</Files>
<Files ~ "\.cgi$">
Options +ExecCGI
</Files>

<IfModule mod_perl.c>
<IfModule mod_alias.c>
Alias /perl/ /home/sly/host/perl/
</IfModule>
<Location /perl>
SetHandler perl-script
PerlHandler Apache::Registry
Options +ExecCGI
</Location>
</IfModule>

<Files ~ "\.pl$">
Options +ExecCGI
</Files>

第 5 步:

非常重要,或者至少我猜是這樣,只有在完成這一步之后,我才能讓它發揮作用。

AddHandler cgi-script.cgi.pl

<Files ~ "\.pl$">
Options +ExecCGI
</Files>
<Files ~ "\.cgi$">
Options +ExecCGI
</Files>

<IfModule mod_perl.c>
<IfModule mod_alias.c>
Alias /perl/ /home/sly/host/perl/
</IfModule>
<Location /perl>
SetHandler perl-script
PerlHandler Apache::Registry
Options +ExecCGI
</Location>
</IfModule>

<Files ~ "\.pl$">
Options +ExecCGI
</Files>

步驟 6

非常重要,或者至少我猜是這樣,只有在完成這一步之后,我才能讓它發揮作用。

將以下內容添加到/etc/apache2/sites-enabled/000-default文件

<Files ~ "\.(pl|cgi)$">
SetHandler perl-script
PerlResponseHandler ModPerl::PerlRun
Options +ExecCGI
PerlSendHeader On
</Files>

第 7 步:

現在將您的 Perl 腳本作為 test.pl 添加到您之前在第 3 步中提到的位置 [ path-where-you-want-your-.pl-and-.cgi-files ]。

使用chmod授予.pl文件權限,然后在瀏覽器的地址欄中鍵入webaddress/cgi-bin/test.pl ,go,你知道了。

(現在,這篇文章中的許多內容都是多余的。請忽略它。)

您需要查看 Apache 錯誤日志以了解“內部服務器錯誤”是什么。 根據我的經驗,四種最可能的情況是:

  1. CGI 程序位於未啟用 CGI 執行的目錄中。 解決方案:通過 httpd.conf 或 .htaccess 文件將ExecCGI選項添加到該目錄。

  2. Apache 僅配置為從專用的cgi-bin目錄運行 CGI。 解決辦法:將CGI程序移到那里或者在httpd.conf中添加一條AddHandler cgi-script.cgi語句。

  3. CGI 程序未設置為可執行。 解決方案(假設是 *nix 類型的操作系統): chmod +x my_prog.cgi

  4. CGI 程序在不發送標頭的情況下退出。 解決方案:從命令行運行程序並驗證 a) 它實際運行而不是因編譯時錯誤而死掉,以及 b) 它生成正確的 output,其中至少應包括Content-Type header 和最后一個標題之后的空行。

(谷歌搜索讓我想到了這個問題,盡管我沒有要求 perl)

我在運行腳本時遇到了問題(盡管 bash 不是 perl)。 Apache 的配置為ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/但是 Apache 錯誤日志顯示File does not exist: /var/www/cgi-bin/test.html

嘗試將腳本放在/usr/lib/cgi-bin//var/www/cgi-bin/中,但都沒有用。

經過長時間的谷歌搜索 session 對我來說破解它的是sudo a2enmod cgi並且使用/usr/lib/cgi-bin/一切都到位了。

如果您已成功安裝 Apache web 服務器和 Perl 請按照以下步驟在 ubuntu 系統上使用 perl 運行 cgi 腳本。

在開始使用 CGI 腳本之前,有必要配置 apache 服務器,使其能夠識別 CGI 目錄(保存 CGI 程序的地方)並允許在該目錄中執行程序。

  1. 在 Ubuntu 中,cgi-bin 目錄通常位於路徑 /usr/lib 中,如果不存在,請使用以下命令創建 cgi-bin 目錄。 cgi-bin本身應該在這個路徑中。

     mkdir /usr/lib/cgi-bin
  2. 發出以下命令來檢查目錄的權限狀態。

     ls -l /usr/lib | less

檢查權限是否為“drwxr-xr-x 2 root root 4096 2011-11-23 09:08 cgi-bin”,如果是 go 到第 3 步。

如果沒有發出以下命令以確保我們的 cgi-bin 目錄具有適當的權限。

     sudo chmod 755 /usr/lib/cgi-bin
     sudo chmod root.root /usr/lib/cgi-bin
  1. 賦予cgi-bin目錄執行權限

     sudo chmod 755 /usr/lib/cgi-bin

因此,您的 cgi-bin 目錄已准備好到 go。這是您放置所有要執行的 cgi 腳本的地方。 我們的下一步是配置 apache 以識別 cgi-bin 目錄並允許執行其中的所有程序作為 cgi 腳本。

配置 Apache 以使用 perl 運行 CGI 腳本

  1. 需要在 apache 服務器的配置文件中添加一個指令,以便它知道 CGI 的存在及其目錄的位置。 最初 go 到 apache 配置文件的位置,然后用你喜歡的文本編輯器打開它

    cd /etc/apache2/sites-available/ sudo gedit 000-default.conf
  2. 將以下內容復制到文件 000-default.conf 代碼行“DocumentRoot /var/www/html/”和“ErrorLog $ {APACHE_LOG_DIR}/error.log”之間

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Require all granted

  3. 使用以下代碼重啟apache服務器

    sudo service apache2 restart
  4. 現在我們需要啟用默認情況下出現在較新版本 ubuntu 中的 cgi 模塊

    sudo a2enmod cgi.load sudo a2enmod cgid.load
  5. 此時我們可以重新加載 apache 網絡服務器,以便它再次讀取配置文件。

     sudo service apache2 reload

apache 的配置部分現在已經結束,讓我們用一個示例 cgi perl 程序來檢查它。

測試一下

  1. Go 到cgi-bin目錄下,創建一個擴展名為.pl的cgi文件

    cd /usr/lib/cgi-bin/ sudo gedit test.pl
  2. 將以下代碼復制到 test.pl 並保存。

     #:/usr/bin/perl -w print “Content-type; text/html\r\n\r\n”; print “CGI working perfectly!! \n”;
  3. 現在給 test.pl 執行權限。

     sudo chmod 755 test.pl
  4. 現在在您的 web 瀏覽器中打開該文件http://Localhost/cgi-bin/test.pl

  5. 如果您看到 output “CGI working perfectly”,您就可以使用 go。現在將所有程序轉儲到 cgi-bin 目錄並開始使用它們。

注意:不要忘記在 cgi-bin 中為您的新程序提供 chmod 755 權限,以便在沒有任何內部服務器錯誤的情況下成功運行它。

Ubuntu 12.04 (Precise Pangolin)(可能是之前的一兩個版本)開始,只需通過 Synaptic 安裝apache2mod-perl並將 CGI 腳本放在 /usr/lib/cgi-bin 中,這就是您真正需要做的全部。

有兩種方法可以處理 CGI 腳本, SetHandlerAddHandler

SetHandler cgi-script

適用於給定上下文中的所有文件,無論它們如何命名,甚至是index.htmlstyle.css

AddHandler cgi-script .pl

類似,但適用於在給定上下文中以.pl結尾的文件。 如果願意,您可以選擇另一個或多個擴展名。

此外,必須加載 CGI 模塊並配置Options +ExecCGI 要激活模塊,發出

a2enmod cgi

然后重新啟動或重新加載 Apache。最后,Perl CGI 腳本必須是可執行的。 所以必須設置執行位

chmod a+x script.pl

它應該從

#! /usr/bin/perl

作為它的第一行。


當您在任何指令之外使用SetHandlerAddHandler (和Options +ExecCGI )時,它會全局應用於所有文件。 但是您可以通過將這些指令包含在內部來將上下文限制為一個子集,例如Directory

<Directory /path/to/some/cgi-dir>
    SetHandler cgi-script
    Options +ExecCGI
</Directory>

現在SetHandler僅適用於 /path/to/some/cgi-dir 內的文件,而不適用於 web 站點的所有文件。 當然,在DirectoryLocation指令中使用AddHandler也是如此。 然后它適用於 /path/to/some/cgi-dir 中以.pl結尾的文件。

對於那些像我這樣一直在摸索着通過比你現在需要知道的多得多的教程和文檔的人來說,只是想看看對初學者有用的東西,我發現了我唯一擁有的東西要做的是添加:

AddHandler cgi-script.pl.cgi

到我的配置文件。

http://httpd.apache.org/docs/2.2/mod/mod_mime.html#addhandler

對於我的情況,這最有效,因為我可以將我的 perl 腳本放在任何我想要的地方,並且只需添加 .pl 或 .cgi 擴展名。

Dave Sherohman 的回答也提到了 AddHandler 解決方案。

當然,您仍然必須確保正確設置腳本的權限/所有權,尤其是腳本是可執行的。 記下從 http 請求運行時“用戶”是誰 - 例如,www 或 www-data。

我猜您已經看過mod_perl了吧?

您是否嘗試過以下教程

編輯:關於你的帖子——也許你可以在你的.cgi文件中包含一個代碼示例。 甚至前幾行?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM