这是如何让 CGI Perl 脚本在 CentOS 7 上正确执行。
我把这个留在这里是因为互联网上的很多资源似乎都没有结合这些步骤,让像我这样的人很困惑。
简而言之,这就是需要做的事情。
httpd.conf。httpd.conf。安装和配置软件
sudo yum update
sudo yum install httpd
sudo yum install perl perl-CGI
sudo systemctl start httpd.service
sudo systemctl enable httpd.service
Run Code Online (Sandbox Code Playgroud)
创建您的测试 CGI 文件
即使一直遵循这些步骤,我也从来没有在/var/www/cgi-bin不修改httpd.conf. 相反,我决定只在另一个目录中激活 CGI。
在我的服务器上,我希望 web 根目录html保存我的 CGI 文件。这是/var/www/html/hello.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n"; # This is mandatory.
print "<h2>Hello world!</h2>";
Run Code Online (Sandbox Code Playgroud)
确保 CGI 模块已加载。里面httpd.conf
您可以通过命令对此进行简单检查。
grep -n "LoadModule" /etc/httpd/conf/httpd.conf
Run Code Online (Sandbox Code Playgroud)
我可以看到没有指定 CGI 模块,我通过以下方式确认了它们在模块文件夹中的存在:
find /etc/httpd/modules/ -iname "*cgi*"
Run Code Online (Sandbox Code Playgroud)
这是我所追求的:
/etc/httpd/modules/mod_cgi.so
Run Code Online (Sandbox Code Playgroud)
让我们将其添加到/etc/httpd/conf/httpd.conf文件中:
LoadModule cgi_module modules/mod_cgi.so
Run Code Online (Sandbox Code Playgroud)
我相信您需要在 Ubuntu 中以不同的方式加载模块,请记住这一点。
更改目录设置 httpd.conf
在重新启动之前,httpd我们必须在里面再更改一件事/etc/httpd/conf/httpd.conf:
<Directory "var/www/html">
Options +ExecCGI
AddHandler cgi-script .cgi .pl
</Directory>
Run Code Online (Sandbox Code Playgroud)
根据许多谷歌搜索,您可能还需要修改httpd.conf文件的这一部分,使其指向与上述相同的目录"var/www/html":
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
Run Code Online (Sandbox Code Playgroud)
我没有修改我的,它似乎没有什么不同。
更改权限以允许 CGI 执行
而这个让我卡住了很多!不要忽视这一点!
您需要告诉您的服务器这些 CGI 脚本允许作为程序执行。
chmod 705 *.cgi
Run Code Online (Sandbox Code Playgroud)
或者您可以针对单个 CGI 脚本。
chmod 705 hello.cgi
Run Code Online (Sandbox Code Playgroud)
(有些人在网上纷纷表示,chmod 755并777可能同样可行。)
现在httpd像这样重新启动:
sudo systemctl restart httpd.service
Run Code Online (Sandbox Code Playgroud)
在这一点上,我的 CGI 脚本正确地呈现为 HTML。
它们是从 web 根目录提供的,如下所示: http://<IP>/hello.cgi
请随时添加可能对其他人有帮助的更多信息。