Web服务器CGI的配置

Web服务器CGI的配置

CGI程序运行在Web服务器端,Web服务器可以是Apache,Nginx等

GGI程序可以是Python,Ruby,Perl,Shell,C/C++等

结构原理如下

配置

apache默认加载cgi模块(若没加载,先加载):

LoadModule cgi_module modules/mod_cgi.so

修改httpd.conf配置中的以下内容:


#1.在Options中添加ExecCGI的选项

<Directory "D:/wamp/www/">
  ...
    Options Indexes FollowSymLinks ExecCGI
    ...
</Directory>

#2.在AddHandler中添加可执行的程序类型(.py)

    # AddHandler allows you to map certain file extensions to "handlers":
    # actions unrelated to filetype. These can be either built into the server
    # or added with the Action directive (see below)
    #
    # To use CGI scripts outside of ScriptAliased directories:
    # (You will also need to add "ExecCGI" to the "Options" directive.)
    #
    AddHandler cgi-script .cgi .py .pl

注意:以下的内容部分不需要修改
<Directory "D:/wamp/bin/apache/apache2.4.9/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

编写CGI程序

  • Python CGI
#!D:\Anaconda2\python.exe
print "Content-type:text/html"
print                           # 空行,告诉服务器头部结束
print '<html>'
print '<head>'
print '<meta charset="utf-8">'
print '<title>Hello Word - 我的第一个 CGI 程序!</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! 我是来自菜鸟教程的第一CGI程序</h2>'
print '</body>'
print '</html>'

# ### 注意:
# 1,第一行Python的执行路径一定要写对
# 2,第二行和第三行必不可少

参考

PythonCGI编程

Wamp支持Python CGI

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页