如何设置Python脚本在Apache 2.0中工作?

时间:2022-10-06 10:08:55

I tried to follow a couple of googled up tutorials on setting up mod_python, but failed every time. Do you have a good, step-by step, rock-solid howto?

我尝试按照几个谷歌搜索关于设置mod_python的教程,但每次都失败了。你有一个好的,一步一步,坚如磐石的指导方针吗?

My dev box is OS X, production - Centos.

我的开发盒是OS X,制作 - Centos。

4 个解决方案

#1


12  

There are two main ways of running Python on Apache. The simplest would be to use CGI and write normal Python scripts while the second is using a web framework like Django or Pylons.

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

Using CGI is straightforward. Make sure your Apache config file has a cgi-bin set up. If not, follow their documentation (http://httpd.apache.org/docs/2.0/howto/cgi.html). At that point all you need to do is place your Python scripts in the cgi-bin directory and the standard output will become the HTTP response. Refer to Python's documentation for further info (https://docs.python.org/library/cgi.html).

使用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)。

If you want to use a web framework you'll need to setup mod_python or FastCGI. These steps are dependent on which framework you want to use. Django provides clear instructions on how to setup mod_python and Django with Apache (http://www.djangoproject.com/documentation/modpython/)

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

#2


9  

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

是的,mod_python设置相当混乱。这是我如何做到的。

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.

对你希望在mod_python下运行的每个python程序重复配置。

#3


5  

Are you running Python on UNIX or Windows?

你在UNIX或Windows上运行Python吗?

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

mod_python和FastCGI的替代方法是mod_wsgi。您可以在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.

我在Solaris上构建并安装了这个没有问题。我以前尝试过mod_python但是在构建过程中遇到了共享库的问题。有很好的安装文档可用。

#4


0  

The problem for me wasn't in Apache set up, but in understanding how mod_apache actually uses the .py files. Module-level statements (including those in a if __name__=='__main__' section) are not executed--I assumed that the stdout from running the script at the commandline would be what the server would output, but that's not how it works.

对我来说问题不是在Apache设置中,而是在理解mod_apache如何实际使用.py文件。模块级语句(包括if __name __ =='__ main__'部分中的语句)不会被执行 - 我假设在命令行运行脚本的stdout将是服务器输出的内容,但这不是它的工作原理。

Instead, I wrote a module-level function called index(), and had it return as a string the HTML of the page. It's also possible to have other module-level functions (e.g., otherFunction()) that can be accessed as further segments in the URI (e.g., testScript/otherFunction for the file testScript.py.)

相反,我编写了一个名为index()的模块级函数,并将其作为字符串返回页面的HTML。也可以使用其他模块级函数(例如,otherFunction()),这些函数可以作为URI中的其他段进行访问(例如,文件testScript.py的testScript / otherFunction)。

Obviously, this makes more sense than my original stdout conception. Better capability of actually using Python as a scripting language and not a humongous markup language.

显然,这比我原来的stdout概念更有意义。实际上使用Python作为脚本语言的能力更强,而不是一种非常棒的标记语言。

#1


12  

There are two main ways of running Python on Apache. The simplest would be to use CGI and write normal Python scripts while the second is using a web framework like Django or Pylons.

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

Using CGI is straightforward. Make sure your Apache config file has a cgi-bin set up. If not, follow their documentation (http://httpd.apache.org/docs/2.0/howto/cgi.html). At that point all you need to do is place your Python scripts in the cgi-bin directory and the standard output will become the HTTP response. Refer to Python's documentation for further info (https://docs.python.org/library/cgi.html).

使用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)。

If you want to use a web framework you'll need to setup mod_python or FastCGI. These steps are dependent on which framework you want to use. Django provides clear instructions on how to setup mod_python and Django with Apache (http://www.djangoproject.com/documentation/modpython/)

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

#2


9  

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

是的,mod_python设置相当混乱。这是我如何做到的。

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.

对你希望在mod_python下运行的每个python程序重复配置。

#3


5  

Are you running Python on UNIX or Windows?

你在UNIX或Windows上运行Python吗?

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

mod_python和FastCGI的替代方法是mod_wsgi。您可以在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.

我在Solaris上构建并安装了这个没有问题。我以前尝试过mod_python但是在构建过程中遇到了共享库的问题。有很好的安装文档可用。

#4


0  

The problem for me wasn't in Apache set up, but in understanding how mod_apache actually uses the .py files. Module-level statements (including those in a if __name__=='__main__' section) are not executed--I assumed that the stdout from running the script at the commandline would be what the server would output, but that's not how it works.

对我来说问题不是在Apache设置中,而是在理解mod_apache如何实际使用.py文件。模块级语句(包括if __name __ =='__ main__'部分中的语句)不会被执行 - 我假设在命令行运行脚本的stdout将是服务器输出的内容,但这不是它的工作原理。

Instead, I wrote a module-level function called index(), and had it return as a string the HTML of the page. It's also possible to have other module-level functions (e.g., otherFunction()) that can be accessed as further segments in the URI (e.g., testScript/otherFunction for the file testScript.py.)

相反,我编写了一个名为index()的模块级函数,并将其作为字符串返回页面的HTML。也可以使用其他模块级函数(例如,otherFunction()),这些函数可以作为URI中的其他段进行访问(例如,文件testScript.py的testScript / otherFunction)。

Obviously, this makes more sense than my original stdout conception. Better capability of actually using Python as a scripting language and not a humongous markup language.

显然,这比我原来的stdout概念更有意义。实际上使用Python作为脚本语言的能力更强,而不是一种非常棒的标记语言。