Deploy Flask With Gunicorn On Manjaro

Deploying a Python project is not easy...

Especially in Arch Linux and Manjaro...

I finally got this set up reading the nginx - ArchWiki and Gunicorn documentation...

mkdir flaskproject
cd flaskproject
python -m venv venv
pip install flask
pip install gunicorn

myproject.py
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
return 'Hello World'

if __name__ == '__main__':
app.run(host='0.0.0.0')

wsgi.py
from myproject import app

if __name__ == '__main__':
app.run()

nano /etc/systemd/system/myproject.service
[Unit]
Description=Gunicorn instance to serve myproject
After=network.target

[Service]
User=action
Group=http
WorkingDirectory=/usr/share/nginx/html/flaskproject
Environment='PATH=/usr/share/nginx/html/flaskproject/venv/bin'
ExecStart=/usr/share/nginx/html/flaskproject/venv/bin/gunicorn --workers 4 --bind
unix:myproject.sock -m 007 wsgi:app

[Install]
WantedBy=multi-user.target

nano /etc/nginx/sites-available
server {
listen 80;
server_name 127.0.0.1;

location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://unix:/usr/share/nginx/html/flaskproject/myproject.sock;
}
}

sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/
sudo systemctl restart nginx
sudo systemctl restart myproject
Back