3

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 [email protected]
    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.

4
  • Why do you think you need to rewrite the URLs? The WSGIScriptAlias should serve your application directly, without any need for rewriting. Commented Dec 18, 2013 at 9:33
  • I am using this tutorial to set things up. codero.com/knowledge-base/questions/316/… I am would like the url not to show the code.py. Commented Dec 18, 2013 at 19:38
  • @Jeremy: That's where WSGIScriptAlias comes in; it takes a root URL and uses your WSGI script to server that URL. code.py will never be part of the URL in such a configuration. Commented Dec 20, 2013 at 23:59
  • @MartijnPieters I do not want code.py to show up in my URL. This all works except that the parameter that is passed to my python script is the file path to the script. I can not figure out why this is happening. Commented Dec 21, 2013 at 10:26

1 Answer 1

2
+50

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 [email protected]
    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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.