Configuring Apache 2.4 for proxy

This worked for me, how would I adjust the config, to run two other independent sites respectively, localhost:8503, localhost:8504?

As this is the “official” post on how to configure Apache for Streamlit I wanted to say that the above mentioned configuration did not work for me on Debian 12 running Apache 2.4. My browser was not able to connect to the Streamlit websocket.
The configuration that worked for me I found here: Unable to use Streamlit behind HTTP proxy due to websocket errors

If anyone is facing this problem yet, pls see below the solution that actually worked like a charm for me from chatgpt:

If need to use https only change from

VirtualHost *:80

to

VirtualHost *:443

, the configuration is the same.
And if you server is local, only change the IP

192.168.186.132

for

localhost

.

<VirtualHost *:80>
    ServerName yourdomain.com

    ProxyPreserveHost On
    ProxyRequests Off

    # WebSocket Proxy
    RewriteEngine On
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule /(.*)           ws://192.168.186.132:8501/$1 [P,L]

    RewriteCond %{HTTP:Upgrade} !=websocket [NC]
    RewriteRule /(.*) http://192.168.186.132:8501/$1 [P,L]

    # Proxy Settings
    ProxyPass / http://192.168.186.132:8501/
    ProxyPassReverse / http://192.168.186.132:8501/

    # WebSocket ProxyPass
    ProxyPass /stream ws://192.168.186.132:8501/stream
    ProxyPassReverse /stream ws://192.168.186.132:8501/stream

    # Allow WebSocket Connections
    <Location />
        ProxyPass http://192.168.186.132:8501/
        ProxyPassReverse http://192.168.186.132:8501/
    </Location>

    <Location /stream>
        ProxyPass ws://192.168.186.132:8501/stream
        ProxyPassReverse ws://192.168.186.132:8501/stream
    </Location>

</VirtualHost>

Streamlit + Apache - LAN

Hi Everyone!

I would love to share what finally worked for me and what i am trying to achieve. First i am on a LAN eg., my home. Here i have a desktop with apache, python,streamlit, ollama, etc running.

Goal:

Access Streamlit running on desktop from other pcs and macs under the same network under http:// desktop.server/department instead of http:// desktop.server:8501/department.

Meaning i want to create my custom domain. So far ive had the following:

Host file

Edit host file in PC/Server where Apache is running. Run Notepad as admin and look for the file from there.

Windows:

under â€ȘC:\Windows\System32\drivers\etc\hosts:

  • and add this line:
    192.168.1.148 desktop.server
  • Save and exit the file

ON MAC:

Open the terminal and type:
sudo nano /etc/hosts
And add the same line, save and exit the terminal

The host file will have to be edited in every pc/mac from where you want to reach the site unless a DNS configuration is done. Read below under conclusions/further notes.

Apache Server Config

Apache installation Folder: â€ȘC:\Apache24\conf\httpd.conf

Adding the virtual host configuration to the file â€ȘC:\Apache24\conf\httpd.conf

<Virtualhost *:80>

    	ServerName desktop.server

	RewriteEngine On
	RewriteCond %{HTTP:Upgrade} =websocket [NC]
	RewriteRule /department/(.*) ws://desktop.server:8501/department/$1 [P,L]

	RewriteCond %{HTTP:Upgrade} !=websocket [NC]
	RewriteRule /department/(.*) http://desktop.server:8501/department/$1 [P,L]

 	ProxyPreserveHost On
	ProxyPass /department http://desktop.server:8501/department
    	ProxyPassReverse /department http://desktop.server:8501/department

</Virtualhost>

Enable necessary modules under â€ȘC:\Apache24\conf\httpd.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
LoadModule rewrite_module modules/mod_rewrite.so

Streamlit

project

  • .streamlit
    –config.toml
  • pagesaa
    – page_home.py
    – page_chatbot.py
  • main.py
  • modules
    –config_pages.py

Conclusion / Further notes

  • So far my goal is only http://, the https:// protocol i want to add as well but first i want to make sure i can route all pages trough this initially. My goal is as well to enable SSL in order to fully use the app with all its functionality, for example enabling copy pase in st.data_editor. Data editor tables need ssl certification for a simple copy past to work, copying from an excel and pasting in a st.data_editor.
  • So far in my other pcs and macs i can reach the desktop.server trough http://192.168.1.148/department but not trough http://desktop.server/department.
  • In order for the other pcs/macs to identify the http://desktop.server/department in the url i had to edit the host file as well in those pcs/macs.
  • This can be resolved apparently by setting up a DNS as well which will allow for the http://desktop.server/department to be identified in the network as well.
  • config.toml, be careful where you edit this file, depending on you project and environment the config.toml file might be recognised from project/.streamlit or sometimes from C:\Users\your_user\.streamlit

Streamlit app files

main.py:

import streamlit as st

from modules.config_pages import return_pages_all



def main():

    st.set_page_config(
        layout="wide",

    )
    pages = return_pages_all()

    pg = st.navigation(pages=pages)

    pg.run()

if __name__ == "__main__":
    main()

home.py

import streamlit as st

def page_home():
    st.title("Home")

if __name__ == "__page__":
    page_home()

chatbot.py:

import streamlit as st

def page_bot():
    st.title("Home")

if __name__ == "__page__":
    page_bot()

config_pages.py

import streamlit as st

def return_pages_all():

    pages = {
        "Main":[
            st.Page(
                title="Home",
                icon=None,
                page="pages_aa/page_home.py",
                url_path="/home",
            ),
            st.Page(
                title="Chatbot",
                icon=None,
                page="pages_aa/page_chatbot.py",
                url_path="/chatbot",
            ),
        ]
    }
    return pages

config.toml


[browser]
serverAddress = "desktop.server"

[server]
headless = true
port = 8501
baseUrlPath = "/department"