- 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
- 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
- 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;
}
}
- Make locations for the gunicorn logs, and start your application:
Note: change 127.0.0.1:5000 to the correct 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
- Restart nginx
nginx -s reload
- Done. Your flask application should be running on the domain you set in the nginx configuration