This page looks best with JavaScript enabled

Deploy a simple flask app on GCP with Nginx

 ·  ☕ 3 min read

Prepare

Create project and VM

Go to Google Cloud Platform and create new project.


create new project

Click NEW PROJECT


click create new project

Fill in Project name


create new project finished

After the creation is complete, switch to the newly added project.

Search and select Compute Engine (You can pin it to the top)


select compute engine

Create


create vm

Follow your own needs to set it.


vm detail 1

I select Ubuntu 20.04 LTS

select system and disk

Allow HTTP and HTTPS traffic

vm detail 2

Switch to Networking tab, add a static IP


vm detail 3

Click External IP, select Create IP address

vm detail 4

Fill in the name

vm detail 5

Create the VM, copy the External IP


copy static ip

Go to your domain name provider and add A type to this IP


add a record

Create an APP

Back to VM, click Connect

Select Open in browser windows


select open in browser

It will open a Terminal.
If the word is too small, click the gear in the upper right corner to adjust.
You can also upload/download files from here.


terminal

First, update and upgrade

1
2
sudo apt-get update
sudo apt-get upgrade

This time will use Python (Ubuntu 20.04 LTS preinstalled python 3.8)

Install other packages

1
2
sudo apt-get install python3-pip
pip3 install pipenv

Create a folder and python virtual environment.

1
2
3
4
mkdir myapp	# create a folder
cd myapp # get into the folder
pipenv --python 3.8	# create python 3.8 virtual environment
pipenv shell # get into the virtual environment

virtual environment

Install Flask, and create a app.py

1
2
3
pipenv install flask # install flask
touch app.py # add app.py file
nano app.py # use nano editor to open app.py

Write a simple Flask APP

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello():
    return "Hello"	# Show Hello


if __name__ == '__main__':
    app.run()	# run APP, default Port: 5000

Press Ctrl + X, and then Y to save it
Run it

1
python app.py

You will see, Running on http://127.0.0.1:5000/

Now, click the gear to create a new connection

After connected

1
curl http://127.0.0.1:5000

It will return Hello if it works.

Nginx

Install Nginx

1
sudo apt-get install nginx

After the installation is complete, and you have added the A type.

Open the browser, input your domain
You can see the Nginx welcome page


welcome to nginx

Check the configuare file, it’s path /etc/nginx/nginx.conf

1
sudo nginx -t

Check configure content

1
cat /etc/nginx/nginx.conf

Nginx files and directories

  • /etc/nginx:Nginx mainly directory
  • /etc/nginx/nginx.conf:Nginx mainly configure file
  • /etc/nginx/sites-available:The configuration of each server will be stored here
  • /etc/nginx/sites-enabled:Enabled configuration, A link file will be created from the sites-available folder
  • /var/log/nginx/access.log:Nginx access log
  • /var/log/nginx/error.log:Nginx error log

Cerbot

Cerbot to get Certificate

Go to Cerbot

Select nginx and Ubuntu 20.04


select nginx and ubuntu

Follow the steps that appear

1
2
3
4
5
6
sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository universe
sudo apt-get update

sudo apt-get install certbot python3-certbot-nginx

Obtain the certificate and automatically configure Nginx.

1
sudo certbot --nginx

Enter in order

  • Email
  • a (Terms)
  • n (Send related emails)
  • YOUR DOMAIN NAME
  • 2 (redirect http -> https)

Auto-renew

1
sudo certbot renew --dry-run

Certificate will be saved to /etc/letsencrypt/live/YOUR_DOMAIN_NAME


nginx with ssl

Edit the server configure

1
sudo nano /etc/nginx/sites-available/default

Find your server_name

location / add proxy_pass to localhost:5000, save it

1
proxy_pass http://127.0.0.1:5000;

edit site-available default

Reload

1
sudo nginx -s reload

It works


flask app with nginx and ssl


Done

Share on
Support the author with

JIHONGO
WRITTEN BY
JIHONGO
A Person