在windows下配置apache以cgi方式支持python

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文件,來確定問題。


運行http://127.0.0.1/cgi-bin/test.py

wKiom1Quy5SSlXgHAAAj9TfcTKs924.jpg

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章