【问题标题】:PERL do not run script after PARPERL 在 PAR 之后不运行脚本
【发布时间】:2021-07-28 07:21:07
【问题描述】:
[root@localhost html]# rpm -q centos-release
centos-release-7-9.2009.1.el7.centos.x86_64

[root@localhost html]# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built:   Dec 13 2020 00:35:05

[root@localhost html]# perl -v
This is perl 5, version 34, subversion 0 (v5.34.0) built for x86_64-linux

[root@localhost html]# cpan
cpan shell -- CPAN exploration and modules installation (v2.28)


[root@localhost html]# pwd
/var/www/html

[root@localhost html]# ls -lha
-rw-r--r--. 1 apache apache  239 Jul 26 09:54 .htaccess
-rw-r--r--. 1 apache apache   47 Jul 25 20:00 index.html
-rwxr-xr-x. 1 apache apache   97 Jul 26 11:02 perl.pl

脚本在 http://localhost/perl.pl 上正常运行

PERL 脚本内容

#!/usr/bin/perl
use strict;
use warnings;

print "Content-type: text/html\n\n";

print "Hello World!";

exit;


[root@localhost html]# pp -x -c -o a.out perl.pl
/tmp/kjzLwQRe6G syntax OK

[root@localhost html]# mv a.out xx.pl
[root@localhost html]# chown apache:apache xx.pl
[root@localhost html]# chmod 755 xx.pl


[root@localhost html]# ./perl.pl
Content-type: text/html

Hello World![root@localhost html]# ./xx.pl
Content-type: text/html

Hello World![root@localhost html]#

当我通过浏览器 http://localhost/xx.pl 运行脚本时

内部服务器错误

服务器遇到内部错误或配置错误,无法完成您的请求。

[root@localhost html]# tail -f /var/log/httpd/ssl_error_log
[Mon Jul 26 11:06:18.315481 2021] [cgi:error] [pid 18880] [client 2.57.171.41:12219] End of script output before headers: xx.pl
[Mon Jul 26 11:07:21.702880 2021] [cgi:error] [pid 18881] [client 2.57.171.41:9437] End of script output before headers: xx.pl
[Mon Jul 26 11:07:22.742897 2021] [cgi:error] [pid 18879] [client 2.57.171.41:19913] End of script output before headers: xx.pl
[Mon Jul 26 11:07:28.448565 2021] [cgi:error] [pid 18882] [client 2.57.171.41:27467] End of script output before headers: xx.pl
[Mon Jul 26 11:30:08.557949 2021] [cgi:error] [pid 18883] [client 2.57.171.41:30987] End of script output before headers: xx.pl

【问题讨论】:

  • 为什么日志显示perl.plxx.pl?像这样:End of script output before headers: perl.pl 然后是这样:End of script output before headers: xx.pl ?
  • 如果您从终端而不是浏览器http://localhost 运行脚本,输出是什么?
  • CGI 的文档说基本上你不应该再使用它了,还有更好的选择,并且一些 HTML 生成功能不再被维护。
  • print 不结束换行可能会导致自动刷新问题,请尝试添加。虽然程序在退出时应该刷新缓冲区,但可能会有一些轻微的延迟。

标签: perl perl-packager pp-perl-par-packager


【解决方案1】:

我能够在 Ubuntu 21.04 上使用 XAMPP 8.0.8 完成这项工作。我首先通过编辑/opt/lampp/etc/httpd.conf 以包含文件etc/extra/httpd-vhosts.conf 来设置一个虚拟主机(我将其命名为localhost2)。然后编辑后一个文件以添加新的虚拟主机。然后编辑/etc/hosts 以包含添加的虚拟主机。

然后从/opt/lampp/etc/httpd.conf启用CGI脚本:

  • 确保加载这些模块:

    LoadModule cgi_module modules/mod_cgi.so
    LoadModule alias_module modules/mod_alias.so
    
  • 然后像这样设置ScriptAlias目录:

    <IfModule alias_module>
        ScriptAlias /cgi-bin/ "/opt/lampp/cgi-bin/"
    </IfModule>
    
  • 最后,通过包含以下内容启用对脚本目录的访问:

    <Directory "/opt/lampp/cgi-bin">
        AllowOverride None
        Options ExecCGI
        AddHandler cgi-script .cgi .pl .bin
        Require all granted
    </Directory>
    

现在确保目录/opt/lampp/cgi-bin 对所有人具有读取和搜索权限。

然后创建一个perl脚本/opt/lampp/cgi-bin/hello.pl:

#!/usr/bin/perl
use strict;
use warnings;

print "Content-type: text/html\n\n";
print "Hello World!\n";
exit;

确保脚本具有所有人的读取和执行权限:

$ sudo chmod 755 hello.pl

使用pp(来自PAR::Packer)将脚本转换为二进制文件:

$ sudo pp -x -c -o a.bin hello.pl

(再次确保脚本具有所有人的读取和执行权限) 请注意,二进制文件有一个 .bin 扩展名,并且我在上面的 httpd.conf 中添加了一个 .bin 处理程序。

启动 LAMP 网络服务器,访问页面http://localhost2/cgi-bin/a.bin。浏览器窗口中的输出是

Hello World!

【讨论】:

    猜你喜欢
    • 2011-06-15
    • 2022-06-16
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多