Apache配置
在httpd.conf中查找DocumentRoot:
+ExecCGI 支持cgi
DocumentRoot "F:\phpStud\PHPTutorial\WWW" <Directory /> Options +Indexes +FollowSymLinks +ExecCGI AllowOverride All Order allow,deny Allow from all Require all granted </Directory>
讓apache識別py文件為cgi程序:
AddHandler cgi-script .cgi .pl .py
指定目錄下執行cgi程序
ScriptAlias /cgi-bin/ "F:/phpStud/PHPTutorial/Apache/cgi-bin/"
重啟 apache, from.py 文件內容
#!D:\python\python.exe #python 運行程序位置 #coding=utf-8 print ("Content-type:text/html") print () # 空行,告訴服務器結束頭部 print ('<html>') print ('<head>') print ('<meta charset="utf-8">') print ('<title>Hello Word - 我的第一個 CGI 程序!</title>') print ('</head>') print ('<body>') print ('<h2>Hello Word! 我是來自菜鳥教程的第一CGI程序</h2>') print ('</body>') print ('</html>')
運行文件存放位置
F:\phpStud\PHPTutorial\Apache\cgi-bin\from.py
瀏覽器訪問地址:
http://localhost/cgi-bin/from.py
cgi 運行文件注意要點
第一:#! 前面不能有空格,后面緊跟解釋程序;
第二,python等解釋程序的目錄是否正確;
第三,作為http協議的要求,一定要輸出http headers;
第四,在存在http headers的前提下,一定要在headers后面打印一個空行,否則服務器會報錯;
第五,把錯誤的程序在python的idle中執行一下,驗證正確性;
最后,實在搞不定的情況下,查看apache的logs文件夾下的error.log文件,來確定問題。