我要遵循哪些步骤来运行股票 Hello World CGI 脚本?

Jje*_*jed 7 lamp development apache2 webapp-development

假设我已经安装了现代版本的 Ubuntu,并且有以下脚本。

#!/bin/sh
echo "Content-type: text/html\n"
echo "<html><body>Hello world</body></html>"
Run Code Online (Sandbox Code Playgroud)

如何安装和配置 Apache 以使用此脚本从http://localhost提供“Hello world”网页?

Bar*_*osa 11

快捷方式

安装 Apache Web 服务器

在命令行(终端)应用程序中:

sudo apt-get install apache2
Run Code Online (Sandbox Code Playgroud)

使您的脚本可由 Apache 执行

对于此示例,我们假设 CGI 脚本已命名test.sh并保存在您的个人主文件夹中。要使用 Apache test.sh,脚本首先需要具有可执行权限:

chmod 755 $HOME/test.sh
Run Code Online (Sandbox Code Playgroud)

它还需要移动到 Apache 指定的 CGI 文件夹。对于 Ubuntu(和其他基于 Debian 的操作系统),这是 /usr/lib/cgi-bin/.html 和其他内容存储在/var/www.

sudo mv $HOME/test.sh /usr/lib/cgi-bin/
Run Code Online (Sandbox Code Playgroud)

访问本地服务器上的输出

Apache 将test.shhttp://localhost/cgi-bin/test.sh提供生成的 HTML 。如果 Apache 位于您自己机器之外的某个地方,请将“localhost”替换为服务器的 URL 或 IP 地址。

将 CGI 脚本存储在 /var/www/test-cgi 而不是 /usr/lib/cgi-bin/

您需要修改 vanilla Apache 配置以将 CGI 脚本存储在另一个文件夹中。

sudoedit /etc/apache2/sites-enabled/000-default
Run Code Online (Sandbox Code Playgroud)

<VirtualHost>指令中添加以下内容:

ScriptAlias /test-cgi/ /var/www/test-cgi/
<Directory "/var/www/test-cgi">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
</Directory>
Run Code Online (Sandbox Code Playgroud)

然后,在命令行中:

sudo mkdir /var/www/test-cgi
sudoedit /var/www/test-cgi/test.sh
Run Code Online (Sandbox Code Playgroud)

然后,在命令行中,重新启动 Apache:

sudo apachectl restart
Run Code Online (Sandbox Code Playgroud)

The Quick Way 一样,CGI 输出应位于http://localhost/test-cgi/test.sh(如果您进行远程开发,请将“localhost”替换为服务器域)。

故障排除

如果遇到问题,请查阅Apache 官方文档