Apache set-up with Flask and wsgi

2019-09-09 08:12发布

问题:

I have a small web application that I have built using Flask and python. With the internal server that I used for developing everything runs fine. However now I want to use apache to start using it. But it doesn`t work. Keep in mind that I have never worked with apache or web based stuff before.

I used this guide as my starting point: http://flask.pocoo.org/docs/deploying/mod_wsgi/

right now I have my application which is in the file called "/rg/server.py" and looks like this:

app=Flask(__name__)
# all app routes...

if __name__ == '__main__':
    app.run(
        debug=True,
        host="127.0.0.1",
        port=80
    )

than I have a wsgi file as "/rg/wsgi/minerva.wsgi"

import sys
sys.path.insert(0, /rg)
from server import app as minerva

and finally I have an apache config file in "etc/apach2/sites-available/minerva.com":

<VirtualHost *>
    ServerName minerva.test

    WSGIDaemonProcess minerva threads=10
    WSGIScriptAlias / /rg/wsgi/minerva.wsgi

    <Directory /rg>
    WSGIProcessGroup minerva
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
    </Directory>

</VirtualHost>

Then I updated apache with a2ensite minerva.com which succeded. Then I releaded Apache and no errors. However I cannot acces minerva.test in any way...

If I type in apache2ctl -S it does list minerva.test

I have no idea what is going wrong...

system information: OS: debian 64bit python 2.7

回答1:

The WSGI application entry point must be called 'application' for mod_wsgi. You have:

from server import app as minerva

It should be:

from server import app as application

You aren't even getting that far though, else the line:

sys.path.insert(0, /rg)

would give a syntax error.

Going back further, instead of:

<VirtualHost *>

you should have:

<VirtualHost *:80>

and finally, if 'minerva.test' isn't actually a resolvable host, you will not get anywhere.

So fill out your question with the actual URL you are using in the browser and also indicate whether 'minerva.test' is even listed in local hosts file.



回答2:

The first thing I would check would be to make sure mod_wsgi is installed and loaded by apache. If that's fine, your setup looks pretty similar to mine with a few minor differences:

  1. I had to add WSGISocketPrefix /var/run/wsgi above my VirtualHost definitoin. See here.
  2. I included user and group values on the WSGIDaemonProcess line.