Flask is a simple and light-weight framework for Python. It provides all utilities and libraries that help you to deploy a web application using Python language. Generally, developers are developing a Flak application on a local environment. You may need to deploy the Flask application to the production environment if you want to serve your application to the users over the internet.
In order to fulfil this need, we've written the tutorial below, where we'll show you how to deploy a Python application using Flask and Nginx on Debian 10.
Requirements
- A server running on Debian 10.
- A root password configured in your server.
Install Required Dependencies
First, you will need to install Python and other required dependencies to your system. You can install all of them using the following command:
# apt-get install python3-pip libssl-dev libffi-dev python3-dev build-essential python3-setuptools -y
Once all the packages are installed, install a Python virtual environment package with the following command:
# apt-get install python3-venv -y
Next, upgrade the PIP to the latest version using the following command:
# pip3 install --upgrade pip
You'll also need to install the Nginx webserver to serve the Python application. You can install it using the following command:
# apt-get install nginx -y
Create a Python Application
In this section, we'll install Flask and create a Python application.
First, create a directory for your application using the following command:
# mkdir ~/flaskapp
Next, change the directory to your application and create a Python virtual environment:
# cd ~/flaskapp
# python3 -m venv venv
To go on, activate the virtual environment with the following command:
# source venv/bin/activate
Next, install Flask and Gunicorn with the following command:
# pip install wheel
# pip install gunicorn flask
Now create a simple Python app with the following command:
# nano ~/flaskapp/flaskapp.py
Add the following lines:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "<h1 style='color:DodgerBlue'>Welcome to Python Application!</h1>"
if __name__ == "__main__":
app.run(host='0.0.0.0')
Save and close the file then run your application with the following command:
# cd ~/flaskapp/
# python3 flaskapp.py
You should see the following output:
* Serving Flask app "flaskapp" (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 http://0.0.0.0:5000/ (Press CTRL+C to quit)
Press "CTRL+C" to stop the application.
Configure Gunicorn to Run Your Application
In this section, we will create a wsgi file and configure Gunicorn to run your Python application.
First, create a wsgi.py file:
# nano ~/flaskapp/wsgi.py
Add the following lines:
from flaskapp import app
if __name__ == "__main__":
app.run()
Save and close the file then run your application with Gunicorn:
# cd ~/flaskapp/
# gunicorn --bind 0.0.0.0:5000 wsgi:app
You should see the following output:
(venv) root@debian10:~/flaskapp# gunicorn --bind 0.0.0.0:5000 wsgi:app
[2021-04-21 07:25:03 +0000] [8483] [INFO] Starting gunicorn 20.1.0
[2021-04-21 07:25:03 +0000] [8483] [INFO] Listening at: http://0.0.0.0:5000 (8483)
[2021-04-21 07:25:03 +0000] [8483] [INFO] Using worker: sync
[2021-04-21 07:25:03 +0000] [8486] [INFO] Booting worker with pid: 8486
Press "CTRL+C" to stop the application.
Next, deactivate your Python virtual environment with the following command:
# deactivate
Create a Systemd Service File for Python Application
Next, you will need to create a systemd service file to manage the Python application.
You can create it with the following command:
# nano /etc/systemd/system/flaskapp.service
Add the following lines:
[Unit]
Description=Gunicorn instance to serve Flask
After=network.target
[Service]
User=root
Group=www-data
WorkingDirectory=/root/flaskapp
Environment="PATH=/root/flaskapp/venv/bin"
ExecStart=/root/flaskapp/venv/bin/gunicorn --bind 0.0.0.0:5000 wsgi:app
[Install]
WantedBy=multi-user.target
Save and close the file then set proper ownership and permission with the following command:
# chown -R root:www-data /root/flaskapp
# chmod -R 775 /root/flaskapp
Next, reload the systemd daemon with the following command:
# systemctl daemon-reload
Next, start the flask service and enable it to start at system reboot:
# systemctl start flaskapp
# systemctl enable flaskapp
Next, verify the status of the flask with the following command:
# systemctl status flaskapp
You should see the following output:
● flaskapp.service - Gunicorn instance to serve Flask
Loaded: loaded (/etc/systemd/system/flaskapp.service; disabled; vendor preset: enabled)
Active: active (running) since Wed 2021-04-21 07:25:51 UTC; 4s ago
Main PID: 8506 (gunicorn)
Tasks: 2 (limit: 2359)
Memory: 26.0M
CGroup: /system.slice/flaskapp.service
├─8506 /root/flaskapp/venv/bin/python3 /root/flaskapp/venv/bin/gunicorn --bind 0.0.0.0:5000 wsgi:app
└─8508 /root/flaskapp/venv/bin/python3 /root/flaskapp/venv/bin/gunicorn --bind 0.0.0.0:5000 wsgi:app
Apr 21 07:25:51 debian10 systemd[1]: Started Gunicorn instance to serve Flask.
Apr 21 07:25:51 debian10 gunicorn[8506]: [2021-04-21 07:25:51 +0000] [8506] [INFO] Starting gunicorn 20.1.0
Apr 21 07:25:51 debian10 gunicorn[8506]: [2021-04-21 07:25:51 +0000] [8506] [INFO] Listening at: http://0.0.0.0:5000 (8506)
Apr 21 07:25:51 debian10 gunicorn[8506]: [2021-04-21 07:25:51 +0000] [8506] [INFO] Using worker: sync
Apr 21 07:25:51 debian10 gunicorn[8506]: [2021-04-21 07:25:51 +0000] [8508] [INFO] Booting worker with pid: 8508
Configure Nginx to Serve Python Application
Next, you will need to create an Nginx virtual host configuration file to serve Python application.
# nano /etc/nginx/conf.d/flaskapp.conf
Add the following lines:
server {
listen 80;
server_name app.example.com;
location / {
include proxy_params;
proxy_pass http://127.0.0.1:5000;
}
}
Save and close the file then restart the Nginx to apply the configuration changes:
# systemctl restart nginx
Access Python Application
Now, open your web browser and access your Python application using the URL http://app.example.com. You should see your application in the following screen:
Conclusion
Congratulations! You've successfully deployed a Python application with Flask and Nginx on a Debian 10 cloud server.
We hope you can now easily move your Python application from the local environment to the production environment.
If you have any question about this tutorial, please, contact us on support@clouding.io. We'll be glad to answer it for you :)