Mac 10.8.5 上运行cgi

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

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

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

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

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

这里我输入后,不管访问http://localhost/仍是http://localhost/~username/index.html都不能访问。this

后来我想起我安装了xampp,经过xampp的控制台开启apache,则能够访问上述地址。猜想xampp的某些设置影响了系统原有的设置。因而,果断卸载了xampp。这时,重启下apache,果真能够访问了。spa

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

编辑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)是表示容许服务端返回的文件内容格式、和包含的输出文件;rest

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


编辑文件:/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目录下任何地方了。server

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

全部上述的都完成后,须要重启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

相关文章
相关标签/搜索