Deploying Flask With Nginx
The Flask docs for this take you 80% of the way there.
Basically, we need uWSGI to create a UNIX socket, which nginx can route requests to as they come in.
First, let’s make sure uWSGI runs independent of nginx.
The docs tell us to write this:
1
|
|
There’s two problems with this:
First, you may get some error that it doesn’t find flask if you are using a virtual environment (which you should).
Second, nginx may not be able to read the file later if the permissions don’t let it.
So I had to adapt it to this:
1
|
|
Now, install nginx with apt-get install nginx. Change the /etc/nginx/sites-available/default to read:
1 2 3 4 5 6 7 8 9 |
|
Now symlink the file above to the enabled one, because that’s how nginx works:
1
|
|
Start nginx with ‘/etc/init.c/nginx restart’
Go to a browser and try it out. Yay!
But what if you want to restart the server? nginx will restart, but your uWSGI will not. We’ll use supervisord for that.
1
|
|
We need to set up the /etc/supervisord.conf to tell supervisor what to do:
Now, kill your supervisor, start it again, and your Flask app be running ad infinitum.
1 2 3 |
|
Here’s another supervisor sample config file to get you inspired.
Hope this helps.