笔者用的是phpstudy(Apache)+ windos ,一开始不理解Apache各个配置的含义(网上很多配置文章都没提到),导致配置老是出错,特总结一下。
Apache配置
在httpd.conf中查找DocumentRoot:
允许目录可以执行cgi:
# 查找一下自己的DocumentRoot
DocumentRoot "E:\Environment\phpstudy\PHPTutorial\WWW"
<Directory />
AllowOverride All
Options +ExecCGI
Order allow,deny
Allow from all
#Require all granted
</Directory>
#让apache识别py文件为cgi程序:
AddHandler cgi-script .cgi .py
只允许在特别目录下执行cgi程序:
ScriptAlias /cgi-bin/ "D:/Program/phpStudy/Apache/cgi-bin/"
以上就配置完成了apache。
测试
下面开始写个例子程序:
#!C:\软件\Python\python.exe
print ("Content-type:text/html")
print () # 空行,告诉服务器结束头部
print ('<html>')
print ('<head>')
print ('<meta charset="utf-8">')
print ('<title>Hello World</title>')
print ('</head>')
print ('<body>')
print ('<h2>Hello World! - 我的第一个 CGI 程序!</h2>')
print ('</body>')
print ('</html>')
写cgi程序注意
第一:#!前面不能有空格,后面紧跟解释程序(python.exe的路径);
第二,python等解释程序的目录是否正确;
第三,作为http协议的要求,一定要输出http headers;
第四,在存在http headers的前提下,一定要在headers后面打印一个空行,否则服务器会报错;
第五,把错误的程序在python的idle中执行一下,验证正确性;
最后,实在搞不定的情况下,百度 + 查看apache的logs文件夹下的error.log文件,来确定问题。
参考链接
http://blog.51cto.com/8796902/1560549
--------------------- 本文来自 晴晨ldl 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/qingchenldl/article/details/79598712?utm_source=copy