Skip to content

Instantly share code, notes, and snippets.

@manashmandal
Forked from mcescalante/instructions.md
Created June 13, 2017 04:27
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save manashmandal/c0df2a97234591effc46399d3e0bfefd to your computer and use it in GitHub Desktop.
Save manashmandal/c0df2a97234591effc46399d3e0bfefd to your computer and use it in GitHub Desktop.
Run a Flask application with gunicorn and nginx

This guide assumes:

  • You have a VPS (something like DigitalOcean, Linode, etc.)
  • You have a Flask application and a basic understanding of command line instructions
  • You've got nginx installed and know where your configuration files are

Instructions

  1. First, you should install gunicorn on your box or virtualenv with pip:
  • source venv/bin/activate (optional, if you're not using virtualenv)
  • pip install gunicorn
  1. Add a configuration block to your nginx configuration:

Note: change 127.0.0.1:5000 to the port that your flask application is set to run on.

server {
  listen 80;
  server_name example.com;
  
  keepalive_timeout 5;
  
  location / {
    proxy_pass http://127.0.0.1:5000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

}
  1. Restart nginx nginx -s reload

  2. Make locations for the gunicorn logs, and start your application:

Note: change 127.0.0.1:5000 to match your nginx block if you used a different port, and change app:app if you've got a different name

  • mkdir /var/log/gunicorn/
  • gunicorn --access-logfile /var/log/gunicorn/access_log --error-logfile /var/log/gunicorn/error_log --pythonpath /var/www/yourapppath -b 127.0.0.1:5000 app:app
  1. Done. Your flask application should be running on the domain you set in the nginx configuration

Notes

  • Don't forget to run this with screen or supervisord or some other init daemon so that the application doesn't shut down when you exit the shell. Yes, adding & at the end will keep it running, but it's the least elegant way
  • sudo killall -HUP gunicorn gracefully restarts workers if you change any of your python code and want to restart the application
  • Feel free to comment with questions or anything else and I can expand on it :)
@arpit0515
Copy link

I tried the code and it worked flawlessly! I want to stop the current processes and seems that daemon processes are created which are constantly running, how to stop them? I tried removing the .py files which were used to run the app, its still running. I am also not able to access the folder which had those files, please help. Thanks!

@cognitiveRobot
Copy link

Hello Manash,
I am stuck. Can you please help?

My app is crashing only at redirect point (i.e. @return redirect(url_for(‘login’)) when I run using gunicorn(i.e. $gunicorn — bind 127.0.0.1:5000 -w 4 wsgi:app). Same issue when I am running using supervisor as you described in this article. But, the app is working fine when I run normally(i.e. python app.py).

Appreciate any suggestions.

Regards

Hossain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment