Run Flask apps as services and manage them with NGINX
sudo apt install python3-pip
sudo apt install nginx
sudo adduser --home=/opt/kobros --disabled-password --gecos "Kobros" kobros
sudo su - kobros
mkdir company_app
cd company_app
python3 -m venv py-env
source py-env/bin/activate
pip3 install gunicorn flask
nano app.py
-------------------------- FILE --------------------------
from flask import Flask
app = Flask(__name__)
@app.route(‘/’)
def hello_world():
return "<center>Hello World! Uooohh Mantaapp</center>"
if __name__ == ‘__main__’:
app.run(debug=True,host=’0.0.0.0')
------------------------------------------------------------
nano wsgi.py
-------------------------- FILE --------------------------
from app import app
if __name__ == “__main__”:
app.run()
------------------------------------------------------------
python3 app.py
gunicorn --bind 0.0.0.0:5000 wsgi:app
deactivate
sudo nano /etc/systemd/system/company_app.service
-------------------------- FILE --------------------------
[Unit]
Description=App to serve my company website
After=network.target
[Service]
User=kobros
Group=kobros
WorkingDirectory=/opt/kobros/company_app
Environment=”PATH=/opt/kobros/company_app/py-env/bin”
ExecStart=/opt/kobros/company_app/py-env/bin/python3 /opt/kobros/company_app/py-env/bin/gunicorn wsgi:app
[Install]
WantedBy=multi-user.target
------------------------------------------------------------
sudo systemctl start company_app
sudo nano /etc/nginx/sites-available/company_app
-------------------------- FILE --------------------------
server {
listen 80;
listen [::]:80;
location /company/ {
include proxy_params;
proxy_pass http://localhost:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /profile/ {
include proxy_params;
proxy_pass http://localhost:8001/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
------------------------------------------------------------
sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/company_app /etc/nginx/sites-enabled
sudo nginx -t
systemctl restart nginx
Comments
Post a Comment