Deploy Streamlit app on Nginx
By: Bala Murugan N G
Tags: Streamlit App, Streamlit, Deployment, Python, Nginx

Deploy Streamlit app on Nginx

Introduction & Agenda

Many blogs written about Deploying Streamlit Application on heroku, docker, AWS and Azure etc . I’ve noticed that, only a less number of blogs that said about the “How to deploy Streamlit app in Server using Nginx”. So, I took an interest on writing this.

Usually while deploying Flask, FastAPI or Django, our deployment architecture like this

While in Streamlit, its little bit different. Here we will use Streamlit and Nginx only.

In this tutorial, we will see “How to Deploy/Host a Streamlit application in Local Machine/Server using Nginx with SSL” and I will share the challenges that I faced while deploying in Ubuntu 18.04.

Here is the Agenda

1. Installing Nginx & Streamlit

2. Building Simple Streamlit Application

3. Changes to be done in Streamlit Config File

4. Changes to be done in Nginx Config File

Let’s Move On

Photo by SpaceX on Unsplash

Install Nginx

NGINX is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. Nginx is free and open-source software, released under the terms of the 2-clause BSD license.

sudo apt update sudo apt install nginx

The config files of nginx are in “/etc/nginx” path.

Install Streamlit

Streamlit is an open-source app framework for Machine Learning and Data Science teams. Create beautiful data apps in hours, not weeks. All in pure Python. Streamlit is released under the Apache 2.0 open-source license. This means you are pretty much able to do what you want with Streamlit (including selling applications), as long as you don’t violate any of the licensing terms. Here’s a brief summary of Apache 2.0 Licence in Plain English.

Streamlit currently supports versions 3.6, 3.7 and 3.8 of Python. Create a new python Virtual env or Conda Env, it helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them.

Activate the Environment and install using pip

pip install streamlit

Streamlit uses Tornado Web Server under the hood. Tornado is a single-threaded web server, and isn’t based on WSGI. Running it with gunicorn is not currently possible (or at least, likely to be extremely non-trivial). Check out the related Streamlit Gunicorn Support Docs for further reference

You can use Nginx as a reverse proxy, sitting in front of a Streamlit server and include SSL in Nginx.

Build Simple Streamlit Application

Here I build Simple Streamlit Application that will be able to upload files from client and save in the server.

Create a file named “captainstream.py”. Copy the below code and paste it.

Simple Streamlit Application

Run the file by using “streamlit run captainstream.py” cmd

Here in nginx conf file,

client_max_body_size 100M; → This will allow below 100Mb of files only.

If you want to run the file in background use “nohup streamlit run captainstream.py &”. To Kill the Background process “ps ax | grep captain” , take the pid number and “kill -9 pid”.

Changes to be done in Nginx Config File

Start by creating and opening a new file in Nginx’s sites-available directory

sudo nano /etc/nginx/sites-available/streamlitnginxconf

and paste the below content. . .

sudo nano /etc/nginx/sites-available/streamlitnginxconf

Please read “streamlitnginxconf” file, that specifies each line working and uncomment and commenting them based on your situation.

As per the File, The Nginx listens 9000 port (eg. 192.168.6.418:9000) and forwards to this ip (eg. 192.168.6.418:12345).

Save and close the file when you are finished by using (ctrl X, then y and then “enter” button).

Now, we have to enable the file by linking it to sites-enabled directory:

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

Test your Nginx configuration for syntax errors by typing:

sudo nginx -t

If no errors are reported, go ahead and restart Nginx by typing:

sudo systemctl restart nginx

Finally, we need to open up our firewall to normal traffic on listening port 9000

sudo ufw allow 9000

Note: After configuring Nginx, the next step should be securing traffic to the server using SSL/TLS. This is important because without it, all information, including passwords are sent over the network in plain text.

If you have a domain name, the easiest way get an SSL certificate to secure your traffic is using Let’s Encrypt. Follow this guide to set up Let’s Encrypt with Nginx on Ubuntu 18.04. Follow the procedure using the Nginx server block we created in this guide.

If you do not have a domain name, you can still secure your site for testing and learning with a self-signed SSL certificate. Again, follow the process using the Nginx server block we created in this tutorial.

Changes to be done in Streamlit Config File

By default, the config file not present in “/home/<username>/.streamlit” folder. By creating this config file, overriding the existing config file(If you want to see the Existing config file “streamlit config show”).

Create a Config file inside “/home/<username>/.streamlit/config.toml”

sudo nano /home/<username>/.streamlit/config.toml

and include the below content

[server]

port=15263 # change port number. By default streamlit uses 8501 port

headless=true # This will eliminate automatically open browser

# enableCORS=false

# enableXsrfProtection=false

# enableWebsocketCompression=false [browser] # This ip and port will show in command prompt

serverAddress = "localhost" # Put your Local IP or Domain Name

serverPort = 15263

If you get any symptoms of “App Not Loading” or “Please Wait” Forever or “Error While uploading files”, check here

F inally, My app is deployed in my Server using Nginx

Final Output

If you faced any issue, please comment it and appreciate for this work by sharing and clap.

Reference

Streamlit Docs

Streamlit Gunicorn Support

Streamlit Commercial Use do’s and don’t

Nginx Setup

Nginx Web Socket

Nginx Web Socket SSL

Streamlit File Uploader Error