Mac配置apache cgi服务

2.打开apache
[cpp] view plain copy
  1. apachectl start  

3.在浏览器输入
[cpp] view plain copy
  1. localhost:8080  
如果得到 It’works,说明apache运行成功
brew安装的apache默认端口是8080,mac自带的apache默认端口是80
 
4.用sublime打开httpd.conf文件,当然用任意一个文本编辑器打开都行
[cpp] view plain copy
  1. subl /usr/local/etc/apache2/2.4/httpd.conf  
  2. 当然没有sublime的话,可以用图形化界面打开  
  3. open /usr/local/etc/apache2/2.4/  
  4. 在Finder中用一个文本编辑器打开httpd.conf  

5.修改httpd.conf文件
[cpp] view plain copy
  1. 修改成:  
  2. DocumentRoot "/Users/deng/Sites”  
  3. <Directory "/Users/deng/Sites">  
  4. ScriptAlias /cgi-bin/ "/Users/deng/Sites/cgi/"    //放在Directory外
  5.   
  6. 如果注释了下面三句,就取消注释  
  7. LoadModule cgi_module libexec/apache2/mod_cgi.so

  8. AddType text/html .shtml  
  9. AddOutputFilter INCLUDES .shtml  
  10.   
  11. 在文件最后加上以下内容
  12. AddHandler cgi-script .cgi .sh .pl  
  13.   
  14. <Directory "/Users/deng/Sites/cgi/">  
  15.     Options ExecCGI  
  16.     AllowOverride None  
  17.     Order deny,allow  
  18.     Allow from all  
  19. </Directory>  

6.给Sites和Sites/cgi权限
[cpp] view plain copy
  1. chmod +x /Users/deng/Sites  
  2. chmod +x /Users/deng/Sites/cgi  

7.重启Apache
[cpp] view plain copy
  1. apachectl restart  

8.在/Users/deng/Sites/cgi放入测试文件
文件名: first.pl
[cpp] view plain copy
  1. #!/usr/bin/perl  
  2.   
  3. =head1 DESCRIPTION  
  4.   
  5. printenv — a CGI program that just prints its environment  
  6.   
  7. =cut  
  8. print "Content-type: text/html\n\n";  
  9.   
  10. for my $var ( sort keys %ENV ) {  
  11.  printf "<h2>%s = \"%s\"<h2>\n", $var, $ENV{$var};  
  12. }  
需要给 first.pl权限
[cpp] view plain copy
  1. chmod +x /Users/deng/Sites/cgi/first.pl  

9.尝试在终端运行first.pl
[cpp] view plain copy
  1. /Users/deng/Sites/cgi/first.pl  
如果有输出,说明可以运行。

10.在浏览器中打开
[cpp] view plain copy
  1. 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协议格式
[cpp] view plain copy
  1. Content-Type:text/html  
  2.   
  3. body  

头和主体之间有一个空行
 
apache输出的日志信息
[cpp] view plain copy
  1. /usr/local/var/log/apache2/access_log    访问apache的请求在这个文件都可找到  
  2. /usr/local/var/log/apache2/error_log     所有非200 OK的错误信息都会在这个文件找到  
posted @ 2017-08-01 14:27  dzldzl  阅读(656)  评论(0编辑  收藏  举报