Deploying Flask apps on Linode.com
By Angela C
June 21, 2021
Reading time: 4 minutes.
In this post I am following the tutorials and guides on hosting Flask apps on Linode.com
A link to an example Flask app on https://github.com/abalarin is provided or you can use your own when following the guide. I used the example Flask app by Austin Balarin who also has a very helpful guide for deploying a Flask app on Linode.
Create an app
- Create or clone the flask app then move into this folder.
git clone https://github.com/abalarin/Flask-on-Linode.git flask_app_project
-
Create a linode
-
SSH into the linode using root
-
apt-get update && apt-get upgrade
to install software updates on Ubuntu distribution -
Set the hostname: to identify the linode with an easy to remember name
hostnamectl set-hostname example_flask
- list timezones:
timedatectl list-timezones
timedatectl set-timezone 'Europe/Dublin'
() dpkg-reconfigure tzdata
is the newer and recommended way to reset timezone for Ubuntu)
date
to check the date is correct
Secure your server
-
Set up an user with sudo to avoid security issues with using the root user.
adduser user1
-
add a password for this user
adduser user1 sudo
to add the limited user to the sudo group -
exit
to exit from root then login again using the new user `` ssh user1@123.234.345.56
To run a command as administrator (user “root”), use “sudo ”. See “man sudo_root” for details.
-
Navigate to home
cd /home
-
Pull application code from source control
sudo git clone https://github.com/abalarin/Flask-on-Linode.git
-
Alternatively retrieving an application from your Local Machine:
-
SCP your Applications root directory into your Linode’s Home directory
-
scp -r flask_app/ user@<Linode-IP>:/home
-
sudo scp -r flask_app/ user1@123.234.345.56:/home
-
Configure Nginx
NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server.
- Install Nginx
sudo apt install nginx
2. Create an NGINX Configuration file
sudo nano /etc/nginx/sites-enabled/flaskapp
Copy and paste the code here into the configuration file using the nano code editor.
replace the IP address with your Linodes own IP address.
server {
listen 80;
server_name <Your Linodes IP>;
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;
}
}
ctrl+x
to exit the nano editor, then y
to save.
- Unlink the NGINX default config
sudo unlink /etc/nginx/sites-enabled/default
- Reload your NGINX server
sudo nginx -s reload
Install Python and packages
sudo apt install python3
sudo apt install python3-pip
- Install Flask Packages/libraries:
cd Flask-on-Linode
pip3 install -r requirements.txt
sudo pip3 install -r flask_app/requirements.txt
Deploy your application
Gunicorn ‘Green Unicorn’ is a Python WSGI HTTP Server for UNIX. It’s a pre-fork worker model. The Gunicorn server is broadly compatible with various web frameworks, simply implemented, light on server resources, and fairly speedy FlaskDeployment tutorial
-
Install Gunicorn
-
Run Gunicorn from your Application’s root directory or a directory up from your Application’s entry point
The command gunicorn -w 3 flask_app:app
tells Gunicorn to look for the WSGI instance named app
in the flask_app
directory. In this example project this WSGI instance named app
is located in __init__.py
.
The amount of workers to use with Gunicorn can be specified with the -w
flag.
As per the tutorial, a good rule of thumb is to double your CPU core’s and add 1 for a Nanode with 1 CPU core thats 3 workers.
gunicorn -w 3 flask_app:app
The application should now be live and you should be able to navigate to it by entering your Linodes IP into a web browser.
This worked and the example Flask app is here.
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 is great for auto-reloading gunicorn if it crashes or if your Linode is rebooted unexpectedly. FlaskDeployment tutorial
- Install Supervisor
sudo apt install supervisor
- Create a supervisor program
sudo nano /etc/supervisor/conf.d/flaskapp.conf
[program:flaskapp]
directory=/home/Flask-on-Linode
command=gunicorn -w 3 flask_app:app
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
stderr_logfile=/var/log/flaskapp/flaskapp.err.log
stdout_logfile=/var/log/flaskapp/flaskapp.out.log
- Create your log files
touch /var/log/flaskapp/flaskapp.out.log
touch /var/log/flaskapp/flaskapp.err.log
- Reload Supervisor to apply changes
sudo supervisorctl reload
How to keep the process running when I log out of ssh
The above was a Flask app following the tutorials referenced. Using Supervisor keeps the app running when I exit from the terminal.
For a dash app I am working on I want to be able to keep the app running when I logout or exit SSH.
At the moment, running the app using gunicorn will make the app available on the internet at the public ip address. However then to exit or shut down my laptop I do ctrl
+ c
. This stops the process and the app is no longer available.
gunicorn -w 3 app:server
I have to figure out how to configure supervisor to work with my dash app. In the meantime to followed the process below as outlined in the Linode Community on how to keep a process running after I disconnect from mu Linode using the bg
command.
-
run the command normally
gunicorn -w 3 app:server
This gets the dash app up and running -
ctrl
+Z
-
run
bg
-
Type
disown
-
Exit the terminal
My dash app is now running and available on the public internet.