我试图用Apache和mod_wsgi在Python下运行webapp2 - 具体来说:Wampserver for Windows 7 with Apache 2.2.22。 到目前为止,我失败了。 :-(
我使用了https://developers.google.com/appengine/docs/python/gettingstartedpython27/usingwebapp中的以下示例:
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, webapp World!')
app = webapp2.WSGIApplication([('/', MainPage)],
debug=True)
当我将此文件保存为c:wamp\\www\\Python\\hello.py
,并浏览到localhost/Python/hello.py
我得到:
Not Found
The requested URL /python/hello.py was not found on this server.
但是,让我说Apache中的Python的mod_wsgi似乎运行正常; 以下代码
def application(environ, start_response):
status = '200 OK'
output = 'Hello from Python!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
位于c:\\wamp\\www\\Python\\test.py
当我转到localhost/Python/test.py
,浏览器会Hello from Python!
说出Hello from Python!
正如我所料。
到目前为止,我只是通过放置行找到了如何将def(=“application”)的默认名称更改为“something_else”
WSGICallableObject something_else
进入.htaccess
。
但是,如何让Apache接受变量app
作为可调用对象呢? (到目前为止,我主要使用Python进行网络外的编程,所以我希望这不是一个愚蠢的问题。)
任何帮助表示赞赏。
更新:
Graham问我关于我在Apache配置文件中使用的mod_wsgi配置以及我在哪里添加它。 我补充道
LoadModule wsgi_module modules/mod_wsgi.so
<Directory "c:/wamp/www/python">
Options +ExecCGI
AddHandler wsgi-script .py
Order allow,deny
Allow from all
</Directory>
到所有“LoadModule”行末尾的httpd.conf
。
关于我的配置的一些其他信息:我正在使用mod_wsgi-win32-ap22py27-3.3.so。 (当然我将它重命名为mod_wsgi.so
并将其放入c:\\wamp\\bin\\apache\\apache2.2.22\\modules
。)我的Python命令行说明了这个版本: Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win 32
。 我使用的wamp服务器是32位。 我的操作系统是Windows 7 Ultimate 64bit SP1。
希望这有助于诊断......
从http://code.google.com/p/modwsgi/wiki/InstallationOnWindows安装mod_wsgi并正确配置您的httpd.conf。
我假设你已经添加了这两行:
LoadModule wsgi_module modules/mod_wsgi.so
WSGICallableObject app
从http://pypi.python.org/pypi/setuptools安装py-setuptools然后为你的python安装模块
easy_install WebOb
easy_install Paste
easy_install webapp2
创建虚拟主机
<VirtualHost *>
ServerAdmin [email protected]
DocumentRoot "/vhost/domains/mydomain/htdocs"
ServerName a.mydomain.net
WSGIScriptAlias / "/vhost/domains/mydomain/wsgi/main.py"
Alias /static/ "/vhost/domains/mydomain/htdocs/static/"
</VirtualHost>
文件:main.py
import webapp2
class Hello(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/html; charset=utf-8'
self.response.out.write('hello world!')
application = webapp2.WSGIApplication([
('/', Hello)
], debug=True)
1)您需要使用pip或easy_install在托管环境中安装webapp2,WebOb,粘贴先决条件模块
2)在网站的根文件夹(/var/www/website/wsgi.py)下创建wsgi.py文件。
#/var/www/website/wsgi.py
import webapp2
class Index(webapp2.RequestHandler):
def get(self):
output = 'webapp2 running on apache2'
self.response.headers = [('Content-type','text/plain'),('Content-length',str(len(output)))]
self.response.out.write(output)
application = webapp2.WSGIApplication([('/',Index)], debug=True)
3)在sites-available文件夹下创建apache2配置文件(/etc/apache2/sites-available/website.conf)
<VirtualHost *:80>
ServerName website
WSGIScriptAlias / "/var/www/ website /wsgi.py"
</VirtualHost>
4)将“website”别名添加到“/ etc / hosts”文件中。
5)运行以下命令启用“/etc/apache2/sites-available/website.conf”
a2ensite website.conf
6)重新加载并重新启动apache2 Web服务器
service apache2 reload
/etc/init.d/apache2 restart
7)Apache web-server将在重启webapp2时自动加载“网站”配置.WSGIApplication实例将指向mod_wsgi“application”。
你没试过:
WSGICallableObject app
你也可以改变你的代码来说:
application = webapp2.WSGIApplication([('/', MainPage)], debug=True)
并避免需要告诉mod_wsgi寻找不同的名称。
我还没有尝试过,但你是否创建了另一个Python模块,比如runme.py,其代码如下:
def main():
run_wsgi_app(yourmodule.app)
if __name__ == '__main__':
main()
(注意:我是从https://developers.google.com/appengine/docs/python/python27/migrate27#wsgi获得的
得到它了! 这条线
app = webapp2.WSGIApplication([('/', MainPage)], debug=True)
应该:
app = webapp2.WSGIApplication([('/Python/hello.py', MainPage)], debug=True)
一切正常! Arghh!
非常感谢Graham耐心地向我推进了正确的方向:一旦WSGICallableObject设置为“app”,问题确实在webapp2的范围内!
为了让任何人遇到与webapp2类似的路由问题,请访问: http ://webapp-improved.appspot.com/guide/routing.html。 关于“简单路由”的第一个例子让我在几分钟内重写了对webapp.WSGIApplication
调用!
更新
不幸的是,上面的解决方案似乎并不可靠:今天,我有时会从webapp2得到正确的响应,有时我从webapp2获得了404。
从昨天起不改变任何一行代码。
我无法在什么条件下重现我获得404或正确的响应。 我现在就放弃了。 这很难过,因为我认为Python是一种很酷的语言。
@Graham:再次感谢你的帮助。