Centos: Python CGI Script environment deployment

Introduction:

Common Gateway Interface (CGI) is a standard way for web servers to interface with executable programs installed on a server that generate web pages dynamically. Such programs are known as CGI scripts or simply CGIs; they are usually written in a scripting language, but can be written in any programming language such as python, perl, shell, c or c++ etc.

Deployment:

A Web server that supports CGI can be configured to interpret a "URL" that it serves as a reference to a CGI script. A common convention is to have acgi-bin/ directory at the base of the directory tree and treat all executable files(for example, chmod 755  var/www/cgi-bin/**.cgi), within this directory (and no other, for security) as CGI scripts. Another popular convention is to use filename extentions; for instance, if CGI scripts are consistently given the extension .cgi, the web server can be configured to interpret all such files as CGI scripts. While convenient, and required by many prepackaged scripts, it opens the server to attack if a remote user can upload executable code with the proper extension.

CGI architecture:


Web server configuration:

Apache: 2.4.6

By default, the CGI directory alias has been set to "/var/www/cgi-bin".

Then, edit the apache configuration file "httpd.conf"(usually in the /etc directory) as follows:

     pre-modified:

         <Directory "/var/www/cgi-bin">
                AllowOverride None
                Options None
                Require all granted
        </Directory>

     post-modified:

        <Directory "/var/www/cgi-bin">
                AllowOverride None
                Options +ExecCGI
                Require all granted
        </Directory>

Last, find the line "#AddHandler cgi-script .cgi" and omit the "#", append the " .py"(Notice the whitespace) or " .pl"(if perl),

Restart the apache server to make the modification working.

Writing a Python CGI Script:

Example:(hello.py)

#!/usr/bin/python
# -*- coding: UTF-8 -*-

print "Content-type:text/html"
print                               # Null line,tell the server that the header is ending.
print '<html>'
print '<head>'
print '<meta charset="utf-8">'
print '<title>Hello Word, My first Python CGI Script!</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word, My first Python CGI Script!</h2>'
print '</body>'
print '</html>'
   Precaution: the first line must be here, which specifies the python execution directory. Or, the apache may response "500 Error", "Internal Server Error".

If you have modified the default CGI directory, the script must be put in the correspondent directory.(by default, it should be in the "/var/www/cgi-bin")

Lastly, to modify the permission of the script to let the server have the right to run it.

  chmod 755 hello.py

Conclusion:

Assuring your apache server has been started, then input the address "http://localhost/cgi-bin/hello.py" to make sure your job is well done.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值