Mac 10.8.5 上运行cgi

搭配置搭了好久,花了近半天时间,有必要把过程记录下。

本文已同步到 icocoa.tk, 欢迎访问

Mountain Lion上Setting设置里已经取消了web share,必须要自己启动apache服务。启动命令:

//启动
sudo apachectl start
//重启
sudo apachectl restart//

然后在用户主目录下找到Site目录,这里用来放置html。

这里我输入后,无论访问http://localhost/还是http://localhost/~username/index.html都不能访问。

后来我想起我安装了xampp,通过xampp的控制台开启apache,则可以访问上述地址。猜测xampp的某些设置影响了系统原有的设置。于是,果断卸载了xampp。这时,重启下apache,果然可以访问了。

接下来,要修改apache的一些配置,来开启cgi。

编辑apache配置文件:/etc/apache2/httpd.conf,取消注释:
AddHandler cgi-script .cgi (1)
AddType text/html .shtml (2)
AddOutputFilter INCLUDES .shtml(3)
(1)是描述对什么样的文件视为cgi文件,用户可添加,比如添加 perl文件:
AddHandler cgi-script .cgi .pl
(2)和(3)是表示允许服务端返回的文件内容格式、和包含的输出文件;

检查cgi_module是否被注释掉了:
LoadModule cgi_module modules/mod_cgi.so


编辑文件:/etc/apache2/users/yourusername.conf
<Directory "/Users/yourusername/Sites/">
Options Indexes FollowSymLinks MultiViews ExecCGI
DirectoryIndex index.html index.cgi
AllowOverride None
Order allow,deny
Allow from all
</Directory>
这里是给Sites目录设置属性,ExecCGI就是开启cgi。接下来就可以把cgi或pl文件放置在Sites目录下任何地方了。

最后赋予脚本文件可执行权限。

所有上述的都完成后,需要重启apache。如果这时,cig还是不能访问,跳出:

Forbidden

You don't have permission to access /~username/test.cgi on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request

那么,你需要做的就是重启你的mac!!(我就是在这里花费太多时间!!)

关于CGI开启部分 主要参考了:
mac下 apache cgi 配置
CGI Programming With Apache and Perl on Mac OS X

Apache Tutorial: Dynamic Content with CGI

 

原文地址:https://www.cnblogs.com/scorpiozj/p/3458941.html