23

我尝试按照一些关于设置 mod_python 的谷歌搜索教程进行操作,但每次都失败了。你有一个好的,一步一步的,坚如磐石的howto吗?

我的开发盒是 OS X,生产 - Centos。

4

4 回答 4

12

在 Apache 上运行 Python 有两种主要方式。最简单的方法是使用 CGI 并编写普通的 Python 脚本,而第二种方法是使用 Django 或 Pylons 等 Web 框架。

使用 CGI 很简单。确保您的 Apache 配置文件设置了 cgi-bin。如果没有,请遵循他们的文档(http://httpd.apache.org/docs/2.0/howto/cgi.html)。此时,您需要做的就是将 Python 脚本放在 cgi-bin 目录中,标准输出将成为 HTTP 响应。有关更多信息,请参阅 Python 的文档 ( https://docs.python.org/library/cgi.html )。

如果你想使用 web 框架,你需要设置 mod_python 或 FastCGI。这些步骤取决于您要使用的框架。Django 提供了关于如何使用 Apache 设置 mod_python 和 Django 的清晰说明(http://www.djangoproject.com/documentation/modpython/

于 2008-08-07T18:40:53.337 回答
10

Yes, mod_python is pretty confusing to set up. Here's how I did it.

In httpd.conf:

LoadModule python_module modules/mod_python.so

<Directory "/serverbase/htdocs/myapp">
  AddHandler mod_python .py
  PythonHandler myapp
  PythonDebug On

and in your application directory:

$ /serverbase/htdocs/myapp$ ls -l
total 16
-r-xr-xr-x 1 root sys        6484 May 21 15:54 myapp.py

Repeat the configuration for each python program you wish to have running under mod_python.

于 2008-08-07T19:02:57.850 回答
5

Are you running Python on UNIX or Windows?

An alternative to mod_python and FastCGI is mod_wsgi. You can find out more at modwsgi

I have built and installed this on Solaris without problems. I had previously tried mod_python but ran into problems with shared libraries as part of the build. There are good install docs available.

于 2008-08-07T19:05:58.310 回答
0

对我来说,问题不在于 Apache 的设置,而在于了解 mod_apache 如何实际使用 .py 文件。执行模块级语句(包括if __name__=='__main__'部分中的语句)——我假设在命令行运行脚本的标准输出将是服务器输出的内容,但这不是它的工作方式。

相反,我编写了一个名为 的模块级函数index(),并让它以字符串的形式返回页面的 HTML。也可以有其他模块级函数(例如,otherFunction()),可以作为 URI 中的进一步段访问(例如,testScript/otherFunction对于文件testScript.py。)

显然,这比我最初的标准输出概念更有意义。更好地实际使用 Python 作为脚本语言而不是庞大的标记语言。

于 2013-02-09T20:05:07.307 回答