Welcome to part 10 of tutorial series on Flask application with EC2 instance. In the previous tutorial, we have deployed multiple flask application running on a different port on a single EC2 instance. In this tutorial we are going to integrate OpenSSL with nginx webserver hence we can load our application with https instead of HTTP.

OpenSSL is the package that enables secure communication between networks. Ideally, it is the open source software package which comprises the implementation of SSL and TLS protocols.

Assuming you have single flask application up and running. OpenSSL comes inbuilt with ubuntu. Here, we are going to create the self-signed certificate and the self-signed key. To create let’s execute the below command.

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/nginx-self-signed.key -out /etc/ssl/certs/nginx-self-signed.crt

If you like to know more about the command then please refer my video tutorial. Post running this command it will ask for some information. Recommend you to enter the domain name in Common Name.

Now, we will move on to edit the nginx configuration file (i.e. flaskapp).

cd /etc/nginx/sites-enabled/
sudo vim flaskapp

Paste the below snippet and save. Here, we have configured nginx to listen on port 443 and turned on the SSL with reference to s.elf-signed certificate and key.

server {
    listen 80;
    listen 443 ssl;
    
    ssl on;
    ssl_certificate /etc/ssl/certs/nginx-self-signed.crt;
    ssl_certificate_key /etc/ssl/private/nginx-self-signed.key;

    server_name 0.0.0.0;
    location / {
         proxy_pass http://unix:/home/ubuntu/flaskapp/flaskapp.sock;
    } 
}

Therefore, we will restart nginx server followed by flaskapp service.

sudo service nginx restart
sudo service flaskapp restart

Before we go ahead and hit the application, make sure to open port 443 via the security group. Post that, access the application with https instead of HTTP from browser. As a result, you will successfully be able to load the application over https. You can refer the below tutorial for more detail.


Well, that’s it for now. In a mean time refer my YouTube channel for more tutorials. Keep sharing and stay tuned for more. Follow me on Twitter