如何使用httpd将测试perl脚本作为cgi脚本运行? [英] How to run test perl script as cgi script with httpd?

查看:168
本文介绍了如何使用httpd将测试perl脚本作为cgi脚本运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循本教程并运行示例

I'm trying to follow this tutorial and run sample script under cgi.

下面是我做的步骤:

1)制作示例脚本

$ cat > test.pl <<EOF
> #!/usr/bin/perl
> print "Content-type: text/html\n\n";
> print "Hello, World.";
EOF

2)运行httpd docker容器并将创建的脚本作为卷挂载在cgi-bin中目录:

2) run httpd docker container and mounted created script as volume in cgi-bin directory:

$ docker run --name cgi_test --rm -p 3000:80 -v $(pwd)/test.pl:/usr/local/apache2/cgi-bin/test.pl httpd

3)检查是否启用了CGI模块:

3) check if CGI module is enabled:

$ docker exec -it cgi_test cat /usr/local/apache2/conf/httpd.conf | grep modules/mod_cgid.so
    #LoadModule cgi_module modules/mod_cgid.so

CGI模块是原来是这样,所以我启用了它:

CGI module was turned out so I enabled it:

$ docker exec -it cgi_test sed -i 's,#\(LoadModule cgid_module modules/mod_cgid.so\),\1,g' /usr/local/apache2/conf/httpd.conf

4)检查 ScriptAlias 指令并启用 alias_module

4) Checked ScriptAlias directive and enabling of alias_module :

$ docker exec -it cgi_test cat /usr/local/apache2/conf/httpd.conf | grep "ScriptAlias /cgi-bin/"
    ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/"
$ docker exec -it cgi_test httpd -M | grep -e cgid -e alias
 cgid_module (shared)
 alias_module (shared)

没关系

5)检查执行权限:

$ docker exec -it cgi_test ls -la /usr/local/apache2/cgi-bin/test.pl
-rwxr-xr-x 1 1000 1000 76 Mar 20 12:41 /usr/local/apache2/cgi-bin/test.pl

还可以

6)在docker容器中重新启动httpd:

6) Restarted httpd in docker container:

$ docker kill --signal="USR1" cgi_test

但是当我访问 http:// localhost:3000 / cgi-bin / test.pl 我看到 503服务不可用和日志中的此错误:

But when I to to http://localhost:3000/cgi-bin/test.pl I see 503 Service Unavailable and this error in log:

[Wed Mar 20 19:03:02.323430 2019] [cgid:error] [pid 1446:tid 139952364902144] (22)Invalid argument: [client 172.17.0.1:34328] AH01257: unable to connect to cgi daemon after multiple tries: /usr/local/apache2/cgi-bin/test.pl

我还需要做什么来运行脚本?

What else I need to do to run script ?

推荐答案

要在CGI下运行脚本,您无需更改官方 httpd Docker中的任何文件图像。

To run a script under CGI you don't need to change any file in the official httpd Docker image.

请确保您的脚本可以通过 chmod + x test.pl 执行,并传递给服务器使用配置指令对 -c 选项进行编程以加载CGI模块,如以下命令所述:

Make sure that your script is executable with a chmod +x test.pl and pass to the server program the -c option with the configuration directive to load the CGI module, as described in the following command:

docker run \
  -it \
  --rm \
  -p 127.0.0.1:8080:80 \
  -v $(pwd)/test.pl:/usr/local/apache2/cgi-bin/test.pl \
  httpd:alpine \
  httpd-foreground -c "LoadModule cgid_module modules/mod_cgid.so"

一些解释:


  • 使用高山变体图像是因为它比默认的基于Debian的图像小,并且足以满足我们的需求

  • Alpine variant image is used because it's smaller than the default Debian based image and it's more than enough for our purposes

httpd-foreground 脚本在所有官方 httpd 映像中均可用,并且允许在前台模式下执行Apache HTTP Server(等效于 httpd- D FOREGROUND

httpd-foreground script is available in all official httpd images and it allows to execute Apache HTTP Server in foreground mode (it's equivalent to httpd -D FOREGROUND)

发布选项 -p 还包含回送IP地址以防止从外部访问该测试服务器

publish option -p contains also the loopback IP address to prevent access to this test server from the outside

这篇关于如何使用httpd将测试perl脚本作为cgi脚本运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