Apache运行python cgi程序

Apache运行python cgi程序

环境

  1. win10 x64 专业版
  2. Apache2.4
  3. python 2.7

Apache安装和配置

Apache服务器的安装请自行搜索。在Apache2.4中默认加载了cgi模块在httpd.conf的103行左右

LoadModule cgi_module modules/mod_cgi.so

在httpd.conf的389行附近检查cgi文件目录的访问属性,默认不需要修改:

<Directory "${SRVROOT}/cgi-bin">
	AllowOverride None
	Options None
	Require all granted
</Directory>

在httpd.conf的末尾加上如下配置,在Apache2.4的配置文件中没有这行配置,在其他版本
中可能存在该配置,请使用文件的全文搜索查看。

ScriptInterpreterSource Registry

缺少这行配置会出现500的访问错误,如下图:

500错误

python的CGI程序

python 的CGI程序就是一个python脚本文件,请参考 Python CGI

CGI程序代码:

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

print "Content-type:text/html"
print
print '<html>'
print '<head>'
print '<title>Hello</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '</body>'
print '</html>'

验证CGI程序

在浏览器中输入 http://localhost:8080/cgi-bin/hello.py 浏览得到如下结果:

结果

原文地址:https://www.cnblogs.com/xueye9/p/5773564.html