部署

Streamlit 单机部署方案

以下是为您推荐的 Streamlit 单机部署方案(基于虚拟机 + 域名 + IP 环境):


部署步骤概览

  1. 基础环境准备
  2. 配置基础 Nginx 代理服务
  3. 配置 Systemd 服务(后台运行)
  4. Nginx 反向代理 + HTTPS 配置
  5. 防火墙与安全设置

1. 基础环境准备

应用部署

使用非国内的IP

配置域名解析

将域名(如 grabbit.chat)A记录指向虚拟机IP。

# 测试运行(临时测试后按 Ctrl+C 退出)
streamlit run your_app.py

# 安装必要工具
sudo apt install nginx certbot python3-certbot-nginx

2. 配置基础 Nginx 代理服务

创建 Nginx 配置 /etc/nginx/sites-available/grabbit

server {
    listen 80;
    server_name grabbit.chat;
    index index.php index.html index.htm;
    location / {
        proxy_pass http://localhost:5000/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}

启用配置

sudo ln -s /etc/nginx/sites-available/grabbit /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

3. 配置 Systemd 服务(后台常驻)

创建服务文件 /etc/systemd/system/streamlit.service

[Unit]
Description=GrabBIT Streamlit APP
After=network.target

[Service]
WorkingDirectory=/root/grabbit/web/
ExecStart=/root/grabbit/bin/streamlit run --server.port 5000 app.py

Environment="PATH=/root/grabbit/bin"
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

启用服务:

sudo systemctl daemon-reload
sudo systemctl start streamlit
sudo systemctl enable streamlit

4. Nginx HTTPS 安全访问配置

确保 443 端口ACL规则是开放的

生成 SSL 证书并自动配置Nginx

sudo certbot --nginx -d grabbit.chat

观察 Nginx 配置已经自动更改如下 /etc/nginx/sites-available/grabbit

server {
    server_name grabbit.chat;

    index index.php index.html index.htm;

    location / {
        proxy_pass http://localhost:5000/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/grabbit.chat/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/grabbit.chat/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = grabbit.chat) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name grabbit.chat;
    return 404; # managed by Certbot


}

启用配置

sudo ln -s /etc/nginx/sites-available/grabbit /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

5. 防火墙与安全设置

可选,基本可以不选

# 启用防火墙
sudo ufw enable

# 开放必要端口
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https

# 验证规则
sudo ufw status

验证部署

  1. 访问 https://grabbit.chat
  2. 检查服务状态: bash systemctl status streamlit journalctl -u streamlit -f # 查看实时日志

维护技巧


通过以上配置,您将获得: - 通过域名 HTTPS 安全访问 - 服务自动重启保障稳定性 - Nginx 高性能反向代理 - 免费 Let's Encrypt 证书自动管理

问题解决

以下是分步骤解决方案:


逐步排查与修复 Certbot 验证失败问题

1. 验证域名解析

# 在本地计算机执行(非服务器)
dig app.yourdomain.com +short
# 或使用 nslookup
nslookup app.yourdomain.com

2. 检查服务器端口开放

# 在服务器执行
sudo ufw status

3. 检查云平台安全组(关键!)


4. 验证外部网络可达性

# 在服务器本地测试80端口
curl -I http://localhost

# 从外部网络测试(在本地计算机执行)
curl -I http://app.yourdomain.com

5. 临时简化Nginx配置(排除干扰)

编辑配置文件 /etc/nginx/sites-available/streamlit.conf暂时删除所有重定向和SSL相关配置,仅保留:

server {
    listen 80;
    server_name app.yourdomain.com;

    location / {
        proxy_pass http://localhost:8501;
    }
}

重新加载Nginx:

sudo nginx -t && sudo systemctl reload nginx

6. 手动触发证书申请(强制HTTP验证)

# 使用--standalone模式绕过Nginx插件问题
sudo certbot certonly --standalone -d app.yourdomain.com --preferred-challenges http

7. 检查Certbot日志

sudo tail -n 100 /var/log/letsencrypt/letsencrypt.log

常见问题总结

问题现象 解决方案
DNS解析错误 检查域名A记录,使用 dig 验证
云平台安全组未放行80端口 在云控制台添加安全组规则
本地防火墙阻止80端口 执行 sudo ufw allow 80 并重启防火墙
端口被其他服务占用 停止占用80端口的服务(如Apache)或修改Nginx监听端口
网络运营商封锁HTTP端口 更换服务器端口(如8080)并通过反向代理转发

修复后完整操作流程

  1. 恢复完整Nginx配置
  2. 重新生成证书: bash sudo certbot --nginx -d app.yourdomain.com
  3. 验证HTTPS访问: bash curl -I https://app.yourdomain.com

通过以上步骤,可系统性解决 Certbot 验证失败问题。确保从外网到服务器的 80端口 畅通无阻是成功的关键!