Setup flask +gunicorn+nginx on ubuntu 18.04
#1 install packages
apt update
apt upgrade -y
apt install python3 python3-pip gunicorn3 nginx
#2 flask with gunicorn
pip3 install flask
mkdir -p /var/www/flask/gunicorn_hello_world
cd /var/www/flask/gunicorn_hello_world
nano hello.py
from flask import Flask
app = Flask( __name__ )@app.route(‘/’)
def index():
return “Hello from gunicorn-Flask!”
gunicorn3 hello:app -b 0.0.0.0:18081
open browser-> ip:18081 make sure you can see hello message
#3 now setup nginx
rm /etc/nginx/sites-enabled/default
prepare: /etc/nginx/sites-enabled/reverse-proxy.conf
server {
listen 80;
listen [::]:80;
server_name _;
access_log /var/log/nginx/reverse-access.log;
error_log /var/log/nginx/reverse-error.log;location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_pass http://127.0.0.1:18081;
proxy_redirect off;
}
}
systemctl restart nginx
#4 now stop gunicorn3 process in terminal and configure supervisor
sudo apt-get install -y supervisor
nano /etc/supervisor/conf.d/gunicorn_hello.conf
[program:gunicorn_hello]
command = gunicorn3 hello:app -b 0.0.0.0:18081
user=your_ubuntu_user
directory = /var/www/flask/gunicorn_hello_world
sudo supervisorctl reread
sudo supervisorctl update
supervisorctl restart gunicorn_hello
#5 Test
open browser -> http://ip_of_server should see ‘hello’ message
Done