Linux Python apache的cgi配置

一、找到安装Apache的目录/usr/local/apache2/conf,并对httpd.conf配置文件进行修改

1.加载cgi模块

去掉注释:

LoadModule cgid_module modules/mod_cgid.so  #//当使用内置模块prefork.c 时动态加载cgi_module

LoadModule cgi_module modules/mod_cgi.so   #当使用内置模块worker.c 时动态加载cgid_module

2.设置cgi脚本文件路径

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

3.设置cgi路径的访问权限

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

4.设置apache可解释python的cgi脚本文件

AddHandler cgi-script .cgi .py

这些配置好后,重启apaache

二、添加CGI脚本文件

在/var/www/cgi-bin/目录下,创建hello.py文件,添加如下代码,复制给他chmod 755 hello.py 的权限

#!/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>'

三步,通过浏览器访问

localhost/cgi-bin/hello.py

参考文档:http://xpleaf.blog.51cto.com/9315560/1740221

http://www.runoob.com/python/python-cgi.html

原文地址:https://www.cnblogs.com/myvic/p/6821368.html