如何配置Apache 2以运行Perl CGI脚本?

44 linux apache perl cgi

我想配置运行的Apache 2 Kubuntu来执行Perl CGI脚本.我尝试了谷歌搜索遇到的一些步骤,但似乎没有任何效果.

实现这一目标的正确方法是什么?

bec*_*076 25

这篇文章旨在拯救那些在Ubuntu上无法正确设置Apache2 for Perl的人.(特定于Linux机器的系统配置将在方括号内提及,如[this]).

Apache 2的不正确设置的可能结果:

  1. 浏览器尝试下载.pl文件而不是执行并给出结果.
  2. 故宫.
  3. 内部服务器错误.

如果按照下面描述的步骤以合理的智慧进行操作,他/她可以解决上述错误.

在开始步骤之前.转到/etc/hosts文件并添加IP地址/域名名称,例如:

127.0.0.1 www.BECK.com
Run Code Online (Sandbox Code Playgroud)

步骤1:安装apache2 步骤2:安装mod_perl 步骤3:配置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>
Run Code Online (Sandbox Code Playgroud)

第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>
Run Code Online (Sandbox Code Playgroud)

第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>
Run Code Online (Sandbox Code Playgroud)

第6步

非常重要,或者至少我是这么认为的,只有在完成这一步之后,才能让它发挥作用.

将以下内容添加到您的/etc/apache2/sites-enabled/000-default文件中

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

第7步:

现在将你的Perl脚本作为test.pl添加到你之前在步骤3中提到的地方作为[ path-where-you-your-your-.pl-and-.cgi-files ].

.pl使用chmod然后在文件webaddress/cgi-bin/test.pl的地址栏中键入权限,然后输入,你得到它.

(现在,这篇文章中的许多内容都是多余的.请忽略它.)

  • 100万行只是为了运行perl? (13认同)

Dav*_*man 19

您需要查看Apache错误日志以查看"内部服务器错误"是什么.根据我的经验,四种最可能的情况是:

  1. CGI程序位于未启用CGI执行的目录中.解决方案:ExecCGI通过httpd.conf或.htaccess文件将选项添加到该目录.

  2. Apache仅配置为从专用cgi-bin目录运行CGI .解决方案:在那里移动CGI程序或AddHandler cgi-script .cgi向httpd.conf 添加语句.

  3. CGI程序未设置为可执行文件.解决方案(假设是*nix类型的操作系统):chmod +x my_prog.cgi

  4. CGI程序正在退出而不发送标题.解决方案:从命令行运行程序并验证a)它实际运行而不是死于编译时错误b)它生成正确的输出,其中应至少包括一个Content-Type标题和一个空行在最后一个标题之后.


nor*_*ley 14

(即使我没有要求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/但都没有工作.

经过长时间的谷歌搜索会议,对我来说破解它是 sudo a2enmod cgi一切都使用了/usr/lib/cgi-bin/.


小智 5

Ubuntu 12.04(精确穿山甲)开始(可能是之前的一两个版本)只需安装apache2mod-perl通过Synaptic并将您的CGI脚本放在/ usr/lib/cgi-bin中就可以了.


小智 5

如果您已经成功安装了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
    
    Run Code Online (Sandbox Code Playgroud)
  2. 发出以下命令以检查目录的权限状态。

     ls -l /usr/lib | less
    
    Run Code Online (Sandbox Code Playgroud)

如果权限为“ drwxr-xr-x 2 root root 4096 2011-11-23 09:08 cgibin”,请执行步骤3。

如果没有,请发出以下命令以确保对我们的cgi-bin目录具有适当的权限。

     sudo chmod 755 /usr/lib/cgi-bin
     sudo chmod root.root /usr/lib/cgi-bin
Run Code Online (Sandbox Code Playgroud)
  1. 将执行权限授予cgi-bin目录

     sudo chmod 755 /usr/lib/cgi-bin
    
    Run Code Online (Sandbox Code Playgroud)

因此,您的cgi-bin目录已准备就绪。在这里放置所有cgi脚本以执行。下一步是配置apache以识别cgi-bin目录,并允许其中所有程序作为cgi脚本执行。

配置Apache使用Perl运行CGI脚本

  1. 需要在apache服务器的配置文件中添加一个指令,以便它知道CGI的存在及其目录的位置。最初转到apache的配置文件的位置,然后使用您喜欢的文本编辑器将其打开

    cd /etc/apache2/sites-available/ 
    sudo gedit 000-default.conf
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将以下内容复制到代码行“ DocumentRoot / var / www / html /”和“ ErrorLog $ {APACHE_LOG_DIR} /error.log”之间的文件000-default.conf中。

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Require all granted
    
    Run Code Online (Sandbox Code Playgroud)

  3. 使用以下代码重新启动apache服务器

    sudo service apache2 restart
    
    Run Code Online (Sandbox Code Playgroud)
  4. 现在我们需要启用默认情况下在较新版本的ubuntu中存在的cgi模块

    sudo a2enmod cgi.load
    sudo a2enmod cgid.load
    
    Run Code Online (Sandbox Code Playgroud)
  5. 此时,我们可以重新加载apache网络服务器,以便它再次读取配置文件。

    sudo service apache2 reload
    
    Run Code Online (Sandbox Code Playgroud)

apache的配置部分已经结束,现在让我们使用示例cgi perl程序对其进行检查。

测试出来

  1. 进入cgi-bin目录,创建扩展名为.pl的cgi文件。

    cd /usr/lib/cgi-bin/
    sudo gedit test.pl
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将以下代码复制到test.pl上并保存。

    #!/usr/bin/perl -w
    print “Content-type: text/html\r\n\r\n”;
    print “CGI working perfectly!! \n”;
    
    Run Code Online (Sandbox Code Playgroud)
  3. 现在,授予test.pl执行权限。

    sudo chmod 755 test.pl
    
    Run Code Online (Sandbox Code Playgroud)
  4. 现在,在您的Web浏览器中打开该文件http://Localhost/cgi-bin/test.pl

  5. 如果看到输出“ CGI正常工作”,就可以开始工作。现在将所有程序转储到cgi-bin目录中并开始使用它们。

注意:不要忘记给您的新程序cgi-bin和chmod 755授予权限,以便在没有任何内部服务器错误的情况下成功运行它。


too*_*kit 1

我猜你已经看过mod_perl了?

您尝试过以下教程吗?

编辑:关于您的发布 - 也许您可以在.cgi文件中包含代码示例。也许甚至是前几行?