Setting up Flask to work with Nginx
-
After struggling for a few hour last night to get a working hello world with Flask and Nginx I think I should get a little guide up to make it a bit more clear. There are a few tutorials on various blog sites but non of them seemed to work correctly. Some had conflicting information and other had bits of code or config files that did not seem relevant. Even the Flask documentation was hard to understand and didn’t seem to work.
I assume that you already have Nginx, Python and pip installed.
Flask
Flask is a lightweight web framework for Python. It is based on Werkzeug and Jinja 2.
It is easy to install and run. You must have python and pip installed first before you can install Flask.
To install you just runpip install Flask
from your command line.For a quick demo, create a hello.py and copy in the following code:
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run()
Then type
python hello.py
and visit http://localhost:5000 to see your first Flask hello world app.More information can be found on the Flask Website
I have installed Flask in a virtual environment. To do this you will need to run
pip install virtualenv
.
Create your directory for you app and runvirtualenv venv
, venv can be any name you want but I kept it simple.
To activate the virtual environment runsource venv/bin/activate
. This will activate your virtual environment. From here you will install Flask with the same command as above,pip install Flask
. If you have created thehello.py
file from above you should be able to run this from within the virtual environment and check http://localhost:5000 to see it running.
Rundeactivate
from within the virtual environment to deactivate it.uWSGI
Nginx is a web server that serves static files. It is unable to serve a python application. This is where uWSGI comes in. You will require
build-essential
andpython-dev
for uWSGI.
Runpip install uwsgi
to install uWSGI.Open up your config file in the sites-available directory inside your Nginx installation and copy in the following:
server { listen 80; server_name localhost; charset utf-8; client_max_body_size 75M; location / { try_files $uri @yourapplication; } location @yourapplication { include uwsgi_params; uwsgi_pass unix:/var/www/demoapp/demoapp_uwsgi.sock; } }
Changing the path to your apps path.
You will no need to create the .ini file for you application. Create a
yourapp.ini
file in the directory your application is located in.
Copy in the following:[uwsgi] #application's base folder base = /var/www/demoapp #python module to import app = hello module = %(app) home = %(base)/venv pythonpath = %(base) #socket file's location socket = /var/www/demoapp/%n.sock #permissions for the socket file chmod-socket = 666 #the variable that holds a flask application inside the module imported at line #6 callable = app #location of log files logto = /var/log/uwsgi/%n.log
Make sure to change the paths to be the correct paths for your app location.
Create the log file directory.Now run
uwsgi --ini /path/to/your/yourapp.ini
.Now if you open your domain name that is linked to your app you should see that the app has loaded and “Hello World!” is displayed.
This is a quick and dirty memory dump on what I did last night to get it running on my server. Hopefully it is easy enough to follow and is useful to some people.