Mac配置apache cgi服务

CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如:HTTP服务器,提供同客户端HTML页面的接口

Mac上是自带CGI运行程序的,但是直接是不可以用,需要进行相应的修改才可以正常运行。

1、打开httpd.conf文件,直接打开是编辑不了的,需要用root权限,具体操作为:sudo vim /etc/apache2/httpd.conf打开文件。找到

LoadModule cgi_module libexec/apache2/mod_cgi.so并取消注释。

 

Web 服务器支持及配置

在你进行 CGI 编程前,确保您的 Web 服务器支持 CGI 及已经配置了 CGI 的处理程序。

Apache 支持 CGI 配置:

设置好CGI目录:

ScriptAlias /cgi-bin/ /var/www/cgi-bin/

所有的HTTP服务器执行 CGI 程序都保存在一个预先配置的目录。这个目录被称为 CGI 目录,并按照惯例,它被命名为 /var/www/cgi-bin 目录。

CGI 文件的扩展名为 .cgi,python 也可以使用 .py 扩展名。

默认情况下,Linux 服务器配置运行的 cgi-bin 目录中为 /var/www。

如果你想指定其他运行 CGI 脚本的目录,可以修改 httpd.conf 配置文件,如下所示:

<Directory "/var/www/cgi-bin">
   AllowOverride None
   Options +ExecCGI
   Order allow,deny
   Allow from all
</Directory>

在 AddHandler 中添加 .py 后缀,这样我们就可以访问 .py 结尾的 python 脚本文件:

AddHandler cgi-script .cgi .pl .py

 

2、在AddHandler cgi-script .cgi后面增加.py,表示可以访问.py结尾的python脚本文件,也可以增加.php,表示可以访问.php结尾的脚本文件。(我自己的理解)

3、配置完成后在/Library/WebServer/CGI-Executables下创建hello.py文件,内容如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

print "Content-type:text/html"
print
print '<html>'
print '<head>'
print '<meta charset="utf-8">'
print '<title>Hello World - 这是一个测试程序!</title>'
print '</head>'
print '<body>'
print '<h2>Hello World! 我是用来测试cgi的</h2>'
print '</body>'
print '</html>'
4、文件创建好后修改权限 ,同样需要root权限用户。

sudo chomd 755 hello.py

5、重启appachectl服务

sudo apachectl restart

6、效果如下所示

 

2.打开apache

  1. apachectl start  


3.在浏览器输入

  1. localhost:8080  
如果得到 It’works,说明apache运行成功

brew安装的apache默认端口是8080,mac自带的apache默认端口是80

 
4.用sublime打开httpd.conf文件,当然用任意一个文本编辑器打开都行
  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文件
  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权限
  1. chmod +x /Users/deng/Sites  
  2. chmod +x /Users/deng/Sites/cgi  

7.重启Apache
  1. apachectl restart  

8.在/Users/deng/Sites/cgi放入测试文件
文件名: first.pl
  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权限
  1. chmod +x /Users/deng/Sites/cgi/first.pl  

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

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

头和主体之间有一个空行
 
apache输出的日志信息
  1. /usr/local/var/log/apache2/access_log    访问apache的请求在这个文件都可找到  
  2. /usr/local/var/log/apache2/error_log     所有非200 OK的错误信息都会在这个文件找到  
posted @ 2022-03-22 17:20  hanease  阅读(228)  评论(0编辑  收藏  举报