Question

I believe I have Apache setup correctly with mod_wsgi and Rewrite Engine. I'm using web.py to serve up content. The test "Hello World" app works but the output includes the file root. Looks like this:

Hello, /var/www/example.com/application/!

I've included the config and code.

Here is the apache config:

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName foodcost.mynetwork.inside
    ServerAlias foodcost.mynetwork.inside
    DocumentRoot /var/www/example.com/public_html/
    ErrorLog /var/www/example.com/logs/error.log
    CustomLog /var/www/example.com/logs/access.log combined
    WSGIScriptAlias / /var/www/example.com/application/

    Alias /static /var/www/example.com/public_html

    <Directory /var/www/example.com/application>
      SetHandler wsgi-script
      Options ExecCGI
      Options +FollowSymLinks
    </Directory>

    AddType text/html .py

    <Location />
      RewriteEngine on
      RewriteBase /
      RewriteCond %{REQUEST_URI} !^/static
      RewriteCond %{REQUEST_URI} !^(/.*)+code.py/
      RewriteRule ^(.*)$ code.py/$1 [PT]
    </Location>
</VirtualHost>

Python Code:

import web

urls = (
    '(.*)', 'hello'
)

app = web.application(urls, globals(), autoreload=False)
application = app.wsgifunc()

class hello:
    def GET(self, name):
        if not name:
            name = 'World'
        return 'Hello, ' + name + '!'

if __name__ == "__main__":
    app.run()

Update: After playing around with the Rewrite I have come to discover that the problem is with RewriteRule ^(.*)$ code.py/$1 [PT]. The $1 (parameter) passes the root of where the python script is running plus whatever the rest of the URL is from the root url.

So an example of this would be URL:

http://{rootURL}/tom

Output:

Hello, /var/www/example.com/application/tom!

I can't figure out why the directory location of the python script is being passed in.

Was it helpful?

Solution

Following the tutorial at http://webpy.org/cookbook/mod_wsgi-apache, I believe you really don't need to rewrite URLs. Your apache config is then:

<VirtualHost *:80>

    ServerAdmin admin@example.com
    ServerName foodcost.mynetwork.inside
    ServerAlias foodcost.mynetwork.inside
    DocumentRoot /var/www/example.com/public_html/
    ErrorLog /var/www/example.com/logs/error.log
    CustomLog /var/www/example.com/logs/access.log combined

    WSGIScriptAlias / /var/www/example.com/application/code.py/
    Alias /static /var/www/example.com/public_html

    <Directory /var/www/example.com/application>
      SetHandler wsgi-script
      Options ExecCGI
      Options +FollowSymLinks
    </Directory>

    AddType text/html .py

</VirtualHost>

(among other changes, note the added code.py/ to the WSGIScriptAlias directive.)

And the file /var/www/example.com/application/code.py is:

import web

urls = (
    '/(.*)', 'hello'
)

app = web.application(urls, globals(), autoreload=False)
application = app.wsgifunc()

class hello:
    def GET(self, name):
        if not name:
            name = 'World'
        return 'Hello, ' + name + '!'

if __name__ == "__main__":
    app.run()

(note the added slash in the urls tuple in the beginning.

This way there's no "code.py" showing up:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top