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
- Sign in to AWS Management Console:
- Go to the EC2 dashboard.
- 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).
- 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
- Update the package list:
sh
Copy code
sudo apt update
- Install NGINX:
sh
Copy code
sudo apt install nginx -y
- Start and enable NGINX:
sh
Copy code
sudo systemctl start nginx
sudo systemctl enable nginx
- Check NGINX status:
sh
Copy code
sudo systemctl status nginx
Step 3: Install Streamlit and Run Your Application
- Install Python and pip (if not already installed):
sh
Copy code
sudo apt install python3-pip -y
- Install Streamlit:
sh
Copy code
pip3 install streamlit
- 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
- Create a new NGINX configuration file:
sh
Copy code
sudo nano /etc/nginx/sites-available/streamlit
- 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;
}
}
- 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
- Install Certbot:
sh
Copy code
sudo apt install certbot python3-certbot-nginx -y
- Obtain an SSL certificate:
sh
Copy code
sudo certbot --nginx -d your-domain.com
- Follow the prompts to complete the installation. Certbot will automatically configure NGINX for SSL.
Step 6: Verify and Test Your Setup
- Check the NGINX configuration:
sh
Copy code
sudo nginx -t
- Restart NGINX:
sh
Copy code
sudo systemctl restart nginx
- 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:
- Enhanced Security: By using NGINX and SSL, you protect the data exchanged between your server and clients, ensuring it remains private and secure.
- Improved Performance: NGINX efficiently manages client requests and serves content quickly, improving the overall performance and responsiveness of your application.
- Scalability: NGINX acts as a reverse proxy, allowing you to easily scale your application by distributing traffic across multiple servers.
- 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.