How to setup SSL for flask web server


In this blog post, I'll walk you through the process of adding an SSL certificate to your Flask web server using a certificate created with Let's Encrypt and Nginx.

Step 1: Install Certbot

First, install Certbot using your package manager. For example, if you're using yum, you can run:

sudo yum install certbot

Make sure to adapt the installation command to your specific OS.

Step 2: Create a Standalone Certificate

To create a standalone certificate, use the following command:

sudo certbot certonly --agree-tos --email emaiid@gmail.com -d example.com -n --standalone

This command generates your SSL certificate. You will find two important files in the /etc/letsencrypt/live/example.com directory: fullchain.pem and privkey.pem.

Let's Encrypt provides certificates that are valid for 90 days. To renew the certificate when it expires, simply run:

sudo certbot renew

Step 3: Implement SSL

Now that we have our SSL certificate, there are two ways to implement it. You can either add it to your Flask configuration or use Nginx. In this example, we'll use Nginx.

Edit your Nginx server configuration and add the following lines:


server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/fullchain.pem;  
    ssl_certificate_key /etc/letsencrypt/privkey.pem;  

    
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
}
    

Replace example.com with your domain name, and make sure the paths to your SSL certificate and private key match the locations where Certbot stored them.

And that's it! You've successfully set up SSL for your Flask web server using Let's Encrypt and Nginx.

Happy Learning!