GithubHelp home page GithubHelp logo

deploy-flask-gunicorn's Introduction

deploy-flask-gunicorn

Deploy Flask on VPS with Gunicorn

Update the local package

sudo apt update

Install Nginx

sudo apt install nginx

Install Package Python

sudo apt install python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools

Creating a Python Virtual Environment

sudo apt install python3-venv

Make dir or Clone project from github

Create Virtual Environment

python3 -m venv myprojectenv

Active the env

source myprojectenv/bin/activate

Setting Flask Application

Install Package

pip install wheel

Install requirements.txt

pip install -r requirements.txt

Allow Access Port you are using

sudo ufw allow 5000

Test Your Flask App

python app.py

You will receive output like the following

Output
 * Serving Flask app 'myproject' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on all addresses.
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://your_server_ip:5000/ (Press CTRL+C to quit)

Visit Your Website

http://your_server_ip:5000

Creating the WSGI Entry Point

Create a new file using your preferred text editor and name it

nano ~/myproject/wsgi.py

import the Flask instance from your application and then run it

from myproject import app

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

Configuring Gunicorn

Next, you can check that Gunicorn can serve the application correctly by passing it the name of your entry point.

gunicorn --bind 0.0.0.0:5000 wsgi:app

You will receive output like the following:

Output
[2021-11-19 23:07:57 +0000] [8760] [INFO] Starting gunicorn 20.1.0
[2021-11-19 23:07:57 +0000] [8760] [INFO] Listening at: http://0.0.0.0:5000 (8760)
[2021-11-19 23:07:57 +0000] [8760] [INFO] Using worker: sync
[2021-11-19 23:07:57 +0000] [8763] [INFO] Booting worker with pid: 8763
[2021-11-19 23:08:11 +0000] [8760] [INFO] Handling signal: int
[2021-11-19 23:08:11 +0000] [8760] [INFO] Shutting down: Master

Visit Your Website

http://your_server_ip:5000

Your are done with your virtual environment

deactivate

create .service

sudo nano /etc/systemd/system/myproject.service

Add a description of your service here

[Unit]
Description=Gunicorn instance to serve myproject
After=network.target

[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/sammy/myproject
Environment="PATH=/home/sammy/myproject/myprojectenv/bin"
ExecStart=/home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 wsgi:app

[Install]
WantedBy=multi-user.target

Now start the Gunicorn service you created

Step 1

sudo systemctl start myproject

Step 2

sudo systemctl enable myproject

Step 3

sudo systemctl status myproject
Output
● myproject.service - Gunicorn instance to serve myproject
   Loaded: loaded (/etc/systemd/system/myproject.service; enabled; vendor preset
   Active: active (running) since Fri 2021-11-19 23:08:44 UTC; 6s ago
 Main PID: 8770 (gunicorn)
    Tasks: 4 (limit: 1151)
   CGroup: /system.slice/myproject.service
       	├─9291 /home/sammy/myproject/myprojectenv/bin/python3.6 /home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 wsgi:app
       	├─9309 /home/sammy/myproject/myprojectenv/bin/python3.6 /home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 wsgi:app
       	├─9310 /home/sammy/myproject/myprojectenv/bin/python3.6 /home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 wsgi:app
       	└─9311 /home/sammy/myproject/myprojectenv/bin/python3.6 /home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 wsgi:app
…

Configuring Nginx to Proxy Requests

sudo nano /etc/nginx/sites-available/myproject
server {
    listen 80;
    server_name your_domain www.your_domain;

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/sammy/myproject/myproject.sock;
    }
}
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx

Delete Access Port

sudo ufw delete allow 5000

Then allow full access to the Nginx server:

sudo ufw allow 'Nginx Full'

Visit Your Website

http://yourdomain

Securing the Application

First, install Certbot using snap

sudo apt install python3-certbot-nginx
sudo certbot --nginx -d your_domain -d www.your_domain
sudo ufw delete allow 'Nginx HTTP'

Visit Your Website

https://yourdomain

Source : https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-18-04

Copyright (c) 2021 Makassar Coding https://makassarcoding.com/

deploy-flask-gunicorn's People

Contributors

otysimanov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.