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

Handpicked posts just for you — based on your current read.

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!