AI/ML

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

July 10, 20242 min read

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.

Step 2: Install NGINX

  1. Update the package list:
  1. Install NGINX:
  1. Start and enable NGINX:
  1. Check NGINX status:

Step 3: Install Streamlit and Run Your Application

  1. Install Python and pip (if not already installed):
  1. 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:

Step 4: Configure NGINX to Serve Streamlit Application

  1. Create a new NGINX configuration file:
  1. Add the following configuration:

nginx

Copy code

  1. Enable the configuration:

Step 5: Obtain and Install an SSL Certificate

  1. Install Certbot:
  1. Obtain an SSL certificate:
  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:
  1. 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.

AI/ML Cybersecurity

Let's build what's next, together

Talk to our team about cloud, cybersecurity, AI or Odoo. We'll help you plan the right next step.