Apache配置实现cgi


装完Apache,需要修改apache的配置文件:
nano /etc/httpd/conf/httpd.conf
配置文件中有
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"这个指令,指示了默认的cgi-bin的路径。
/var/www/cgi-bin/,在配置文件中也可以看到。
还需要配置
AddHandler cgi-script .cgi .pl
该配置默认被注释掉了,设置了cgi的后缀名
编写一个cgi文件,内容如下:
#!/bin/bash
echo "Content-type: text/html"
echo ""
echo "Hello World"

注意,该文件必须是要UNIX文件格式的,可以editpro等工具编写。将该文件命名为first.cgi,然后放入/var/www/cgi-bin/目录中。
然后运行http://localhost:9000/cgi-bin/first.cgi,可以发现,有错误

Apache配置实现cgi

有错误不可怕,默认的httpd的错误日志在/var/log/httpd/error_log里可以看到,也可以去http的软连接中访问/etc/httpd/logs/error_log
打开error_log文件,可以看到如下的错误:
[Thu Jan 22 09:06:54 2015] [error] [client 192.168.6.2] (13)Permission denied: exec of '/var/www/cgi-bin/first.cgi' failed
[Thu Jan 22 09:06:54 2015] [error] [client 192.168.6.2] Premature end of script headers: first.cgi
说明了权限被拒绝了。将first.cgi的权限设置成755
chmod 755 /var/www/cgi-bin/first.cgi
再次运行http://localhost:9000/cgi-bin/first.cgi,发现就正常显示了
同样的,可以编辑一个perl文件,也可以以cgi的方式运行
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello, World.";

-------------------------------------我是分割线 -------------------------------------

Ubuntu下Apache的Rewrite如何启用 

Ubuntu 14.04中Apache 2.2升级到2.4后的几个要点

Ubuntu 13.04 安装 LAMP\Vsftpd\Webmin\phpMyAdmin 服务及设置

CentOS 5.9下编译安装LAMP(Apache 2.2.44+MySQL 5.6.10+PHP 5.4.12)

RedHat 5.4下Web服务器架构之源码构建LAMP环境及应用PHPWind

LAMP源码环境搭建WEB服务器Linux+Apache+MySQL+PHP

本文永久更新链接地址:

相关内容