【问题标题】:How do I run a cgi script on an apache web server如何在 apache Web 服务器上运行 cgi 脚本
【发布时间】:2018-01-30 03:26:15
【问题描述】:

我不确定这是否是提出这个问题的正确地方。但是我无法让我的 cgi 脚本在我的 XAMPP 服务器上运行(使用 windows 8 和 apache)这是我的 cgi 脚本:

#!usr/bin/perl

use warnings;
use diagnostics;

use strict;

my $time = localtime;
my $remote_id = $ENV{REMOTE_HOST} || $ENV{REMOTE_ADDR};
my $admin_email = $ENV{SERVER_ADMIN};

print <<END_OF_PAGE;
<HTML>
<HEAD>  
<TITLE>Welcome to Mike's Mechanics Database</TITLE>
</HEAD>
<BODY BGCOLOR="#ffffff">  
<IMG SRC="/images/mike.jpg" ALT="Mike's Mechanics">  
<P>Welcome from $remote_host! What will you find here? You'll find a list of mechanics 
from around the country and the type of    service to expect -- based on user input and 
suggestions.</P>  
<P>What are you waiting for? Click <A HREF="/cgi/list.cgi">here</A>    
to continue.</P>  <HR>  <P>The current time on this server is: $time.</P>  
<P>If you find any problems with this site or have any suggestions,    
please email <A HREF="mailto:$admin_email">$admin_email</A>.</P>
</BODY>
</HTML>
END_OF_PAGE

这是我得到的完整错误:

**服务器错误! 服务器遇到内部错误,无法完成您的请求。 错误信息: 无法创建子进程:720002:welcome.cgi 如果您认为这是服务器错误,请联系网站管理员。 错误 500 本地主机 Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/5.6.30

最后,这里是apache错误日志中的条目,对应问题

[Mon Aug 21 20:46:19.403512 2017] [cgi:error] [pid 1436:tid 1724] (OS 2)系统找不到指定的文件。 : [client ::1:61381] 无法创建子进程:720002:welcome.cgi,referer:http://localhost/Perl/

[Mon Aug 21 20:46:19.404515 2017] [cgi:error] [pid 1436:tid 1724] (OS 2)系统找不到指定的文件。 : [client ::1:61381] AH01223: 无法生成子进程:C:/xampp/htdocs/Perl/welcome.cgi,引用者:http://localhost/Perl/

【问题讨论】:

    标签: apache perl localhost cgi


    【解决方案1】:

    我发现了两个问题。

    1. 您的 CGI 脚本实际上并不兼容 CGI——它需要在文档正文之前输出标题。考虑使用CGI 模块为您处理其中的一些问题。

    2. 您在 shebang 中缺少前导斜杠。应该是#!/usr/bin/perl。 (并确保 Perl 实际安装在该路径中。)

    【讨论】:

    • 你能告诉我程序中的标题应该是什么样的吗,我是 cgi 的新手,你提供的链接对我来说真的是压倒性的。谢谢
    • 在 Windows 上,ScriptInterpreterSource 是一个有用的设置。
    【解决方案2】:

    请不要在 2017 年学习 CGI 编程。请查看 CGI::Alternatives,了解一些用 Perl 编写 Web 程序的现代方法。

    话虽如此,解决您当前问题的方法是:

    • 修复代码中的 shebang 行
    • 在 HTML 之前输出 CGI 标头。

    我正在使用 CGI 模块来生成标题。我还更新了您的 HTML,使其看起来像过去十年编写的内容(小写标签、使用 CSS 而不是演示属性、HTML5 文档类型、缩进)。

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use diagnostics; # Remove before putting into production
    
    use CGI 'header';
    
    my $time = localtime;
    my $remote_id = $ENV{REMOTE_HOST} || $ENV{REMOTE_ADDR};
    my $admin_email = $ENV{SERVER_ADMIN};
    
    print header;
    
    print <<END_OF_PAGE;
    <!DOCTYPE html>
    <html>
      <head>
        <style type='text/css'>
    body {
      background-color: #ffffff;
    }
        </style>
        <title>Welcome to Mike's Mechanics Database</title>
      </head>
      <body>
        <img src="/images/mike.jpg" ALT="Mike's Mechanics">  
        <p>Welcome from $remote_host! What will you find here? You'll
          find a list of mechanics from around the country and the type
          of service to expect -- based on user input and  suggestions.</p>
        <p>What are you waiting for?
          Click <a href="/cgi/list.cgi">here</a> to continue.</p>
        <hr>
        <p>The current time on this server is: $time.</p>
        <p>If you find any problems with this site or have any 
          suggestions, please email
          <a href="mailto:$admin_email">$admin_email</a>.</p>
      </body>
    </html>
    END_OF_PAGE
    

    【讨论】:

    • 这段代码和我的代码出现了完全相同的错误,为什么 cgi 不好学?
    • @tgrim90:谷歌搜索错误消息给了我这个链接 - forum.xojo.com/…
    • @tgrim90:至于为什么你不应该学习 CGI - 好吧,只是因为有更好的工具可用。基于 PSGI 的东西更容易开发、更灵活、更强大。
    猜你喜欢
    • 2016-12-08
    • 1970-01-01
    • 2011-09-30
    • 2012-07-27
    • 2014-03-27
    • 2019-08-06
    • 2013-03-30
    • 2019-11-17
    相关资源
    最近更新 更多