Deploy Flask App on Ubuntu Under 15 minutes!

Adit Shrimal
6 min readSep 25, 2021

Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. However, Flask supports extensions that can add application features as if they were implemented in Flask itself. Extensions exist for object-relational mappers, form validation, upload handling, various open authentication technologies and several common framework related tools.

This tutorial explains the steps to deploy a Flask application to a production environment running on a GCP instance. The production environment uses NGINX as the web server and reverse proxy, Gunicorn as the web server gateway interface (WSGI) application server, and Supervisor for monitoring and auto-reloading.

Before You Begin

1. Create a Flask Application. Clone and run it on the local machine using GitHub.

Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. However, Flask supports extensions that can add application features as if they were implemented in Flask itself. Extensions exist for object-relational mappers, form validation, upload handling, various open authentication technologies and several common framework related tools.

This tutorial explains the steps to deploy a Flask application to a production environment running on a GCP instance. The production environment uses NGINX as the web server and reverse proxy, Gunicorn as the web server gateway interface (WSGI) application server, and Supervisor for monitoring and auto-reloading.

Before You Begin

1. Create a Flask Application. Clone and run it on the local machine using GitHub.

git clone https://github.com/your-flask-project.git flask_app_project

2. Create an instance on google cloud platform. Depending on the size of the application and the amount of users you expect to visit it, you may consider a large GCP instance.

Copy the Flask App

After creating the Flask application in the local development environment, you are now ready to deploy it to a production environment. You need to copy the local Flask application code to your GCP instance. You can accomplish this by either cloning the GitHub project to the server using Git or by using the secure copy method to directly transfer the application files.

Clone the App From Github
1. Ensure that the latest Flask application code is available in the project’s remote repository.
2. SSH into your instance. Replace the example IP address with the IP address of your instance:

ssh adit@10.0.2.0

3. Navigate to the home directory:

cd ~

4. Clone the project from the remote version control system.

git clone https://github.com/your-flask-project.git flask_app_project

Secure Copy the App From a Local Machine
1. From the local machine, secure copy (SCP) the project into the /home directory of the GCP instance. Replace the example IP address with the IP address of the instance and flask_app with the name of the root directory of the project:

scp -r flask_app_project/ adit@10.0.2.0:/~

2. After the application is copied, navigate to the /home directory and view the contents of the directory that you copied:

ls flask_app_project

Prepare the Production Environment
NGINX is open-source software that can be used as a high-performance web server, reverse proxy, load-balancer, and more. In this section you configure NGINX as a web server and reverse proxy for the Flask application. This means that NGINX sits between the Flask application and external clients and forwards all client requests to the running Flask application.
1. Install NGINX:

sudo apt install nginx

2. Using an editor of choice, create an NGINX configuration file for the app with the example content and save it. This example uses the nano text editor. Replace flask_app with the name of the application and 10.0.2.0 with the IP address of the GCP instance:

sudo vim /etc/nginx/sites-enabled/flask_appserver {
listen 80;
server_name 10.0.2.0;

location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

3. Disable the NGINX’s default configuration file by removing its symlink:

sudo unlink /etc/nginx/sites-enabled/default

4. Reload the NGINX configuration file:

sudo nginx -s reload

5. Navigate to the IP address of the GCP instance in a web browser. You should see a similar NGINX Gateway error. This error appears because you have not set up the WSGI application server yet. You set up the application server in the Install and Configure Gunicorn section of the guide.

Install Python and Packages
To run the Flask application, you need to install Python, Flask, pip3 and any other required package dependencies on the GCP instance. 1. In the GCP instance’s /home directory, install Python 3:

sudo apt install python3

2. Install pip3, the standard package manager for Python:

sudo apt install python3-pip

3. Navigate to the project’s root directory:

cd ~/flask_app_project

4. Install Flask packages and libraries using pip3. Install and Configure Gunicorn

Gunicorn, Green Unicorn, is a Python web server gateway interface (WSGI) HTTP Server for UNIX. It is used to forward requests from the NGINX web server to the Flask application. Install Gunicorn on GCP instance:

sudo apt-get install gunicorn

Run Gunicorn from the root directory of the application, flask_app_project. The command tells Gunicorn to look for the WSGI instance named app in the flask_app directory. In the example project, the WSGI instance named app is located in /home/flask_app_project/flask_app/__init__.py.

gunicorn -w 3 flask_app:appadit@10.0.2.0:~/flask_app_project# gunicorn -w 3 flask_app:app
[2021-06-05 15:09:04 +0000] [32421] [INFO] Starting gunicorn 19.9.0
[2021-06-05 15:09:04 +0000] [32421] [INFO] Listening at: http://127.0.0.1:8000 (32421)
[2021-06-05 15:09:04 +0000] [32421] [INFO] Using worker: sync
[2021-06-05 15:09:04 +0000] [32424] [INFO] Booting worker with pid: 32424
[2021-06-05 15:09:04 +0000] [32425] [INFO] Booting worker with pid: 32425
[2021-06-05 15:09:04 +0000] [32426] [INFO] Booting worker with pid: 32426

After running Gunicorn, the Flask application should be live and available over the internet. Open a web browser and enter the IP address of the GCP instance to access the application.
Continue on to the next section to configure Supervisor to monitor and control the Flask app. Install and Configure Supervisor
Supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems. Supervisor can handle auto-reloading Gunicorn if it crashes or if the GCP instance is rebooted unexpectedly. In this section, you install and configure Supervisor. 1. Open a new shell session and SSH into the GCP instance:

ssh adit@10.0.2.0

2. Install Supervisor:

sudo apt install supervisor

3. Create a Supervisor script. Replace any instances of flask_app with the name of the application:

  sudo vim /etc/supervisor/conf.d/flask_app.conf  File: /etc/supervisor/conf.d/flask_app.conf
[program:flask_app]
directory=/home/flask_app_project
command=gunicorn3 --workers=3 flask_app:app
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
stderr_logfile=/var/log/flask_app/flask_app.err.log
stdout_logfile=/var/log/flask_app/flask_app.out.log

4. Create the log directories and files listed in the flask_app.conf file. Make sure to replace flask_app if it was modified in the Supervisor script above:

  sudo mkdir /var/log/flask_app
sudo touch /var/log/flask_app/flask_app.out.log
sudo touch /var/log/flask_app/flask_app.err.log

5. Reload Supervisor to apply the changes:

sudo supervisorctl reload

6. An output similar to the following appears:

Restarted supervisord

NoteThe application should now be accessible again through the IP address of the GCP instance. If you are unable to access the application or receive a bad gateway error, Gunicorn is likely not running. Check the log files to further investigate the issue.

  cat /var/log/flask_app/flask_app.err.log
cat /var/log/flask_app/flask_app.out.log

The Flask application is now deployed to the production environment and available to anyone for viewing.
You can follow a similar workflow to deploy any Flask application to a GCP instance.

--

--