Figuring out how to serve my flask site with virtualenv, uwsgi, and nginx was frustrating. There are plenty of articles on this topic. None of them worked for me on the first try. Here are my notes for Debian Jessie in case someone else is experiencing similar issues.
First make sure that your distro has the latest updates:
sudo apt-get update
Now install python and virtualenv:
sudo apt-get install build-essential python-dev python-pip
Make a folder for your website:
sudo mkdir -p /var/www/mysite
Setup virtualenv and install flask:
virtualenv .env --no-site-packages
Place your flask app in this folder. Make sure that your host is set to 0.0.0.0 and that your app is under if name == ‘main‘:. If your app is in a function, uwsgi will not be able to call it.
Now is a good time to test your app with the flask development server to see if everything is working so far. If everything runs smoothly, install nginx and uwsgi:
Next we must create a socket file for nginx to communicate with uwsgi:
By changing the owner of mysite.sock to www-data, nginx will be able to write to the socket. Now all we have to do is add our configuration files for nginx and uwsgi. First delete the default configuration for nginx:
Create a new configuration file mysite and add the following:
In order to enable the site, we must link our configuration file to /etc/nginx/sites-enabled/:
sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/mysite
The process is similar for uwsgi. Create the file
/etc/uwsgi/apps-available/mysite.ini and add the following:
Module is the name of your python script and callable is the name of your flask instance. So if your flask site was in a file called mysite.py that looked like this:
from flask import Flask
Your mysite.ini file would be:
module = mysite
Link the configuration file to the enabled-apps folder:
sudo ln -s /etc/uwsgi/apps-available/mysite.ini /etc/uwsgi/apps-enabled/mysite.ini
Finally, restart nginx and uwsgi:
sudo service nginx restart
Thats it. If you notice any errors in my guide, please feel free to email me. Here are some tips in case you get stuck:
Check that your flask site runs under virtualenv without any errors.
Ensure that you can run your site with just uwsgi from the command line.
If you run uwsgi with sudo you will change the owner of mysite.sock to root and this will create errors for nginx. Make sure that you change the owner back to www-data.
If uwsgi cannot find your app, you probably have an issue with file permissions. In order to serve the site uwsgi must have executable permissions for your python script and your .env folder.
The logs for nginx and uwsgi are
/var/log/uwsgi/app/mysite.log respectively. If nginx is working properly, you will want to look at