Apache 中运行 Python CGI 程序

栏目: Python · 发布时间: 7年前

内容简介:Web 程序我还是喜欢用 Apache 来作为入口,因为我用的系统是 Mac OS, Apache 它就静静的躺上那儿了。最好是都像 PHP 那样的程序,无需启动额外的服务,这种使用方式的缺陷是不太适合于做微服务。这儿呢,我也是来探索如何在 Apache 中运行 Python 的 CGI 程序,这主要是涉及了 Apache 与 Python 的集成。集成方式有直接集成(mod_python),CGI,FastCGI, WSGI 和 uWSGI。本文主要是讲如何运行一个简单的 Python CGI。虽然由于

Web 程序我还是喜欢用 Apache 来作为入口,因为我用的系统是 Mac OS, Apache 它就静静的躺上那儿了。最好是都像 PHP 那样的程序,无需启动额外的服务,这种使用方式的缺陷是不太适合于做微服务。

这儿呢,我也是来探索如何在 Apache 中运行 Python 的 CGI 程序,这主要是涉及了 Apache 与 Python 的集成。集成方式有直接集成(mod_python),CGI,FastCGI, WSGI 和 uWSGI。本文主要是讲如何运行一个简单的 Python CGI。

直接集成(mod_python)

虽然由于多方面的安全因素,这已经成为历史了,但还是提一下。参见

  1. Apache HTTP Server/mod_python (它推荐使用 mod_wsgi , 以下该节相关内容摘在本链接,没有亲自尝试)
  2. mod_python 官网 (最后的更新是 2013-11-13)

需要安装模块 mod_python.so,并在 httpd.conf 加上相关的配置

LoadModule python_module modules/mod_python.so
 
<Directory /home/www/html> 
   AddHandler mod_python .py
   PythonHandler mod_python.publisher 
   PythonDebug On 
</Directory>

程序代码示例(创建在 /home/www/html/mptest.py)

from mod_python import apache
def handler(req):
    req.content_type = 'text/plain'
    req.send_http_header()
    req.write("Hello World!")
    return apache.OK

通过 http://localhost/mptest.py/handler 来访问,内空肯定是 Hello World 了。相信 mod_python 也提供了不少处理请求参数的函数。

CGI

这是本文希望重点强调的部份。本人所使用的系统是 mac OS High Sierra, 自带的 Apache 是

httpd -v  Server version: Apache/2.4.33 (Unix)  Server built: Apr 3 2018 23:45:11

不同版本的 Apache 配置上会略有不同,随后稍有提及,Linux 发行版下的 Apache 请作参考。

最简单的配置方式是,打开 /etc/apache2/httpd.conf 文件,找到下行并移除起始的注释符号 # , 最终是

LoadModule cgi_module libexec/apache2/mod_cgi.so

Mac 下的 libexec/apache2 的绝对目录是 /usr/libexec/apache2

不同系统下可能要启用的模块是 mod_cgid.so , 它与 mod_cgi.so 在配置上没什么差别, d 代表 Daemon 。不启用该模块的话,访问的脚本文件将不被解释器执行,而是直接显示源代码去客户端。

mod_cgimod_cgid 可以支持系统任意的脚本,如 sh , perl , ruby 等,反正都是根据第一行的指示交给相应的脚本解释器,最后获得输出。

默认目录放置 Python 脚本

在 Mac 下的只需要在目录 /Library/WebServer/CGI-Executables 目录下创建文件 hello.py , 内容如下

#!/usr/local/bin/python3
 
print("Context-type:text/html")
print("")
 
print("<h2>Hello Python CGI</h2>")

这是依照 HTTP 的标准响应格式,头与体之间空行分隔。注意 Shebang 注释行(第一行),要指定 python 3 解释器的路径,爱用 python 2 也无妨。但是用 #!/usr/bin/env python3 却不行,只适于运行为 shell。

hello.py 文件必须是可执行,用以下命令修改

sudo chown +r /Library/WebServer/CGI-Executables/hello.py

重启 Apache

