Skip to main content

Run Flask apps as services and manage them with NGINX

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

Popular posts from this blog

Use CS50 Library in Your Local Machine Offline (C Language Setup)

Use CS50 Library in Your Local Machine Offline (C Language Setup) Make Your PC Ready for C Programming with CS50 In this guide, you will learn how to prepare your computer to run C programs locally and use the CS50 library offline. The following videos will walk you through all required tools and setup steps. Step 1: Install Visual Studio Code This video explains how to download and install Visual Studio Code on Windows 10. Step 2: Install C/C++ Compiler (GCC, G++, GDB) Learn how to install the C/C++ toolchain using MinGW-w64 and MSYS2. Step 3: Configure VS Code for C/C++ This step shows how to properly configure Visual Studio Code for C and C++ programming. How to Use CS50 Library Offline After completing the setup above, follow these steps to use the CS50 library locally without internet access: Download the CS50 library from GitHub Releases: https://github.com/cs50/libcs50/rel...

Install Windows 11 on a bootable USB using Linux

 Install Windows 11 on a bootable USB using Linux 1 Download the iso image of Windows from the offical website for Microsoft 2 Open terminal and run these commands to install WoeUSB sudo add-apt-repository ppa:tomtomtom/woeusb sudo apt update sudo apt install woeusb 3 Plugin the USB into the device, and run this command lsblk Assume your USB is /dev/sdb 4 Format it to NTFS or FAT32: sudo mkfs.ntfs -f /dev/sdb # or sudo mkfs.vfat -F 32 /dev/sdb 5 Unmount the USB sudo umount /dev/sdb* 6 Burn the ISO file into the USB sudo woeusb --device /path/to/windows.iso /dev/sdb 7 Remove the USB if the files are burned successfuly and it can be used as bootable to install Windows.