Apache Web 服务器配置CGI程序,执行Python、Perl脚本

来源:互联网 发布:js new cookie 编辑:程序博客网 时间:2024/05/17 23:23

关于什么是Apache、CGI、Python、Perl等问题,请详见百度介绍。


1、apache服务器的下载地址:http://mirror.bit.edu.cn/apache/httpd/ 根据需要的操作系统选择适用的文件,如本人Win7 系统,就下载http://mirror.bit.edu.cn/apache/httpd/binaries/win32/httpd-2.2.25-win32-x86-no_ssl.msi;

2、安装,启动,修改Apache的配置文件 ..\Apache2.2\conf/httpd.conf;

3、内容如下:

ScriptAlias /cgi-bin/ "D:/Apache2.2/cgi-bin/"
# "D:/Apache2.2/cgi-bin" should be changed to whatever your ScriptAliased# CGI directory exists, if you have that configured.#<Directory "D:/Apache2.2/cgi-bin">    AllowOverride None#    Options None   Options ExecCGI    Order allow,deny    Allow from all</Directory>
AddHandler cgi-script .cgi .py .pl .sh
其中,

“D:/Apache2.2/cgi-bin”为本人安装路径,程序会根据安装时的配置自动生成,无需更改;

Options None是默认配置,我们更改为Options ExecCGI,关于Options的参数,在httpd.conf中有详细介绍,还包括其他参数,如Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews None All等;

AddHandler 增加可执行的脚本,根据需求,自己增加。

如上,我们已经完成了conf脚本的配置,接下来我们用简单的Python测试下。

4、编写hello.cgi脚本

#!D:\Python34\python.exeprint ('Content-type: text/plain\n\n<?xml version="1.0" encoding="utf-8"?>\n<result>')print ('Hellp world\n')print ("<B>Hello,World!</B>")print ("</result>")
最上一行,必须有,指定调用哪个目录下python.exe,其他系统自行更改
问题来了?python.exe哪里来的?

这里我们需要安装python,下载地址,以博文撰写时间来说,最新版本有python2.7.8和python3.4.2,自行选择需要,2.x和3.x语法有区别,可看官网的介绍,最直接的如2.x中print "hello",在3.x中必须写成print ("hello")

将hello.cgi拷贝到apache安装目录下cgi-bin文件夹下,重启服务器。

5、打开任意浏览器,输入:http://localhost/cgi-bin/hello.cgi 会看到结果



希望能够帮助你,解决问题,这个过程中还遇到了各种奇葩的问题,不过均一一搞定,如有疑问,请留言,共同进步。


0 0
原创粉丝点击