Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

python - Hello World in mod_wsgi

After failing repeatedly in my quest to get my flask application to run on Apache using mod_wsgi I decided to try running the hello world example. Here is what I have -

Directory Structure (I changed the apache default /var/www to ~/public_html)

- public_html    
   - wsgi-scripts
      - test_wsgi.wsgi
   - test_wsgi
      - test_wsgi.wsgi

test_wsgi.wsgi file

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]

    start_response(status, response_headers)

    return [output]

VirtualHost Configuration file (called testwsgi) - This resides in /etc/apache2/sites-enabled/

<VirtualHost *:80>
    DocumentRoot ~/public_html/test_wsgi

    <Directory ~/public_html/test_wsgi>
        Order allow,deny
        Allow from all
    </Directory>

    WSGIScriptAlias /wsgi ~/public_html/wsgi-scripts/test_wsgi.wsgi

    <Directory ~/public_html/wsgi-scripts>
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

When I try going to localhost/wsgi on the browser I get a 404 Not Found error. What am I doing wrong? This is the first time I'm trying to deploy an app on a production server. Until now I took the easy way of using Google App Engine. I cant proceed to deploy my flask app until this is up and running. Thanks a lot!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to use an absolute path, i.e. don't use ~. This works fine for me...

[mpenning@tsunami public_html]$ sudo cat /etc/apache2/sites-available/wsgi_test
<VirtualHost *:80>
    ServerName wsgihost
    DocumentRoot /home/mpenning/public_html
    WSGIScriptAlias / /home/mpenning/public_html/test.wsgi
</VirtualHost>
[mpenning@tsunami public_html]$

First I set up a hostname in /etc/hosts, so I could ensure that I can mux on the hostname in the query...

[mpenning@tsunami public_html]$ grep wsgihost /etc/hosts
127.0.1.1       tsunami.foo.net  tsunami wsgihost
[mpenning@tsunami public_html]$

Restart apache, and issue a wget...

[mpenning@tsunami public_html]$ wget http://wsgihost/
--2012-08-29 05:50:26--  http://wsgihost/
Resolving wsgihost... 127.0.1.1
Connecting to wsgihost|127.0.1.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12 [text/plain]
Saving to: aindex.html.3a

100%[======================================>] 12          --.-K/s   in 0s

2012-08-29 05:50:26 (1.48 MB/s) - aindex.html.3a

[mpenning@tsunami public_html]$ cat index.html
Hello World![mpenning@tsunami public_html]$ #  <------

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...