一、找到安裝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