osdse:streamlit_with_apache

This is an old revision of the document!


Streamlit Configuration

mkdir streamlit_app
cd streamlit_test
 
python --version
python -m venv venv
 
source venv/bin/activate
 
pip install -U pip
pip install streamlit
mkdir .streamlit
vi .streamlit/config.toml
config.toml
[server]
port = 8502
headless = true
baseUrlPath= "/streamlit-app"
 
[browser]
serverAddress = "streamlit.roman-halliday.com"
serverPort = 8502
streamlit hello

To keep application running after session disconnected

sudo apt-get update && sudo apt-get update -y
sudo apt-get install tmux
# create tmux session
tmux -s streamlit
 
# ctrl b + d to exit the tmux.
 
# attach to existing tmux session
tmux attach
session_name="your_session_name"  # Replace with the actual name of your tmux session
 
if tmux has-session -t "$session_name" 2>/dev/null; then
  echo "Tmux session '$session_name' exists. Attaching..."
  tmux attach-session -t "$session_name"
else
  echo "Tmux session '$session_name' does not exist. Creating and attaching..."
  tmux new-session -s "$session_name"
fi

Apache Configuration

cd /etc/apache2/sites-available/
sudo vi streamlit_VirtualHost.conf
sudo vi streamlit_VirtualHost-le-ssl.conf
 
sudo a2ensite streamlit_VirtualHost.conf
 
sudo certbot --apache
 
sudo systemctl reload apache2
streamlit_VirtualHost.conf
<VirtualHost *:80>
 
        ServerName streamlit.roman-halliday.com
        ServerAlias reporting.roman-halliday.com
 
        ServerAdmin david@roman-halliday.com
 
        #######################################################################
        # No documents being served, only streamlit.
        # Don't configure for this VirtualHost
        #######################################################################
        #DocumentRoot /var/www/streamlit
 
        #######################################################################
        # Logging (use standard log files)
        #######################################################################
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
 
        #######################################################################
        # Rewrite configuration
        #######################################################################
        RewriteEngine on
 
        #######################################################################
        # Reverse Proxy
        # HTTP (port 80) only exists to push to HTTPS
        # So ignore ofiginal configuration for serving streamlit
        #######################################################################
        #RewriteCond %{HTTP:Upgrade} websocket
        #RewriteRule /streamlit-app(.*) ws://streamlit.roman-halliday.com:8502/streamlit-app$1 [P,L]
        #RewriteCond %{HTTP:Upgrade} !websocket
        #RewriteRule /streamlit-app(.*) http://streamlit.roman-halliday.com:8502/streamlit-app$1 [P,L]
 
        #######################################################################
        # Push all requests for http to https and forward to /streamlit-app
        # (originaly auto configured by letsencrypt, RewriteRule modified)
        #######################################################################
        RewriteCond %{SERVER_NAME} =streamlit.roman-halliday.com [OR]
        RewriteCond %{SERVER_NAME} =reporting.roman-halliday.com
        RewriteRule ^ https://%{SERVER_NAME}/streamlit-app [END,NE,R=permanent]
</VirtualHost>
streamlit_VirtualHost-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
 
        ServerName streamlit.roman-halliday.com
        ServerAlias reporting.roman-halliday.com
 
        ServerAdmin david@roman-halliday.com
 
        #######################################################################
        # No documents being served, only streamlit.
        # Don't configure for this VirtualHost
        #######################################################################
        #DocumentRoot /var/www/streamlit
 
        #######################################################################
        # Logging (use standard log files)
        #######################################################################
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
 
        #######################################################################
        # Rewrite configuration
        #######################################################################
        RewriteEngine on
 
        #######################################################################
        # Reverse Proxy
        #######################################################################
        RewriteCond %{HTTP:Upgrade} websocket
        RewriteRule /streamlit-app(.*) ws://streamlit.roman-halliday.com:8502/streamlit-app$1 [P,L]
        RewriteCond %{HTTP:Upgrade} !websocket
        RewriteRule /streamlit-app(.*) http://streamlit.roman-halliday.com:8502/streamlit-app$1 [P,L]
 
        #######################################################################
        # Redirect if app path not given
        #        So: https://streamlit.roman-halliday.com
        #   Becomes: https://streamlit.roman-halliday.com/streamlit-app
        #######################################################################
        RewriteCond %{REQUEST_URI} !streamlit-app
        RewriteRule ^/?$ /streamlit-app [L,R=302]
 
        #######################################################################
        # Certificate - letsencrypt (auto configured by letsencrypt)
        #######################################################################
        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateFile /etc/letsencrypt/live/reporting.roman-halliday.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/reporting.roman-halliday.com/privkey.pem
</VirtualHost>
</IfModule>

Notes

#!/bin/bash
 
script_name="$1"  # Get the script name as the first argument
lock_file="/tmp/${script_name//[^a-zA-Z0-9_.-]/}_running.lock" # Create a lock file name
 
# Function to execute if the script is not running
execute_script_function() {
  echo "Executing the script's function..."
  # Add the actual commands you want to execute here
  sleep 5 # Example: Simulate some work
  echo "Function execution complete."
}
 
# Check if the lock file exists
if [ -f "$lock_file" ]; then
  # Check if the process associated with the lock file is still running
  pid=$(cat "$lock_file")
  if ps -p "$pid" > /dev/null 2>&1; then
    echo "Script '$script_name' is already running (PID: $pid)."
    exit 1
  else
    # Lock file exists, but the process is gone. Clean up the stale lock file.
    echo "Warning: Stale lock file found. Removing '$lock_file'."
    rm -f "$lock_file"
  fi
fi
 
# Create the lock file with the current process ID
echo "$$" > "$lock_file"
 
# Execute the script's function
execute_script_function
 
# Clean up the lock file when the script finishes
rm -f "$lock_file"
 
exit 0
  • osdse/streamlit_with_apache.1744040975.txt.gz
  • Last modified: 2025/04/07 15:49
  • by david