- 修改apache的配置文件,位置:
/private/etc/apache2/httpd.conf
- 去掉前面的#
LoadModule cgi_module libexec/apache2/mod_cgi.so
- 修改CGI目录
#ScriptAliasMatch ^/cgi-bin/((?!(?i:webobjects)).*$) "/Library/WebServer/CGI-Executables/$1"
ScriptAlias /catch/ "/Users/myweb/html/catch/"
- 修改原CGI目录的Directory
<Directory "/Users/myweb/html/catch">
AllowOverride None
Options +ExecCGI
Order allow,deny
Allow from all
</Directory>
- 添加尾缀
AddHandler cgi-script .cgi .py .pl .sh
- 保存重启apache
sudo apachectl restart
- 注意脚本解释器的声明
#!/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 - 我的第一个 CGI 程序!</title>'
print '</head>'
print '<body>'
print '<h2>Hello World! 我是第一个CGI程序</h2>'
print '</body>'
print '</html>'
#!/usr/bin/python
,申明这个脚本,需要的解释器的位置
- 文件权限修改
cd /Users/myweb/html/catch
chmod 755 hipy.py
- 网页运行
http://localhost/hipy
参考资料:http://www.runoob.com/python/python-cgi.html