<\/span><\/h2>\n\n\n\nFirst, we\u2019re going to need to log into our server using SSH. You can do that by entering this command:<\/p>\n\n\n\n
ssh root@IP_Address -p Port_Number<\/pre>\n\n\n\nRemember to replace root<\/code> with your username if you are not using the root user. Change IP_Address<\/code> and Port_Number<\/code> according to your server\u2019s IP address and SSH port number.<\/p>\n\n\n\nOnce you are logged in, you should update all of your packages to their latest available versions.<\/p>\n\n\n\n
apt-get update -y
apt-get upgrade -y<\/p>\n\n\n\n
Once all the packages are up-to-date, restart your server to apply the configuration changes.<\/p>\n\n\n\n
<\/span>Install Required Dependencies<\/span><\/h2>\n\n\n\nFlask is a python-based application. So Python and other required dependencies must be installed on your server. If not installed you can install all of them with the following command:<\/p>\n\n\n\n
apt-get install python3 python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools -y<\/pre>\n\n\n\nOnce all the dependencies are installed, install the Python virtual environment package using the following command:<\/p>\n\n\n\n
apt-get install python3-venv -y<\/pre>\n\n\n\nOnce installed, you can proceed to the next step.<\/p>\n\n\n\n
<\/span>Install Nginx Web Server<\/span><\/h2>\n\n\n\nIn this tutorial, we will use Nginx as a reverse proxy for the Flask application. So you will need to install the Nginx web server package to your server. You can install it using the following command:<\/p>\n\n\n\n
apt-get install nginx -y<\/pre>\n\n\n\nOnce the Nginx is installed, start and enable the Nginx service using the following command:<\/p>\n\n\n\n
systemctl start nginx\nsystemctl enable nginx<\/pre>\n\n\n\n<\/span>Create a Virtual Environment for Flask Application<\/span><\/h2>\n\n\n\nNext, you will need to create a virtual environment for the Flask application.<\/p>\n\n\n\n
First, create a project directory with the following command:<\/p>\n\n\n\n
mkdir ~\/project<\/pre>\n\n\n\nNext, change the directory to your project and create a Python virtual environment with the following command:<\/p>\n\n\n\n
cd ~\/project\npython3 -m venv venv<\/pre>\n\n\n\nNext, activate your environment with the following command:<\/p>\n\n\n\n
source venv\/bin\/activate<\/pre>\n\n\n\nNext, install Gunicorn, Flask, and other components with the following command:<\/p>\n\n\n\n
pip install wheel\npip install gunicorn flask<\/pre>\n\n\n\nOnce you are finished, you can proceed to the next step.<\/p>\n\n\n\n
<\/span>Create a Flask Application<\/span><\/h2>\n\n\n\nNext, you will need to create a sample Flask application for your project. Run the following command to create it inside your project directory:<\/p>\n\n\n\n
nano ~\/project\/flaskapp.py<\/pre>\n\n\n\nAdd the following codes:<\/p>\n\n\n\n
from flask import Flask\napp = Flask(__name__)\n@app.route(\"\/\")\ndef hello():\n return \"Welcome to Flask Application!\"\nif __name__ == \"__main__\":\n app.run(host='0.0.0.0')\n<\/pre>\n\n\n\nSave and close the file then verify your application with the following command:<\/p>\n\n\n\n
cd ~\/project\/\npython3 flaskapp.py<\/pre>\n\n\n\nIf everything is fine, you should get the following output:<\/p>\n\n\n\n
* Serving Flask app 'flaskapp' (lazy loading)\n * Environment: production\n WARNING: This is a development server. Do not use it in a production deployment.\n Use a production WSGI server instead.\n * Debug mode: off\n * Running on all addresses.\n WARNING: This is a development server. Do not use it in a production deployment.\n * Running on http:\/\/69.28.84.227:5000\/ (Press CTRL+C to quit)\n<\/pre>\n\n\n\nPress CTRL+C to close the application.<\/p>\n\n\n\n
<\/span>Create a WSGI Entry Point for Gunicorn<\/span><\/h2>\n\n\n\nNext, you will need to create a WSGI entry point to serve your application via Gunicorn.<\/p>\n\n\n\n
Run the following command to create it:<\/p>\n\n\n\n
nano ~\/project\/wsgi.py<\/pre>\n\n\n\nAdd the following lines:<\/p>\n\n\n\n
from flaskapp import app\nif __name__ == \"__main__\":\n app.run()\n<\/pre>\n\n\n\nSave and close the file then verify whether Gunicorn can serve the application correctly using the command below:<\/p>\n\n\n\n
cd ~\/project\/\ngunicorn --bind 0.0.0.0:5000 wsgi:app<\/pre>\n\n\n\nIf everything is fine, you should get the following output:<\/p>\n\n\n\n
[2021-12-23 10:37:15 +0000] [9352] [INFO] Starting gunicorn 20.1.0\n[2021-12-23 10:37:15 +0000] [9352] [INFO] Listening at: http:\/\/0.0.0.0:5000 (9352)\n[2021-12-23 10:37:15 +0000] [9352] [INFO] Using worker: sync\n[2021-12-23 10:37:15 +0000] [9354] [INFO] Booting worker with pid: 9354\n<\/pre>\n\n\n\nPress CTRL+C to stop the application. Next, deactivate from the Python virtual environment with the following command:<\/p>\n\n\n\n
deactivate<\/pre>\n\n\n\n<\/span>Create a Systemd Service File for Flask Application<\/span><\/h2>\n\n\n\nNext, you will need to create a systemd unit file for the Flask application. You can create it with the following command:<\/p>\n\n\n\n
nano \/etc\/systemd\/system\/flask.service<\/pre>\n\n\n\nAdd the following lines:<\/p>\n\n\n\n
[Unit]\nDescription=Gunicorn instance to serve Flask\nAfter=network.target\n[Service]\nUser=root\nGroup=www-data\nWorkingDirectory=\/root\/project\nEnvironment=\"PATH=\/root\/project\/venv\/bin\"\nExecStart=\/root\/project\/venv\/bin\/gunicorn --bind 0.0.0.0:5000 wsgi:app\n[Install]\nWantedBy=multi-user.target\n<\/pre>\n\n\n\nSave and close the file then set proper ownership and permission to flask project:<\/p>\n\n\n\n
chown -R root:www-data \/root\/project\nchmod -R 775 \/root\/project<\/pre>\n\n\n\nNext, reload the systemd daemon with the following command:<\/p>\n\n\n\n
systemctl daemon-reload<\/pre>\n\n\n\nNext, start the flask service and enable it to start at system reboot:<\/p>\n\n\n\n
systemctl start flask\nsystemctl enable flask<\/pre>\n\n\n\nNext, verify the status of the flask with the following command:<\/p>\n\n\n\n
systemctl status flask<\/pre>\n\n\n\nOutput:<\/p>\n\n\n\n
\u25cf flask.service - Gunicorn instance to serve Flask\n Loaded: loaded (\/etc\/systemd\/system\/flask.service; disabled; vendor preset: enabled)\n Active: active (running) since Thu 2021-12-23 10:38:26 UTC; 8s ago\n Main PID: 9376 (gunicorn)\n Tasks: 2 (limit: 2353)\n Memory: 27.8M\n CGroup: \/system.slice\/flask.service\n \u251c\u25009376 \/root\/project\/venv\/bin\/python3 \/root\/project\/venv\/bin\/gunicorn --bind 0.0.0.0:5000 wsgi:app\n \u2514\u25009393 \/root\/project\/venv\/bin\/python3 \/root\/project\/venv\/bin\/gunicorn --bind 0.0.0.0:5000 wsgi:app\n\nDec 23 10:38:26 ubuntu2004 systemd[1]: Started Gunicorn instance to serve Flask.\nDec 23 10:38:26 ubuntu2004 gunicorn[9376]: [2021-12-23 10:38:26 +0000] [9376] [INFO] Starting gunicorn 20.1.0\nDec 23 10:38:26 ubuntu2004 gunicorn[9376]: [2021-12-23 10:38:26 +0000] [9376] [INFO] Listening at: http:\/\/0.0.0.0:5000 (9376)\nDec 23 10:38:26 ubuntu2004 gunicorn[9376]: [2021-12-23 10:38:26 +0000] [9376] [INFO] Using worker: sync\nDec 23 10:38:26 ubuntu2004 gunicorn[9393]: [2021-12-23 10:38:26 +0000] [9393] [INFO] Booting worker with pid: 9393\n<\/pre>\n\n\n\n