Mac配置apache cgi服务
2.打开apache
3.在浏览器输入
- apachectl start
3.在浏览器输入
- localhost:8080
如果得到 It’works,说明apache运行成功
brew安装的apache默认端口是8080,mac自带的apache默认端口是804.用sublime打开httpd.conf文件,当然用任意一个文本编辑器打开都行
5.修改httpd.conf文件
6.给Sites和Sites/cgi权限
7.重启Apache
需要给 first.pl权限
9.尝试在终端运行first.pl
如果有输出,说明可以运行。
- subl /usr/local/etc/apache2/2.4/httpd.conf
- 当然没有sublime的话,可以用图形化界面打开
- open /usr/local/etc/apache2/2.4/
- 在Finder中用一个文本编辑器打开httpd.conf
5.修改httpd.conf文件
- 修改成:
- DocumentRoot "/Users/deng/Sites”
- <Directory "/Users/deng/Sites">
- ScriptAlias /cgi-bin/ "/Users/deng/Sites/cgi/" //放在Directory外
- 如果注释了下面三句,就取消注释
-
LoadModule cgi_module libexec/apache2/mod_cgi.so
- AddType text/html .shtml
- AddOutputFilter INCLUDES .shtml
- 在文件最后加上以下内容
- AddHandler cgi-script .cgi .sh .pl
- <Directory "/Users/deng/Sites/cgi/">
- Options ExecCGI
- AllowOverride None
- Order deny,allow
- Allow from all
- </Directory>
6.给Sites和Sites/cgi权限
- chmod +x /Users/deng/Sites
- chmod +x /Users/deng/Sites/cgi
7.重启Apache
- apachectl restart
8.在/Users/deng/Sites/cgi放入测试文件
文件名: first.pl
- #!/usr/bin/perl
- =head1 DESCRIPTION
- printenv — a CGI program that just prints its environment
- =cut
- print "Content-type: text/html\n\n";
- for my $var ( sort keys %ENV ) {
- printf "<h2>%s = \"%s\"<h2>\n", $var, $ENV{$var};
- }
- chmod +x /Users/deng/Sites/cgi/first.pl
9.尝试在终端运行first.pl
- /Users/deng/Sites/cgi/first.pl
10.在浏览器中打开
- localhost:8080/cgi-bin/first.pl
注意:
1.404 not fount
可能是DocumentRoot没有设置对,或者ScriptAlias /cgi-bin/ 没设置对,或者没有对应的文件
2.403 forbidden
我遇到这个问题是因为 /cgi-bin/ 不在 DocumentRoot的子目录下。
还有一个可能是 没有给 /cgi-bin/还有里面的脚本执行权限
3.500 Internal Server Error
是脚本输出的格式不符合http1.1协议格式
- Content-Type:text/html
- body
头和主体之间有一个空行
apache输出的日志信息
- /usr/local/var/log/apache2/access_log 访问apache的请求在这个文件都可找到
- /usr/local/var/log/apache2/error_log 所有非200 OK的错误信息都会在这个文件找到