Question

How to deploy streamlit app from droplet

I have a streamlit app in droplet. It worked using the ip address. To run the app I would go to console and type “streamlit run myapp.py”.

How do I deploy it so that I don’t have to type “streamlit run myapp.py” on the console?

Thanks for the help.

Other questions for the next step.

  • Setup domain
  • Run on https

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
December 5, 2023

Heya @ferdinandmosca,

You should be able to deploy this using PM2 so that you don’t have to constantly type in streamlit run myapp.py.

Additionally, in order to setup a domain and run it on https, I’ll recommend using Nginx as a reverse proxy.

1. Set Up PM2 to Manage Your Streamlit App

PM2 is a process manager for Node.js applications, but it can also be used to manage other types of applications, including Python apps like Streamlit.

  1. Install PM2: If you haven’t already installed PM2, you can do so via NPM (Node Package Manager). First, install Node.js and NPM, then install PM2:
sudo apt update
sudo apt install nodejs npm
sudo npm install pm2@latest -g
  1. Run Streamlit with PM2: Start your Streamlit app using PM2. This will keep your app running in the background:
pm2 start 'streamlit run myapp.py' --name my-streamlit-app

The --name flag is optional but helps identify the process.

  1. Set PM2 to Start on Boot: To ensure PM2 and your app start automatically on system reboot:
pm2 startup
pm2 save

2. Configure Nginx as a Reverse Proxy

Nginx can be used to reverse proxy your Streamlit app, making it accessible via a standard web port (like 80 for HTTP).

  1. Install Nginx: If Nginx is not installed, you can install it using:
sudo apt update
sudo apt install nginx
  1. Configure Nginx: Create a new Nginx server block configuration or edit the default one:
sudo nano /etc/nginx/sites-available/default

Add the following inside the server block:

location / {
    proxy_pass http://localhost:8501; # Ensure this is the port Streamlit runs on
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
}
  1. Restart Nginx: To apply the changes, restart Nginx:
systemctl restart nginx

3. Domain and HTTPS

  1. Setup Domain:

    • Point your domain to your Droplet’s IP address using your domain provider’s DNS settings.
    • Update the Nginx configuration to include your domain in the server_name directive.
  2. Enable HTTPS with Let’s Encrypt:

    • Install Certbot and the Nginx plugin:
sudo apt install certbot python3-certbot-nginx
  • Obtain the certificate
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
  • Certbot will modify your Nginx configuration to use HTTPS and set up automatic certificate renewal.

Final Steps

  • Verify everything is working as expected. Your Streamlit app should now be accessible through your domain with HTTPS enabled.
  • Remember to open the necessary ports (like 80 and 443) in your firewall settings.

This setup should ensure your Streamlit app is running continuously without manual intervention, accessible through a domain name, and secured with HTTPS.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel