Ubuntu – setting up a server for Flask

2 min

Configuring Linux server Ubuntu for sites to work on python through the framework Flask

In the article we work on the server Ubuntu 20.04 with basic initial setup. How to update and do the initial setup of an Ubuntu server.

We have already set up a server for Django. Some processes are similar and some are different.

1. Installing packages for Python

Let’s update the packages in the system:

Install the necessary packages to work with python

sudo apt install python3-pip python3-dev python3-setuptools build-essential libssl-dev libffi-dev

2. Create a Python virtual environment

Install the package python3-venv:

sudo apt install python3-venv

We go to the user folder:

Create a new directory for the project. This could be the name of the site:

mkdir ploshadka.net
cd ploshadka.net

Install the virtual environment to the directory venv… It will be created in the current directory:

This isolated virtual environment will contain various modules for running Python.

For further configuration, we activate the virtual environment:

3. Install Flask

While in a virtual environment, install:

pip install wheel
pip install uwsgi flask

We exit the virtual environment:

Additionally, you can install SQLAlchemy.

Let’s create and test the start page

Create a file wsgi.py at the root of the site. This will be the starting point for launching our application.

Previously, a port must be open in our ufw firewall:

We can already launch our site with this command:

uwsgi –socket –protocol= http -w wsgi: app –py-autoreload 1

The site will be available at:

Played around and closed access again:

sudo ufw delete allow 5000

Settings file

Next, let’s create a file with settings in the root of the site:

Inside it:

# Start page file name without extension
module = wsgi: app

# Master mode and number of worker processors
master = true
processes = ten

# Create and clean up a socket
socket = wsgi-socket.sock
chmod-socket = 660
vacuum = true

die-on-term = true

# Automatically refreshes the data on the page after changes to the site content
py-autoreload = 1

4. Configuring the systemd service

sudo nano /etc/systemd/system/ploshadka.service

File contents:

Description= uWSGI instance for ploshadka
After= network.target

User= ploshadka
Group= www-data
Environment=“PATH = / home / ploshadka / ploshadka.net / venv / bin”
ExecStart=/home/ploshadka/ploshadka.net/venv/bin/uwsgi –ini wsgi-config.ini

WantedBy= multi-user.target

UWSGI Service Commands

Service start:

sudo systemctl start ploshadka

Let’s put the service at startup when the server starts:

sudo systemctl enable ploshadka

Checking status:

sudo systemctl status ploshadka


sudo systemctl restart ploshadka

5. Configuring Nginx

Install nginx on the server:

sudo apt update
sudo apt install nginx

Let’s add permission to our ufw firewall… Let’s see the possible configurations:

And add:

sudo ufw allow ‘Nginx Full’

Let’s set up a basic configuration for nginx:

sudo nano /etc/nginx/sites-available/ploshadka.net

File contents:

server {
listen 80;
server_name ploshadka.net www.ploshadka.net;

location / {
include uwsgi_params;
uwsgi_pass unix: /home/ploshadka/ploshadka.net/wsgi-socket.sock;

We activate the settings of the new site for nginx:

sudo ln -s /etc/nginx/sites-available/ploshadka.net /etc/nginx/sites-enabled

Let’s check for errors in the file syntax:

Reload nginx:

sudo systemctl restart nginx

The site is ready and should work at http: //:

6. Get Let’s Encrypt certificate for https

Install Certbot for nginx:

sudo apt install certbot python3-certbot-nginx

Certbot will automatically renew the certificate 30 days before the expiration date.

We request a certificate for our site:

sudo certbot –nginx -d your-site.net -d www.your-site.net

Let’s check the expiration time of the certificate:

sudo systemctl status certbot.timer

7. Install PostgreSQL

Install PostgreSQL on the server:

sudo apt-get install postgresql postgresql-contrib libpq-dev

We activate the python virtual environment (you must first be in the site directory):

Now install the package psycopg2which is python-postgreSQL adapter:

Next, we create and configure the user and database in PostgreSQL.

After setting up the database in PostgreSQL, you can connect to it from your local computer.


Leave a Reply