Skip to content
Topics
Streamlit
How to Run Streamlit Applications on Port 80

How to Run Streamlit Applications on Port 80

Streamlit is a powerful tool that enables developers to create interactive web applications with minimal hassle. However, it's not without its challenges, particularly when it comes to configuration. One such issue frequently encountered is running applications on port 80. In this comprehensive guide, we delve into the subject, drawing from user experiences, expert insights, and practical examples to help you navigate this challenge with confidence.

Want to Create a Data Analysis & Data Visualization App within Streamlit for free?

PyGWalker (opens in a new tab) is a Python Library that helps you easily embed a Tableau-like UI into your own Streamlit app effortlessly. Check out this amazing video produced by Sven from Coding is Fun (opens in a new tab) demonstrating the detailed steps for empowering your Streamlit app with this powerful Data Visualization Python Library!


Special Thanks to Sven and his great contribution (opens in a new tab) to PyGWalker community!

Additionally, you can also check out PyGWalker GitHub Page (opens in a new tab) for more PyGWalker examples.

Part 1: The Challenge of Configuring Streamlit to Run on Port 80

In May 2022, a user in the Streamlit community, named Nom_ji, ran into an issue while attempting to run an application on port 80. Despite following the appropriate configuration steps, as detailed in the official Streamlit documentation, Nom_ji was met with a PermissionError: [Errno 13] when deploying the app on port 80.

Interestingly, the application ran perfectly well on ports 8051 and 8080. This leads us to the first key point:

  • Not all ports are created equal. Ports below 1024, including port 80, are considered privileged in Unix-like operating systems, and typically require superuser (or root) access.

In response to Nom_ji's issue, Randyzwitch, the former head of developer relations at Streamlit, suggested ensuring that port 80 wasn't occupied by another instance. Checking for existing instances on a port can be done using the netstat command in the terminal:

sudo netstat -tuln | grep :80

This command will list any services currently running on port 80. If the command returns any output, it means that port 80 is already in use, and you'll need to stop the other service before you can run your Streamlit application on this port.

Part 2: Diving Deeper Into Port Assignment in Streamlit

Another issue that Streamlit users may encounter is running multiple Streamlit applications simultaneously, each requiring its unique port. Streamlit community moderator, BeyondMyself, provided a straightforward solution to this: specify the --server.port parameter when starting the application.

For instance, the command streamlit run myapp.py --server.port 8080 will run the Streamlit app defined in myapp.py on port 8080. You can use any available port number in place of 8080, provided it is not already in use.

Thus, when planning to run multiple Streamlit apps, ensure to:

  • Assign unique port numbers to each application.
  • Make sure the chosen port is not already in use by another service.

Part 3: Making Streamlit Apps Appear to Run on Standard Ports

While you may choose to run your Streamlit apps on any available port, there might be scenarios where you'd want the app to appear to run on a standard port (like 80) when accessed via a domain. This is where the concept of a reverse proxy comes into play.

A reverse proxy can forward requests from a web server, like Apache or Nginx, to the port where your Streamlit app is running. This can be set up in several ways:

  • Forward all requests to your domain to your Streamlit app.
  • Configure your web server to forward requests to specific subdirectories to different Streamlit apps.

For instance, if you have multiple Streamlit apps running on different ports, you

could have mydomain.com/app1 forwarded to the Streamlit app running on port 8000 and mydomain.com/app2 forwarded to the app on port 8080. This setup makes it appear as though the apps are running on port 80 from the user's perspective.

Part 4: Setting a Fixed Port Number for Streamlit Applications

Lastly, you may want to fix the port number for your Streamlit applications, allowing you to share a stable link with your team members or users. This is where the ./streamlit/config.toml file comes into play.

In the config.toml file, you can specify the fixed port number. For instance:

[server]
port = 8501

This configuration tells Streamlit to always use port 8501 when running your app. Note that this port must not be in use by another service, or the app will fail to run.

To summarize:

  • Ensure you have superuser access for privileged ports.
  • Check for existing instances on a port using netstat.
  • Use the --server.port parameter for running multiple apps.
  • Employ reverse proxies to make apps appear to run on port 80.
  • Use the config.toml file to fix the port number.

Part 5: Dealing with Common Errors

Despite following the outlined steps, you may still encounter errors, such as the “port is reserved” error. This error usually appears when another process has not shut down correctly or something else is running on the port you are trying to use.

To investigate if a port is occupied, you can utilize the netstat command. Below is a simple code snippet that allows you to check if a specific port (in this case port 80) is being used:

netstat -tuln | grep :80

In the output, if you see the desired port number listed, it means the port is occupied. In Linux systems, you can also use the lsof command to find out which process is using a certain port:

sudo lsof -i :80

Once you identify the process, you can terminate it using the kill command.

kill -9 PID

Replace PID with the process ID that you obtained from the previous command.

If you are sure that nothing else is using the port and you're still encountering the error, it may be due to an issue with your operating system's network settings. In this case, consider seeking help from an IT professional or the Streamlit community.

Part 6: Keeping Streamlit Applications Up-to-Date

Another challenge you might face is ensuring that your Streamlit application always displays up-to-date data. This is especially important if your app is displaying live or frequently updated data. The solution to this challenge lies in the Streamlit cache.

Streamlit's caching mechanism allows your app to store data in memory, so it doesn't need to be loaded from disk or fetched from a remote server every time the app runs.

By using the @st.cache decorator in your data loading functions, you can significantly improve your application's speed and responsiveness. For example, if you have a function that fetches data from a remote server, you could add the @st.cache decorator above the function like this:

@st.cache
def get_data():
    # fetch data from remote server
    return data

With this, Streamlit will store the returned data in its cache. When the function is called again, Streamlit will first check if the data is in its cache. If it is, Streamlit will use the cached data instead of calling the function again.

In addition, Streamlit provides the ttl parameter that you can use to specify how long the data should be cached. If you set ttl to some number of seconds, Streamlit will automatically refresh the cached data after that duration.

Here's an example of how to use the ttl parameter in your function:

@st.cache(ttl=60*60*2)
def get_data():
    # fetch data from remote server
    return data

In this example, the ttl is set to two hours (60 seconds * 60 minutes * 2), which means Streamlit will automatically refresh the data every two hours.

Conclusion

Running Streamlit applications on port 80 is not as straightforward as it might seem, due to the privileged nature of the port and other potential issues like occupied ports or permissions. But with the insights we've discussed in this guide, you should be well-prepared to navigate these challenges.

Remember, when you encounter difficulties, don't hesitate to reach out to the Streamlit community. It's filled with knowledgeable individuals who have likely faced similar issues and are more than willing to lend a helping hand.

Frequently Asked Questions

1. Why can't I run my Streamlit application on port 80?

Ports below 1024, including port 80, are considered privileged in Unix-like operating systems and typically require superuser (or root) access. Ensure you have the necessary permissions, and the port is not already in use.

2. How can I run multiple Streamlit applications simultaneously?

You can run multiple Streamlit apps by specifying different port numbers for each application using the --server.port parameter when starting the application.

3. How can I keep my Streamlit application data up-to-date?

You can use the @st.cache decorator in your data loading functions to store data in memory, improving your application's speed and responsiveness. Streamlit also provides the ttl parameter for specifying how long the data should be cached.