Categories

编译支持cgi的apache

在有的系统下编译apache时不会自动增加cgi支持,所以需要手工指定一下。今天被这个问题坑了一个小时。

1. 安装 httpd 2.4.3

官方下载地址 http://httpd.apache.org/download.cgi
在configure时加一个选项–enable-cgi来打开apache的cgi支持

tar xzf httpd-2.4.3.tar.gz
cd httpd-2.4.3
./configure --prefix=/users/ghtang/lamp/apache --enable-so --enable-vhost-alias --enable-rewrite --enable-deflate --enable-mods-shared=all --enable-cgi
make
make install

2. 修改配置文件 apache/conf/httpd.conf

#(1) 找到下面这一行,把前面的'#'删掉,让apache加载cgi_module
#LoadModule cgi_module modules/mod_cgi.so
 
#(2) 找到类似下面这一个block
<Directory "/some/path/apache/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>
 
#改成
<Directory "/some/path/apache/cgi-bin">
    AllowOverride None
    Options +ExecCGI
    Order allow,deny
    Allow from all
    AddHandler cgi-script .py   #让apache处理以.py结尾的cgi程序
    AddHandler default-handler .html .htm
    Require all granted
</Directory>

3. 编写python cgi脚本

编写python脚本,保存为hello.py

#!/usr/bin/env python
#coding:utf8
 
print "Content-Type: text/html\n\n"
 
print """\
<html>
<head><title>First Python HTTP Programming </title>
<meta charset="utf8" content="text/html" http-equiv="content-type" />
</head>
<body>
<h2>Hello candy!</h2>
</body>
</html>
"""

给hello.py加上执行权限,然后把它放到cgi-bin目录中

chmod u+x hello.py
mv hello.py /some/path/to/apache/cgi-bin/

访问 http://localhost/cgi-bin/hello.py 就能看到python脚本的执行结果了

4. 参考资料

[1]. http://stackoverflow.com/questions/10835436/my-browser-show-the-content-of-my-perl-cgi-script-instead-of-execute

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  

*