【问题标题】:How to run cgi script on apache server如何在 apache 服务器上运行 cgi 脚本
【发布时间】:2016-12-08 19:25:07
【问题描述】:

这是我的程序:

[root@localhost cgi-bin]# locate first.pl 
/home/Ram/Desktop/work/first.pl
/usr/local/apache2/cgi-bin/first.pl
[root@localhost cgi-bin]# cd /usr/local/apache2/cgi-bin/
[root@localhost cgi-bin]# vi first.pl 
#!/usr/bin/perl -w
use warnings;
use warnings;
use CGI;
print "content-type: text/html\n\n";
print "<h2>Hello, World!</h2>\n";

我从我的浏览器调用脚本是这样的:

http://localhost/usr/local/apache2/cgi-bin/first.pl

我没有得到输出,但我得到一个错误:

找不到

在此服务器上找不到请求的 URL /usr/local/apache2/cgi-bin/first.pl。

Apache/2.2.15 (CentOS) 服务器在 localhost 端口 80

我通过以下方式在网络浏览器中检查了 Apache 网络服务器是否正常工作:

https://localhost

我们显示欢迎页面。

如何解决此错误?

【问题讨论】:

  • 你能添加你的虚拟主机配置吗
  • 没有 httpd.conf 或者如果你的虚拟主机有一个单独的配置文件包括它
  • 尝试运行“chmod 755 /usr/local/apache2/cgi-bin/first.pl”
  • 你能告诉我如何查看httpd.conf
  • @Mark Setchell 在浏览器中???

标签: apache perl cgi


【解决方案1】:

在linux上安装apache2后基本上需要修改两个文件。

转到终端并设置以下配置:

  1. sudo vim /etc/apache2/sites-enabled/000-default.conf 并添加以下内容:

    <Files ~ "\.(pl|cgi)$">
        SetHandler perl-script
        PerlResponseHandler ModPerl::PerlRun
        Options +ExecCGI
        PerlSendHeader On
    </Files>
    
  2. sudo vim /etc/apache2/apache2.conf 并添加以下内容:

    <Directory /var/www/cgi-bin/> AddHandler cgi-script .cgi .pl Options FollowSymLinks ExecCGI AllowOverride None </Directory>

添加这两个配置更改后,编写一个perl脚本,将其放在cgi-bin目录中,然后赋予它足够的权限(sudo chmod 755 &lt;filename&gt;

最后重启apache2:sudo apache2ctl restart

截图:

【讨论】:

    【解决方案2】:

    是的,上述过程有效,但简单的方法是:

    1. 启用CGI- sudo a2enmod cgi
    2. 重启 Apache 即可运行 -service apache2 restart
    3. 运行cgi文件http://localhost/cgi-bin/1.sh

    祝你好运!!

    【讨论】:

      【解决方案3】:

      在您的网络配置(httpd.conf 或您的虚拟主机配置文件)中,您应该有以下片段:

      ScriptAlias /cgi-bin/ /etc/local/apache2/cgi-bin/
      <Directory "/etc/local/apache2/cgi-bin">
         Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
      </Directory>
      

      重启服务器,别忘了chmod +x /usr/local/apache2/cgi-bin/first.pl再加载http://localhost/cgi-bin/first.pl

      这假定您的配置中没有虚拟主机,或者您配置的虚拟主机是默认主机。如果需要,请参阅 apache 文档。

      【讨论】:

      • ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" # # "/var/www/cgi-bin" should be changed to whatever your ScriptAliased # CGI directory exists, if you have that configured. # &lt;Directory "/var/www/cgi-bin"&gt; AllowOverride None Options None Order allow,deny Allow from all &lt;/Directory&gt; 这个只有
      • 这些/usr/local/apache2/conf/httpd.conf/usr/local/apache2/conf/original/httpd.conf 只存在于机器中
      • 在哪一个上检查 httpd.conf?请告诉我
      • 我认为是/usr/local/apache2/conf/httpd.conf,但无论如何,为什么不尝试一个,如果它不起作用尝试另一个?
      • 您之前的评论中是否隐藏了一个问题? ;--)
      猜你喜欢
      • 2018-01-30
      • 2012-07-27
      • 2019-08-06
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      相关资源
      最近更新 更多