您的位置:首页 > 运维架构 > Linux

Centos: Python CGI Script environment deployment

2016-05-24 20:47 453 查看

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 a
cgi-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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python cgi server