DeveloperBreeze

This tutorial will guide you through the process of deploying a Flask application on a VPS using Gunicorn as the WSGI server and Nginx as the reverse proxy. We will use pipreqs to manage Python dependencies and set up HTTPS using Let's Encrypt.

Prerequisites

  • A Flask application ready to be deployed.
  • Access to a VPS (e.g., Ubuntu 20.04).
  • A registered domain name.
  • Basic knowledge of Linux command line operations.

Step 1: Set Up the VPS

Update and Upgrade the System

sudo apt update
sudo apt upgrade -y

Install Required Packages

Install essential packages for building Python packages and SSL:

sudo apt install -y python3-pip python3-venv nginx curl

Step 2: Clone the Flask Application

Clone Your Git Repository

git clone https://github.com/username/your-flask-app.git
cd your-flask-app

Replace https://github.com/username/your-flask-app.git with the URL of your repository.


Step 3: Set Up the Python Environment

Create and Activate a Virtual Environment

python3 -m venv venv
source venv/bin/activate

Install Dependencies Using pipreqs

First, install pipreqs:

pip install pipreqs

Generate a requirements.txt file based on your project imports:

pipreqs . --force

Then, install the dependencies:

pip install -r requirements.txt

Step 4: Set Up Gunicorn

Install Gunicorn

pip install gunicorn

Test Gunicorn Locally

gunicorn --bind 0.0.0.0:8000 app:app

Replace app:app with your actual Flask application entry point.

Create a Systemd Service for Gunicorn

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

Paste the following content:

[Unit]
Description=Gunicorn instance to serve Flask application
After=network.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/path/to/your-flask-app
Environment="PATH=/path/to/your-flask-app/venv/bin"
ExecStart=/path/to/your-flask-app/venv/bin/gunicorn --workers 3 --bind unix:/path/to/your-flask-app/developerbreeze.sock app:app

[Install]
WantedBy=multi-user.target

> Replace /path/to/your-flask-app with your actual project path.

> Replace app:app with your actual Flask app entry point.

Start and Enable the Gunicorn Service

sudo systemctl enable developerbreeze.service
sudo systemctl start developerbreeze.service
sudo systemctl status developerbreeze.service

Step 5: Configure Nginx

Create an Nginx Configuration File

sudo nano /etc/nginx/sites-available/developerbreeze

Paste the following:

# HTTP to HTTPS redirection
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name developerbreeze.com www.developerbreeze.com;

    return 301 https://developerbreeze.com$request_uri;
}

# HTTPS Server Block
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name developerbreeze.com www.developerbreeze.com;

    ssl_certificate /etc/letsencrypt/live/developerbreeze.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/developerbreeze.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    root /path/to/your-flask-app;
    index index.html;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location / {
        include proxy_params;
        proxy_pass http://unix:/path/to/your-flask-app/developerbreeze.sock;
    }

    location /static {
        alias /path/to/your-flask-app/static;
    }

    location /favicon.ico {
        alias /path/to/your-flask-app/static/favicon.ico;
    }

    location ~ /.well-known {
        allow all;
    }
}

> Replace /path/to/your-flask-app with your actual project path.

Enable the Nginx Site

sudo ln -s /etc/nginx/sites-available/developerbreeze /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default

Test and Restart Nginx

sudo nginx -t
sudo systemctl restart nginx

Step 6: Set Up SSL with Let's Encrypt

Install Certbot

sudo apt install certbot python3-certbot-nginx

Obtain an SSL Certificate

sudo certbot --nginx -d developerbreeze.com -d www.developerbreeze.com

Follow the prompts to complete the installation.

Verify SSL Renewal

sudo certbot renew --dry-run

Step 7: Verify the Deployment

sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log
sudo journalctl -u developerbreeze.service -f

Conclusion

By following these steps, you have successfully deployed your Flask application using Gunicorn and Nginx, secured with SSL from Let's Encrypt. This setup provides a robust and scalable environment for running your web application.

Continue Reading

Discover more amazing content handpicked just for you

Tutorial
bash

Renaming a subdomain in Apache and generating a new SSL certificate

sudo certbot --apache -d new.example.com

This command will:

Nov 07, 2024
Read More
Tutorial
bash

How to Install and Configure Apache on Ubuntu

sudo apt install apache2

Once the installation is complete, Apache will automatically start. You can verify if it's running with:

Oct 21, 2024
Read More
Tutorial
bash

How to Create SSL for a Website on Ubuntu

Save the file and restart Nginx:

sudo systemctl restart nginx

Oct 21, 2024
Read More
Tutorial
bash

How to Reset the MySQL Root Password Using DROP USER

sudo systemctl start mysql

Now that MySQL is running in "safe mode" without requiring passwords, you can log in without being prompted for the root password.

Oct 03, 2024
Read More
Article
bash

Top 25 Nginx Web Server Best Security Practices

By default, Nginx comes with multiple modules, some of which may not be necessary for your use case. Disable any modules that aren’t needed to reduce the attack surface.

By default, Nginx runs as the root user. Change this to a non-privileged user like www-data to limit the scope of potential security breaches.

Sep 24, 2024
Read More
Tutorial
python

Setting Up and Managing Python Virtual Environments Using venv

To deactivate the virtual environment, simply run:

deactivate

Aug 29, 2024
Read More
Tutorial

Adding a Subdomain on an Apache Server

To verify that everything is working correctly, you can create a simple index.html page in the subdomain’s directory.

   nano /var/www/blog.example.com/index.html

Aug 21, 2024
Read More
Tutorial
json bash

Building Progressive Web Apps (PWAs) with Modern APIs

mkdir my-pwa
cd my-pwa

Create the basic file structure:

Aug 05, 2024
Read More
Code
php

Get Current URL

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!