Files
happy-life-star/setup-nginx.sh
T
peanut 26f0cdd760 feat: 完成项目整理优化和生产环境配置
🧹 项目结构优化:
- 删除重复和过时的文件
- 整理文档到docs目录结构
- 优化配置文件到configs目录
- 创建清晰的PROJECT_STRUCTURE.md

🔧 中间件配置:
- 重启MySQL/Redis/Nacos中间件
- 使用现有数据目录,确保数据完整性
- 统一密码配置: MySQL(EmotionMuseum2025*#), Nacos(Peanut2817*#)

🌐 Nginx配置:
- 配置前端路径: /emotion-museum
- 配置API代理: /api/ -> 19000
- 配置WebSocket代理: /ws/ -> 19007
- 添加健康检查端点: /health

📋 部署脚本优化:
- restart-middleware.sh - 中间件重启脚本
- setup-nginx.sh - Nginx配置脚本
- cleanup-project.sh - 项目清理脚本
- one-click-deploy.sh - 一键部署脚本

📖 文档完善:
- DEPLOYMENT_FINAL.md - 最终部署指南
- PROJECT_STRUCTURE.md - 项目结构说明
- 完整的运维和故障排查指南

 生产环境就绪:
- 中间件: MySQL/Redis/Nacos 运行正常
- Nginx: 反向代理配置完成
- 访问地址: http://47.111.10.27/emotion-museum
- 健康检查: http://47.111.10.27/health

🎯 项目现状:
- 10个微服务模块完整
- 前后端分离架构
- 容器化部署
- 统一配置管理
- 完整的部署和运维体系
2025-07-21 13:55:36 +08:00

240 lines
6.5 KiB
Bash
Executable File

#!/bin/bash
# 配置远程服务器Nginx脚本
# 作者: emotion-museum
# 日期: 2025-07-21
set -e
REMOTE_HOST="root@47.111.10.27"
NGINX_CONF_PATH="/www/server/nginx/conf"
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log_info() {
echo -e "${BLUE}[INFO]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
# 检查Nginx状态
check_nginx() {
log_info "检查Nginx状态..."
ssh "$REMOTE_HOST" "
echo '=== Nginx进程 ==='
ps aux | grep nginx | grep -v grep || echo 'Nginx未运行'
echo ''
echo '=== Nginx配置目录 ==='
ls -la $NGINX_CONF_PATH/ || echo 'Nginx配置目录不存在'
echo ''
echo '=== 端口80监听 ==='
netstat -tlnp | grep :80 || echo '端口80未监听'
"
}
# 备份现有配置
backup_nginx_config() {
log_info "备份现有Nginx配置..."
ssh "$REMOTE_HOST" "
mkdir -p $NGINX_CONF_PATH/backup/\$(date +%Y%m%d_%H%M%S)
cp $NGINX_CONF_PATH/nginx.conf $NGINX_CONF_PATH/backup/\$(date +%Y%m%d_%H%M%S)/ 2>/dev/null || true
cp -r $NGINX_CONF_PATH/conf.d $NGINX_CONF_PATH/backup/\$(date +%Y%m%d_%H%M%S)/ 2>/dev/null || true
"
log_success "Nginx配置已备份"
}
# 创建情感博物馆站点配置
create_site_config() {
log_info "创建情感博物馆站点配置..."
ssh "$REMOTE_HOST" "cat > $NGINX_CONF_PATH/conf.d/emotion-museum.conf << 'EOF'
# 情感博物馆站点配置
server {
listen 80;
server_name 47.111.10.27;
# 前端静态文件
location /emotion-museum {
alias /data/www/emotion-museum;
index index.html;
try_files \$uri \$uri/ /emotion-museum/index.html;
# 静态资源缓存
location ~* \\.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)\$ {
expires 1y;
add_header Cache-Control \"public, immutable\";
}
}
# API网关代理
location /api/ {
proxy_pass http://localhost:19000/;
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 30s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
}
# WebSocket代理
location /ws/ {
proxy_pass http://localhost:19007/;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection \"upgrade\";
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;
}
# 健康检查
location /health {
access_log off;
return 200 \"healthy\";
add_header Content-Type text/plain;
}
# 日志配置
access_log /var/log/nginx/emotion-museum.access.log;
error_log /var/log/nginx/emotion-museum.error.log;
}
# 微服务直接访问(用于调试)
server {
listen 80;
server_name api.47.111.10.27;
# 网关服务
location /gateway/ {
proxy_pass http://localhost:19000/;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
}
# 用户服务
location /user/ {
proxy_pass http://localhost:19001/;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
}
# AI服务
location /ai/ {
proxy_pass http://localhost:19002/;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
}
# 认证服务
location /auth/ {
proxy_pass http://localhost:19008/;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
}
}
EOF"
log_success "站点配置创建完成"
}
# 测试Nginx配置
test_nginx_config() {
log_info "测试Nginx配置..."
if ssh "$REMOTE_HOST" "nginx -t"; then
log_success "Nginx配置测试通过"
else
log_error "Nginx配置测试失败"
return 1
fi
}
# 重载Nginx配置
reload_nginx() {
log_info "重载Nginx配置..."
if ssh "$REMOTE_HOST" "systemctl reload nginx"; then
log_success "Nginx配置重载成功"
else
log_warning "Nginx重载失败,尝试重启..."
if ssh "$REMOTE_HOST" "systemctl restart nginx"; then
log_success "Nginx重启成功"
else
log_error "Nginx重启失败"
return 1
fi
fi
}
# 检查配置结果
check_result() {
log_info "检查配置结果..."
ssh "$REMOTE_HOST" "
echo '=== Nginx状态 ==='
systemctl status nginx --no-pager -l
echo ''
echo '=== 端口监听 ==='
netstat -tlnp | grep :80
echo ''
echo '=== 测试访问 ==='
curl -I http://localhost/health 2>/dev/null || echo '健康检查失败'
"
}
# 主函数
main() {
log_info "🔧 开始配置远程服务器Nginx..."
# 检查SSH连接
if ! ssh -o ConnectTimeout=10 "$REMOTE_HOST" "echo 'SSH连接成功'" > /dev/null 2>&1; then
log_error "无法连接到远程服务器: $REMOTE_HOST"
exit 1
fi
check_nginx
backup_nginx_config
create_site_config
test_nginx_config
reload_nginx
check_result
log_success "🎉 Nginx配置完成!"
echo ""
echo "📋 访问地址:"
echo " 前端应用: http://47.111.10.27/emotion-museum"
echo " API接口: http://47.111.10.27/api/"
echo " WebSocket: ws://47.111.10.27/ws/"
echo " 健康检查: http://47.111.10.27/health"
echo ""
echo "🔧 调试地址:"
echo " 网关服务: http://api.47.111.10.27/gateway/"
echo " 用户服务: http://api.47.111.10.27/user/"
echo " AI服务: http://api.47.111.10.27/ai/"
echo " 认证服务: http://api.47.111.10.27/auth/"
}
# 执行主函数
main "$@"