#!/bin/bash
# 调试信息
echo "===== Open WebUI with Cloudflare Tunnel ====="
echo "User: $(whoami)"
echo "Home: $HOME"
echo "Working dir: $(pwd)"
echo "=============================================="
# 创建简单的 HTML 页面
# 确保目录存在并有正确权限
mkdir -p /var/www/html
chmod 755 /var/www/html 2>/dev/null || echo "无法修改 /var/www/html 权限,将使用临时目录"
# 尝试创建 HTML 文件,如果失败则使用临时目录
if cat > /var/www/html/index.html 2>/dev/null <<'EOF'
Open WebUI Service
🤖
hello world
hello world
hello world
hello world
hello world
• hello world
• hello world
• hello world
• hello world
📝 Note:
hello world
EOF
then
echo "HTML 文件创建成功:/var/www/html/index.html"
WEB_ROOT="/var/www/html"
else
echo "无法写入 /var/www/html/index.html,使用临时目录..."
mkdir -p /tmp/www
cat > /tmp/www/index.html <<'EOF'
Open WebUI Service
🤖
Open WebUI Service
Secure AI Chat Interface
Service Status: Running
Backend accessible via Cloudflare Tunnel
🔒 Security Features:
• End-to-end encrypted tunnel
• Private backend deployment
• Automatic data backup
• No direct exposure
📝 Note:
Access the actual Open WebUI through your Cloudflare Tunnel domain.
EOF
WEB_ROOT="/tmp/www"
echo "HTML 文件创建在临时目录:/tmp/www/index.html"
fi
# 验证 HTML 文件是否创建成功
if [ ! -f "$WEB_ROOT/index.html" ]; then
echo "错误:无法创建 HTML 文件"
exit 1
fi
# 配置 Nginx
cat > /tmp/nginx.conf </dev/null || true
fi
echo "环境变量设置:"
echo "- DATA_DIR: $DATA_DIR"
echo "- PORT: $PORT"
echo "- HOST: $HOST"
echo "- ENV: $ENV"
# 查找正确的启动方式
if command -v open-webui >/dev/null 2>&1; then
echo "使用 open-webui 命令启动"
open-webui serve --host $HOST --port $PORT &
WEBUI_PID=$!
elif [ -f "/app/backend/start.sh" ]; then
echo "使用 /app/backend/start.sh 启动 Open WebUI"
cd /app/backend
chmod +x start.sh
./start.sh &
WEBUI_PID=$!
elif [ -f "/app/start.sh" ]; then
echo "使用 /app/start.sh 启动 Open WebUI"
cd /app
chmod +x start.sh
./start.sh &
WEBUI_PID=$!
elif [ -f "/tmp/openwebui/main.py" ]; then
echo "使用 Python 直接启动 Open WebUI"
cd /tmp/openwebui
python3 -m uvicorn main:app --host $HOST --port $PORT &
WEBUI_PID=$!
else
echo "错误:找不到 Open WebUI 启动方式"
echo "尝试使用 uvicorn 启动..."
cd /app/backend
python3 -m uvicorn open_webui.main:app --host $HOST --port $PORT &
WEBUI_PID=$!
fi
# 等待 Open WebUI 启动
echo "Waiting for Open WebUI to start..."
echo "Open WebUI PID: $WEBUI_PID"
# 等待更长时间并检查进程状态
for i in {1..30}; do
if kill -0 $WEBUI_PID 2>/dev/null; then
echo "Open WebUI 进程正在运行 (检查 $i/30)"
if curl -s http://localhost:8080 > /dev/null; then
echo "✅ Open WebUI 启动成功并响应请求"
break
fi
else
echo "❌ Open WebUI 进程已停止"
break
fi
sleep 2
done
# 最终检查
if ! curl -s http://localhost:8080 > /dev/null; then
echo "⚠️ WARNING: Open WebUI 可能未完全就绪,但继续运行其他服务..."
echo "请检查 Open WebUI 日志以获取更多信息"
fi
# 启动 Cloudflare Tunnel(如果配置了 TUNNEL_TOKEN)
if [ -n "$TUNNEL_TOKEN" ]; then
echo "Starting Cloudflare Tunnel..."
cloudflared tunnel run --token $TUNNEL_TOKEN 2>&1 | tee /tmp/cloudflared.log &
TUNNEL_PID=$!
echo "Cloudflare Tunnel started with PID: $TUNNEL_PID"
else
echo "TUNNEL_TOKEN not set, skipping Cloudflare Tunnel startup"
echo "Please set TUNNEL_TOKEN environment variable to enable tunnel"
fi
# 服务状态监控和保活
echo "All services started. Monitoring service status..."
while true; do
echo "=== Service Status Check $(date) ==="
# 检查 Nginx
if kill -0 $NGINX_PID 2>/dev/null; then
echo "✅ Nginx (Frontend): Running"
else
echo "❌ Nginx (Frontend): Stopped"
fi
# 检查 Open WebUI
if kill -0 $WEBUI_PID 2>/dev/null; then
echo "✅ Open WebUI: Running"
else
echo "❌ Open WebUI: Stopped"
fi
# 检查数据同步
if kill -0 $SYNC_PID 2>/dev/null; then
echo "✅ Data Sync: Running"
else
echo "❌ Data Sync: Stopped"
fi
# 检查 Cloudflare Tunnel
if [ -n "$TUNNEL_TOKEN" ] && [ -n "$TUNNEL_PID" ]; then
if kill -0 $TUNNEL_PID 2>/dev/null; then
echo "✅ Cloudflare Tunnel: Running"
else
echo "❌ Cloudflare Tunnel: Stopped"
fi
fi
# 检查端口监听
echo "Port Status:"
echo "- Port 7860 (Frontend): $(ss -tulpn | grep :7860 | wc -l) listeners"
echo "- Port 8080 (Open WebUI): $(ss -tulpn | grep :8080 | wc -l) listeners"
echo "=================================="
# 保活 ping
curl -s http://localhost:7860 > /dev/null || echo "Frontend ping failed"
curl -s http://localhost:8080 > /dev/null || echo "Open WebUI ping failed"
sleep 30
done