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
- A server with ubuntu server and Root privileges to the server
- A domain name configured to point to your server (example.com)
- install nginx
apt install nginx
4. Python
apt install python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools
Step 2 : Flask
- 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.