| #!/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)) |
| 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 |
|
|
| |
| 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配置已生成" |
|
|
| |
| 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 |
|
|
| |
| sleep 5 |
| if [ -f "$LOG_FILE" ]; then |
| echo "日志文件已生成,内容如下(前9行):" |
| head -n 9 "$LOG_FILE" |
| fi |
|
|
| |
| 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 |
|
|
| |
| echo "启动Nginx服务..." |
| nginx -t -c /tmp/nginx-config/nginx.conf || { |
| echo "错误: Nginx配置测试失败" |
| exit 1 |
| } |
| nginx -c /tmp/nginx-config/nginx.conf -g "daemon off;" & |
|
|
| |
| echo "所有服务已启动,开始实时显示日志..." |
| tail -f "$LOG_FILE" | grep -v '[GIN].*/api/health' & |
|
|
| |
| wait |