Spaces:
dss11
/
Paused

ow / start.sh
Your Name
2
a79ea2b
Raw History Blame Contribute Delete
11.4 kB
#!/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'
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Open WebUI Service</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
color: white;
}
.container {
text-align: center;
background: rgba(255, 255, 255, 0.1);
padding: 3rem;
border-radius: 20px;
backdrop-filter: blur(10px);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
max-width: 500px;
}
h1 {
font-size: 2.5rem;
margin-bottom: 1rem;
font-weight: 300;
}
.subtitle {
font-size: 1.2rem;
margin-bottom: 2rem;
opacity: 0.9;
}
.status {
background: rgba(76, 175, 80, 0.2);
padding: 1rem;
border-radius: 10px;
border: 1px solid rgba(76, 175, 80, 0.3);
margin-bottom: 2rem;
}
.info {
background: rgba(255, 255, 255, 0.1);
padding: 1.5rem;
border-radius: 10px;
text-align: left;
font-size: 0.9rem;
line-height: 1.6;
}
.emoji {
font-size: 3rem;
margin-bottom: 1rem;
}
</style>
</head>
<body>
<div class="container">
<div class="emoji">🤖</div>
<h1>hello world</h1>
<div class="subtitle">hello world</div>
<div class="status">
<strong>hello world</strong><br>
hello world
</div>
<div class="info">
<strong>hello world</strong><br>
• hello world<br>
• hello world<br>
• hello world<br>
• hello world<br><br>
<strong>📝 Note:</strong><br>
hello world
</div>
</div>
</body>
</html>
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'
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Open WebUI Service</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
color: white;
}
.container {
text-align: center;
background: rgba(255, 255, 255, 0.1);
padding: 3rem;
border-radius: 20px;
backdrop-filter: blur(10px);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
max-width: 500px;
}
h1 {
font-size: 2.5rem;
margin-bottom: 1rem;
font-weight: 300;
}
.subtitle {
font-size: 1.2rem;
margin-bottom: 2rem;
opacity: 0.9;
}
.status {
background: rgba(76, 175, 80, 0.2);
padding: 1rem;
border-radius: 10px;
border: 1px solid rgba(76, 175, 80, 0.3);
margin-bottom: 2rem;
}
.info {
background: rgba(255, 255, 255, 0.1);
padding: 1.5rem;
border-radius: 10px;
text-align: left;
font-size: 0.9rem;
line-height: 1.6;
}
.emoji {
font-size: 3rem;
margin-bottom: 1rem;
}
</style>
</head>
<body>
<div class="container">
<div class="emoji">🤖</div>
<h1>Open WebUI Service</h1>
<div class="subtitle">Secure AI Chat Interface</div>
<div class="status">
<strong>Service Status: Running</strong><br>
Backend accessible via Cloudflare Tunnel
</div>
<div class="info">
<strong>🔒 Security Features:</strong><br>
• End-to-end encrypted tunnel<br>
• Private backend deployment<br>
• Automatic data backup<br>
• No direct exposure<br><br>
<strong>📝 Note:</strong><br>
Access the actual Open WebUI through your Cloudflare Tunnel domain.
</div>
</div>
</body>
</html>
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 <<EOF
worker_processes auto;
pid /tmp/nginx.pid;
error_log /tmp/nginx_error.log;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /tmp/nginx_access.log;
client_body_temp_path /tmp/client_body;
fastcgi_temp_path /tmp/fastcgi_temp;
proxy_temp_path /tmp/proxy_temp;
scgi_temp_path /tmp/scgi_temp;
uwsgi_temp_path /tmp/uwsgi_temp;
server {
listen 7860;
root $WEB_ROOT;
index index.html;
location / {
try_files \$uri \$uri/ =404;
}
}
}
EOF
# 创建临时目录
mkdir -p /tmp/client_body /tmp/fastcgi_temp /tmp/proxy_temp /tmp/scgi_temp /tmp/uwsgi_temp
# 启动 Nginx(前端展示)
echo "Starting Nginx for frontend display..."
nginx -c /tmp/nginx.conf -g "daemon off;" &
NGINX_PID=$!
# 启动数据同步服务(后台)
echo "Starting data sync service..."
/sync_data.sh &
SYNC_PID=$!
# 等待一下让服务启动
sleep 3
# 启动 Open WebUI(后台,内部端口 8080)
echo "Starting Open WebUI service..."
# 运行环境检查
if [ -f "/check_openwebui_env.sh" ]; then
echo "运行 Open WebUI 环境检查..."
chmod +x /check_openwebui_env.sh
/check_openwebui_env.sh
fi
# 设置 Open WebUI 必需的环境变量
export DATA_DIR="/app/backend/data"
export WEBUI_SECRET_KEY="${WEBUI_SECRET_KEY:-$(openssl rand -hex 32)}"
export WEBUI_JWT_SECRET_KEY="${WEBUI_JWT_SECRET_KEY:-$(openssl rand -hex 32)}"
export PORT=8080
export HOST=0.0.0.0
export ENV=production
export PYTHONPATH="/app/backend:$PYTHONPATH"
# 创建数据目录并设置权限
mkdir -p "$DATA_DIR"
chmod 777 "$DATA_DIR"
# 创建可写的工作目录
mkdir -p /tmp/openwebui
cd /tmp/openwebui
# 复制必要的文件到可写目录
if [ -d "/app/backend" ]; then
cp -r /app/backend/* /tmp/openwebui/ 2>/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