Implementing NGINX on an EC2 Instance and Adding SSL for Hosting a Streamlit Application 

Hosting a Streamlit application on an AWS EC2 instance with NGINX and SSL can significantly enhance its performance, security, and scalability. This blog provides a step-by-step guide to set up NGINX on an EC2 instance, install an SSL certificate, and configure it to serve a Streamlit application. Let’s dive in! 

Step 1: Launch an EC2 Instance 

  1. Sign in to AWS Management Console: 
  • Go to the EC2 dashboard. 
  1. Launch an Instance: 
  • Click on “Launch Instance.” 
  • Choose an Amazon Machine Image (AMI), such as Amazon Linux 2 or Ubuntu 20.04. 
  • Select an instance type (e.g., t2.micro for free tier eligibility). 
  • Configure instance details and add storage. 
  • Configure security groups: Add rules to allow HTTP (port 80), HTTPS (port 443), and custom TCP for Streamlit (default port 8501). 
  1. Launch and Connect: 
  • Launch the instance and connect to it using SSH. 
sh 

Copy code 

ssh -i your-key-pair.pem ec2-user@your-ec2-public-dns

Step 2: Install NGINX 

  1. Update the package list: 
sh 

Copy code 

sudo apt update
  1. Install NGINX: 
sh 

Copy code 

sudo apt install nginx -y
  1. Start and enable NGINX: 
sh 

Copy code 

sudo systemctl start nginx 

sudo systemctl enable nginx
  1. Check NGINX status: 
sh 

Copy code 

sudo systemctl status nginx

Step 3: Install Streamlit and Run Your Application 

  1. Install Python and pip (if not already installed): 
sh 

Copy code 

sudo apt install python3-pip -y
  1. Install Streamlit: 
sh 

Copy code 

pip3 install streamlit
  1. Deploy your Streamlit app: 
  • Transfer your Streamlit application files to the EC2 instance using scp or any other method. 
  • Run your Streamlit application: 
sh 

Copy code 

streamlit run your_app.py

Step 4: Configure NGINX to Serve Streamlit Application 

  1. Create a new NGINX configuration file: 
sh 

Copy code 

sudo nano /etc/nginx/sites-available/streamlit
  1. Add the following configuration: 

nginx 

Copy code 

server { 

    listen 80; 

    server_name your-domain.com; 

    location / { 

        proxy_pass http://localhost:8501; 

        proxy_set_header Host $host; 

        proxy_set_header X-Real-IP $remote_addr; 

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 

        proxy_set_header X-Forwarded-Proto $scheme; 

    } 

}
  1. Enable the configuration: 
sh 

Copy code 

sudo ln -s /etc/nginx/sites-available/streamlit /etc/nginx/sites-enabled/ 

sudo nginx -t 

sudo systemctl restart nginx

Step 5: Obtain and Install an SSL Certificate 

  1. Install Certbot: 
sh 

Copy code 

sudo apt install certbot python3-certbot-nginx -y
  1. Obtain an SSL certificate: 
sh 

Copy code 

sudo certbot --nginx -d your-domain.com
  1. Follow the prompts to complete the installation. Certbot will automatically configure NGINX for SSL. 

Step 6: Verify and Test Your Setup 

  1. Check the NGINX configuration: 
sh 

Copy code 

sudo nginx -t
  1. Restart NGINX: 
sh 

Copy code 

sudo systemctl restart nginx
  1. Access your Streamlit application: 
  • Open a web browser and navigate to https://your-domain.com. 
  • Ensure your application is served securely over HTTPS. 

Conclusion 

Implementing NGINX on an EC2 instance and adding an SSL certificate to your Streamlit application is crucial for several reasons: 

  1. Enhanced Security: By using NGINX and SSL, you protect the data exchanged between your server and clients, ensuring it remains private and secure. 
  1. Improved Performance: NGINX efficiently manages client requests and serves content quickly, improving the overall performance and responsiveness of your application. 
  1. Scalability: NGINX acts as a reverse proxy, allowing you to easily scale your application by distributing traffic across multiple servers. 
  1. Trust and Credibility: SSL certificates establish trust with your users, showing that your site is secure and their data is protected, which is essential for maintaining user confidence. 

By following these steps, you’ve set up a robust, secure, and scalable environment for your Streamlit application. This ensures that your application not only performs well but also adheres to best practices in web security, ultimately providing a better user experience. 

Leave a Reply

Your email address will not be published. Required fields are marked *