sudo apachectl -k restart    #-k 可检查配置中的语法错误

测试

curl -i http://localhost/cgi-bin/hello.py  HTTP/1.1 200 OK  Date: Sat, 10 Nov 2018 07:21:38 GMT  Server: Apache/2.4.33 (Unix)  Context-type: text/html  Transfer-Encoding: chunked
<h2>Hello Python CGI</h2>

为什么把可执行的 hello.py 丢到目录 /Library/WebServer/CGI-Executables 中就行了呢?因为在 /etc/apache2/httpd.conf 有这样的配置

ScriptAliasMatch ^/cgi-bin/((?!(?i:webobjects)).*$) "/Library/WebServer/CGI-Executables/$1"
 
<Directory "/Library/WebServer/CGI-Executables">
    AllowOverride None
    Options None
    Require all granted
</Directory>

配置自己的 Python 脚本目录

前面是把 hello.py 放置在默认的目录中,我们也可以在自定义的目录中安装 Python CGI 脚本。比如用下面的配置

ScriptAlias /py-cgi/ "/Users/yanbin/python/cgi-bin/"
<Directory "/Users/yanbin/python/cgi-bin/">
    AllowOverride None
    Options +ExecCGI
    Require all granted
</Directory>
AddHandler cgi-script .py   # Mac 下这一行不是必须的

需要事先创建好目录 /Users/yanbin/python/cgi-bin ,然后把先前的 hello.py 放到该目录中。对于 Apache 2.4 之前的版本

Require all granted

要替换成

Order allow,deny  # 逗号后不能用空格
Allow from all

然后重启 Apache 后,访问 http://localhost/py-cgi/hello.py 得到相同的结果。

注:任何 Apache 的访问错误请一定要查看它的错误日志文件, Mac 下的日志文件位置是 /var/log/apache2/error_log , 这个比 Google 都更好使。

FastCGI

这个在 Mac 下现在不好用了,以下命令都失效了

brew install homebrew/apache/mod_fcgid

说是

Error: homebrew/apache was deprecated. This tap is now empty as all its formulae were migrated.
brew install mod_fastcgi 
brew install --with-homebrew-httpd24 mod_fastcgi

上面两个命令都找不到想要安装的组件,倒是有一个 fcgi 不知道是什么

 brew install fcgi

安装之后找不到想要的 mod_fcgid.somod_fcgi.so ,有一个 cgi-fcgi 可执行文件, 以及其他的如 fastcgi.h 和 .a, .dylib 静态,动态库。

Linux 下也许能轻易找到 mod_fcgi 模块,这儿一个链接 Good Idea: Python with FastCGI (mod_fcgid)

Apache, FastCGI and Python 中介绍的, Debian 系列的 Linux 可以用如下命令安装和启用

# apt-get install libapache2-mod-fastcgi
# a2enmod fastcgi

根据 https://github.com/Homebrew/homebrew-core/issues/18732,说是 mod_fastcgimod_fcgi 已经被 mod_proxy_fcgi 替代了,而 Mac 中的  Apache 是带有这个模块的

#LoadModule proxy_fcgi_module libexec/apache2/mod_proxy_fcgi.so

只需要取消注释就能用了。并且提到 mod_wsgi 的安装可以用命令

APXS=/usr/local/bin/apxs pip install mod_wsgi

关于 FastCGI 结合 Python 的方式就不再深入了。接下来会关注 WSGI,以及集成 Flask。从上面看来搭建服务器使用 Mac 比 Linux 更具有挑战性,谁让 Mac Server 版基本没有份额。


以上所述就是小编给大家介绍的《Apache 中运行 Python CGI 程序》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

About Face 3

About Face 3

Alan Cooper、Robert Reimann、David Cronin / John Wiley & Sons / 2007-5-15 / GBP 28.99

* The return of the authoritative bestseller includes all new content relevant to the popularization of how About Face maintains its relevance to new Web technologies such as AJAX and mobile platforms......一起来看看 《About Face 3》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

随机密码生成器
随机密码生成器

多种字符组合密码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器