yt-summer 发表于 2018-6-15 06:41:21

在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。
  但是,有时候我们可能会遇到:
     > > C:/Program
  > > Files/Apache Group/Apache2/cgi-bin/test.py is not executable; ensure
  > > interpreted scripts have "#!" first line
  > > (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
页: [1]
查看完整版本: 在windows下配置apache以cgi方式支持python