简体   繁体   English

配置apache以运行python(cgi)

[英]Configure apache to run python (cgi)

i've wampserver with apache 2.4.4 installed in 我已经在安装了Apache 2.4.4的wampserver中

I've installed python and i created a test file : 我已经安装了python并且创建了一个测试文件:

#!/Python34/python
print "Content-type: text/html"
print
print "<html><head>"
print ""
print "</head><body>"
print "Hello."
print "</body></html>"

i wanna know how to run this script ? 我想知道如何运行此脚本?

I personally don't like the way CGI and all this stuff work (slow to spawn processes, need to use some kind of tricks like "fastcgi" to bypass this, etc...) 我个人不喜欢CGI和所有这些东西的工作方式(缓慢生成进程,需要使用诸如“ fastcgi”之类的技巧来绕开它,等等。)

I think you may build your Python programm as an HTTP server (Use cherrypy for example, or whatever you want.), start your Python program to listen on localhost:whatever, then, from the Apache side, just configure a proxy to localhost:whatever. 我认为您可以将Python程序构建为HTTP服务器(例如,使用cherrypy或您想要的任何东西),启动Python程序以监听localhost:无论如何,然后从Apache端,只需将代理配置为localhost:随你。

Pros: 优点:

  • No need for apache to fork a Python process each requests (An expensive operation) 无需apache派生每个请求的Python进程(昂贵的操作)
  • You'll change your web server easily (like switch to Nginx) as nginx also support proxying. 您将轻松更改Web服务器(例如切换到Nginx),因为nginx还支持代理。
  • You'll be able to start multiple Python servers and load balance between them 您将能够启动多个Python服务器并在它们之间进行负载平衡
  • You'll be able to host your python servers on different host to load balance charge 您将能够在其他主机上托管python服务器,以实现负载平衡
  • You'll be able to completly bypass Apache if you put a Varnish in front of your app to cache results. 如果将Varnish放在应用程序前面以缓存结果,则可以完全绕过Apache。
  • Cherrypy can automatically reload files if they are changed, no need to restart apache. Cherrypy可以在更改文件后自动重新加载文件,而无需重新启动apache。
  • You'll stick to HTTP, no need to use protocols like fastcgi. 您将坚持使用HTTP,无需使用诸如fastcgi之类的协议。
  • Easy to test on your dev machine without Apache, just point your browser to your localhost:whatever 无需Apache即可轻松在您的开发机上进行测试,只需将浏览器指向本地主机即可:

Configuring apache 2 to pass your requests to your python daemon is as easy as: 配置apache 2将您的请求传递给python守护程序很简单:

<VirtualHost *:80>
    ServerName example.com
    ProxyPass / http://localhost:8080/
</VirtualHost>

And an hello world from the cherrypy documentation: 以及来自cherrypy文档的世界:

import cherrypy

class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True

cherrypy.quickstart(HelloWorld())

+1 to what Julien Palard says about not using CGI, it's really slow and inefficient. +1关于Julien Palard关于不使用CGI的说法,这确实很慢而且效率很低。 An alternative to running your server standalone and proxying through to it using Apache is to use mod_wsgi, which allows you to run Python processes inside the Apache process. 独立运行服务器并使用Apache代理服务器的另一种方法是使用mod_wsgi,它允许您在Apache进程内部运行Python进程。 Most web frameworks (Django, Bottle, Flask, CherryPy, web2py, etc) work well with it. 大多数Web框架(Django,Bottle,Flask,CherryPy,web2py等)都可以很好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM