File size: 6,245 Bytes
5459210 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
#!/bin/sh
set -e
# 防止重复执行(增强锁机制)
LOCK_FILE=/tmp/pansou_start.lock
if [ -f "$LOCK_FILE" ]; then
LOCK_PID=$(cat "$LOCK_FILE")
if ps | grep -q "[${LOCK_PID}]"; then
echo "错误: start.sh已在运行(锁文件 $LOCK_FILE,PID $LOCK_PID)"
exit 1
else
echo "警告: 发现旧锁文件 $LOCK_FILE,已移除"
rm -f "$LOCK_FILE"
fi
fi
echo $$ > "$LOCK_FILE"
trap 'rm -f $LOCK_FILE' EXIT
# 默认值
export PANSOU_PORT=${PANSOU_PORT:-8888}
export PANSOU_HOST=${PANSOU_HOST:-127.0.0.1}
export DOMAIN=${DOMAIN:-localhost}
echo "正在启动PanSou服务,配置信息如下:"
echo "- 后端地址: ${PANSOU_HOST}:${PANSOU_PORT}"
echo "- 域名: ${DOMAIN}"
echo "- 前端目录: /app/frontend/dist"
# 创建必要目录
mkdir -p /data/pansou_data /data/pansou_logs /tmp/nginx_logs /tmp/nginx-config /tmp/nginx-run \
/tmp/nginx-tmp/client_temp /tmp/nginx-tmp/proxy_temp /tmp/nginx-tmp/fastcgi_temp \
/tmp/nginx-tmp/uwsgi_temp /tmp/nginx-tmp/scgi_temp || {
echo "错误: 无法创建必要目录"
exit 1
}
# 日志轮转
LOG_FILE=/data/pansou_logs/pansou.log
MAX_SIZE=$((10 * 1024 * 1024)) # 10MB
if [ -f "$LOG_FILE" ]; then
SIZE=$(stat -f %z "$LOG_FILE" 2>/dev/null || stat -c %s "$LOG_FILE")
if [ "$SIZE" -gt "$MAX_SIZE" ]; then
echo "日志文件大小 ($SIZE bytes) 超过限制 ($MAX_SIZE bytes),执行轮转"
mv "$LOG_FILE" "${LOG_FILE}.$(date +%F-%H%M%S)"
find /data/pansou_logs -name "pansou.log.*" -mtime +7 -delete
fi
fi
# 生成Nginx配置
cat > /tmp/nginx-config/nginx.conf << EOF
worker_processes auto;
error_log /dev/stderr error;
pid /tmp/nginx-run/nginx.pid;
events { worker_connections 1024; }
http {
client_body_temp_path /tmp/nginx-tmp/client_temp;
proxy_temp_path /tmp/nginx-tmp/proxy_temp;
fastcgi_temp_path /tmp/nginx-tmp/fastcgi_temp;
uwsgi_temp_path /tmp/nginx-tmp/uwsgi_temp;
scgi_temp_path /tmp/nginx-tmp/scgi_temp;
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /dev/stdout main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 20M;
server {
listen 7860;
server_name ${DOMAIN};
location = /api/health {
proxy_pass http://${PANSOU_HOST}:${PANSOU_PORT}/api/health;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_connect_timeout 5s;
proxy_read_timeout 10s;
proxy_send_timeout 5s;
proxy_buffering off;
}
location = /api/search {
proxy_pass http://${PANSOU_HOST}:${PANSOU_PORT}/api/search;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_set_header Referer \$http_referer;
proxy_connect_timeout 15s;
proxy_read_timeout 60s;
proxy_send_timeout 15s;
proxy_buffering off;
}
location /api/ {
proxy_pass http://${PANSOU_HOST}:${PANSOU_PORT}/api/;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_set_header Referer \$http_referer;
proxy_buffering off;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
root /app/frontend/dist;
expires 30d;
add_header Cache-Control "public, max-age=2592000";
add_header X-Content-Type-Options nosniff;
}
location / {
root /app/frontend/dist;
index index.html;
try_files \$uri \$uri/ /index.html;
location ~* \.html$ {
add_header Cache-Control "no-cache, no-store, must-revalidate";
add_header Pragma "no-cache";
add_header Expires "0";
}
}
}
}
EOF
echo "Nginx配置已生成"
# 启动pansou并过滤空日志
cd /app
echo "启动pansou..."
/app/pansou 2>&1 | grep -v '\[\] "" *"" *"" *""' > "$LOG_FILE" &
PANSOU_PID=$!
echo "pansou进程ID: $PANSOU_PID"
if ps | grep -q "[${PANSOU_PID}]"; then
echo "pansou进程正在运行"
else
echo "错误: pansou进程未能启动"
sleep 2
if [ -f "$LOG_FILE" ]; then
echo "pansou日志内容(前9行):"
head -n 9 "$LOG_FILE"
else
echo "错误: 日志文件未生成"
fi
exit 1
fi
# 等待pansou日志初始化
sleep 5
if [ -f "$LOG_FILE" ]; then
echo "日志文件已生成,内容如下(前9行):"
head -n 9 "$LOG_FILE"
fi
# 等待pansou服务启动
echo "等待后端服务启动..."
for i in $(seq 1 30); do
if curl -f http://${PANSOU_HOST}:${PANSOU_PORT}/api/health >/dev/null 2>&1; then
echo "后端服务启动成功"
break
fi
echo "等待后端服务... ($i/30)"
sleep 1
done
# 检查后端服务
if ! curl -f http://${PANSOU_HOST}:${PANSOU_PORT}/api/health >/dev/null 2>&1; then
echo "错误: 后端服务启动失败"
if [ -f "$LOG_FILE" ]; then
echo "pansou日志内容(前20行):"
head -n 20 "$LOG_FILE"
fi
exit 1
fi
# 启动Nginx
echo "启动Nginx服务..."
nginx -t -c /tmp/nginx-config/nginx.conf || {
echo "错误: Nginx配置测试失败"
exit 1
}
nginx -c /tmp/nginx-config/nginx.conf -g "daemon off;" &
# 显示日志(过滤/health)
echo "所有服务已启动,开始实时显示日志..."
tail -f "$LOG_FILE" | grep -v '[GIN].*/api/health' &
# 等待后台进程
wait |