Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webapp2 under Apache (= without Google App Engine)

I am trying to run webapp2 under Python with Apache and mod_wsgi - specifically: Wampserver for Windows 7 with Apache 2.2.22. So far, I have failed miserably. :-(

I used the following example from 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)

When I save this file as c:wamp\www\Python\hello.py, and browse to localhost/Python/hello.pyI get:

Not Found
The requested URL /python/hello.py was not found on this server.

However, let me state that mod_wsgi for Python within Apache seems to be running fine; the following code

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]

is located at c:\wamp\www\Python\test.py. When I go to localhost/Python/test.py, the browser says Hello from Python! as I would expect.

So far, I have only found out how to change the default name of the def (="application") to "something_else" by putting the line

WSGICallableObject something_else

into .htaccess.

But how can I get Apache to accept the variable app as a callable object? (So far, I have used Python mainly for programming outside of the web, so I hope this is not a dumb question.)

Any help is appreciated.

Update:

Graham asked me about the mod_wsgi configuration I am using in Apache configuration files and where I am adding it. I added

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>

to httpd.conf right at the end of all the "LoadModule" lines.

Some additional info on my configuration: I am using mod_wsgi-win32-ap22py27-3.3.so. (Of course I renamed it to mod_wsgi.so and placed it into c:\wamp\bin\apache\apache2.2.22\modules.) My Python command line says this about the version: Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win 32. The wamp server I am using is 32 bit. My operating system is Windows 7 Ultimate 64bit SP1.

Hope this helps with the diagnosis...

like image 899
Rainer Avatar asked Feb 20 '23 21:02

Rainer


2 Answers

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 [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>

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)
like image 140
Danny Hong Avatar answered Feb 22 '23 10:02

Danny Hong


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.

like image 38
mahifernando Avatar answered Feb 22 '23 10:02

mahifernando