webapp2 under Apache (= without Google App Engine)

痞子三分冷 提交于 2019-12-03 16:46:11
Danny Hong

Install mod_wsgi from http://code.google.com/p/modwsgi/wiki/InstallationOnWindows and configure your httpd.conf properly.

I assume you have already added these 2 lines:

LoadModule wsgi_module modules/mod_wsgi.so
WSGICallableObject app

Install py-setuptools from http://pypi.python.org/pypi/setuptools then install Modules for your python

easy_install WebOb
easy_install Paste
easy_install webapp2

Create virtualhost

<VirtualHost *>
  ServerAdmin admin@mydomain.com
  DocumentRoot "/vhost/domains/mydomain/htdocs"
  ServerName a.mydomain.net
  WSGIScriptAlias / "/vhost/domains/mydomain/wsgi/main.py"
  Alias /static/ "/vhost/domains/mydomain/htdocs/static/"
</VirtualHost>

File: 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) You need to install webapp2, WebOb, Paste prerequisites modules on hosting environment using pip or easy_install

2) Create wsgi.py file under website’s root folder (/var/www/website/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) Create apache2 configuration file under sites-available folder (/etc/apache2/sites-available/website.conf)

<VirtualHost *:80>
    ServerName website
    WSGIScriptAlias / "/var/www/ website /wsgi.py"
</VirtualHost>

4) Add “website” alias to “/etc/hosts” file.

5) Run following command to enable “/etc/apache2/sites-available/website.conf”

a2ensite website.conf

6) Reload and restart apache2 web server

service apache2 reload 
/etc/init.d/apache2 restart

7) Apache web-server will automatically load “website” configuration on restart webapp2.WSGIApplication instance will point to mod_wsgi "application".

Please note above example is tested on an Ubuntu 13.10 operating system.

Have you not tried:

 WSGICallableObject app

You could also change your code to say:

application = webapp2.WSGIApplication([('/', MainPage)], debug=True)

and avoid needing to tell mod_wsgi to look for a different name.

I haven't tried it myself just yet, but have you created another Python module, say runme.py, with the following code:

def main():
  run_wsgi_app(yourmodule.app)
if __name__ == '__main__':
  main()

(Note: I got this from https://developers.google.com/appengine/docs/python/python27/migrate27#wsgi

Got it! The line

app = webapp2.WSGIApplication([('/', MainPage)], debug=True)

should be:

app = webapp2.WSGIApplication([('/Python/hello.py', MainPage)], debug=True)

and then everything works! Arghh!

Many thanks to Graham for patiently pushing me in the right direction: The problem was indeed within the bounds of webapp2, as soon as WSGICallableObject was set to "app"!

For the benefit of anyone being stuck on a similar routing problem with webapp2: Check out http://webapp-improved.appspot.com/guide/routing.html. The first example on "simple routes" made me rewrite my call to webapp.WSGIApplication within minutes!

Update

Unfortunately, the above solution doesn't seem to be reliable: Today, I sometimes got a correct response from webapp2, and sometimes I got a 404 from webapp2.

Without changing a single line of code since yesterday.

I can't reproduce under what condition I get the 404 or the correct response. I am giving up on this for now. Which is sad, since I think that Python is such a cool language.

@Graham: Again, thanks for your help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!