Deploy Flask Application

Deploy Flask Application

how to serve flask application with Nginx and gunicorn?

Flask is a light-weight web framework for Python that includes several utilities and libraries you can use to create a web application. After you have developed a Flask application in a local environment, you need to prepare the application’s production environment in order to run the application and serve it to the users of the application through the internet.in this article we going to Serve a Flask Application in 3 Step.

Step 1 : Server

  1. A server with ubuntu server and Root privileges to the server
  2. A domain name configured to point to your server (example.com)
  3. install nginx 
apt install nginx

4. Python

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

Step 2 : Flask

  1. upload your flask application to the server :

for more information about Flask Application Setup

Flask Application Path:

/var/www/example.com/[Flask_App]

nano /var/www/example.com/MyApp.py

from flask import Flask
MyApp = Flask(__name__)

@MyApp.route("/")
def welcome():
    return "<h1>Welcome To Flask!</h1>"

if __name__ == "__main__":
    MyApp.run(host=YOUR_SERVER_IP, port=PORT)

2. Virtualenv or pipenv

Install Virtualenv in python2:

pip install virtualenv

Install Virtualenv in python3:

pip3 install virtualenv

Create Virtualenv:

virtualenv <your virtualenv>

if you are using pipenv

install pipenv

cd /var/www/example.com/

//this command create .venv to current directory
$ export PIPENV_VENV_IN_PROJECT="enabled"

$ pipenv shell 


//to install project Pipfile
$ pipenv install 



install gunicorn and wheel :


// if not using pipenv 
$ pip install gunicorn  wheel


// if using pipenv
$ pipenv install gunicorn wheel


3. wsgi :

nano /var/www/example.com/wsgi.py


from MyApp import MyApp
if __name__ == "__main__":
    MyApp.run()


run this command to start Application :

gunicorn --bind SERVER_IP_ADDRESS:PORT wsgi:MyApp

your app going to be serve on SERVER_IP_ADDRESS and PORT ; remember that before start gunicorn run this command :
ufw allow PORT

press ctrl+c and exit

Systemd Unit File

nano /etc/systemd/system/myapp.service

Remember to replace the username and project paths with your own information:

[Unit]
Description=Gunicorn instance to serve myapp
After=network.target
[Service]
User=majid
Group=www-data
WorkingDirectory=/var/www/example.com
Environment="PATH=/var/www/example.com/.venv/bin"
ExecStart=/var/www/example.com/.venv/bin/gunicorn  --workers 3 --bind unix:myapp.sock -m 007 wsgi:MyApp
[Install]
WantedBy=multi-user.target


systemctl start myapp

systemctl enable myapp

systemctl status myapp

step 3 : Nginx

Configuring Nginx to Proxy Requests

nano /etc/nginx/sites-available/example.com

server {
server_name  example.com www.example.com;
location / {
     include proxy_params;
     proxy_pass https://unix://var/www/example.com/myapp.sock;
     proxy_set_header   X-Real-IP $remote_addr;
     proxy_set_header   Host      $http_host;
     proxy_http_version 1.1;
     proxy_set_header Connection "";
 }
}


ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

systemctl restart nginx 

Now you should be able to navigate to your server’s domain name in your web browser.

If you encounter any errors, trying checking the following:

  • less /var/log/nginx/error.log: checks the Nginx error logs.
  • less /var/log/nginx/access.log: checks the Nginx access logs.
  • journalctl -u nginx: checks the Nginx process logs.


要查看或添加评论,请登录

Majid Ahmaditabar的更多文章

  • Strategy Design?Pattern

    Strategy Design?Pattern

    Strategy is a behavioral design pattern that lets you define a family of algorithms, put each of them into a separate…

    4 条评论
  • SOLID Principles

    SOLID Principles

    In object-oriented computer programming, SOLID is a mnemonic acronym for five design principles intended to make…

  • Blockchain Developer

    Blockchain Developer

    A blockchain developer is a type of software developer who works with blockchain technology. Blockchain is a…

    2 条评论
  • Personal Websites

    Personal Websites

    1. Include: Your Elevator Pitch Right off the bat, when people land on your site, you want them to understand who you…

  • (PMI Project Management Institute)What is Project Management?

    (PMI Project Management Institute)What is Project Management?

    More specifically, what is a project? It's a temporary endeavor undertaken to create a unique product, service or…

  • 5 e-commerce Trends to Watch in 2015

    5 e-commerce Trends to Watch in 2015

    The distinction between retailer and etailer is becoming less defined, as more consumers turn to the web for product…

社区洞察

其他会员也浏览了