Apache配置
在httpd.conf中查找DocumentRoot:
允許目錄可以執行cgi:
DocumentRoot "D:\WWW" <Directory /> Options Indexes FollowSymLinks MultiViews ExecCGI AllowOverride All Order allow,deny Allow from all Require all granted </Directory>
只允許在特別目錄下執行cgi程序:
ScriptAlias /cgi-bin/ "D:/Program/phpStudy/Apache/cgi-bin/"
讓apache識別py文件爲cgi程序:
AddHandler cgi-script .cgi .py
以上就配置完成了apache。
但是,有時候我們可能會遇到:
> > [Sat Apr 01 13:30:03 2006] [error] [client 127.0.0.1] C:/Program
> > Files/Apache Group/Apache2/cgi-bin/test.py is not executable; ensure
> > interpreted scripts have "#!" first line
> > [Sat Apr 01 13:30:03 2006] [error] [client 127.0.0.1] (9)Bad file
> > descriptor: don't know how to spawn child process: C:/Program Files/Apache
> > Group/Apache2/cgi-bin/test.py
這個時候可以嘗試在配置文件結尾添加:'ScriptInterpreterSource Registry'
下面開始寫個例子程序:
test.py
#!D:\Program\Python27\python.exe print "Content-type: text/html" print print "<h1>hello,world!</h1>"
寫cgi程序要注意,
第一:#!前面不能有空格,後面緊跟解釋程序;
第二,python等解釋程序的目錄是否正確;
第三,作爲http協議的要求,一定要輸出http headers;
第四,在存在http headers的前提下,一定要在headers後面打印一個空行,否則服務器會報錯;
第五,把錯誤的程序在python的idle中執行一下,驗證正確性;
最後,實在搞不定的情況下,查看apache的logs文件夾下的error.log文件,來確定問題。