production.sh 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #!/bin/bash
  2. # Tolan 生产环境部署脚本
  3. # 用于生产环境的部署和管理
  4. set -e
  5. # 颜色定义
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[1;33m'
  9. BLUE='\033[0;34m'
  10. NC='\033[0m'
  11. log_info() {
  12. echo -e "${BLUE}[PROD]${NC} $1"
  13. }
  14. log_success() {
  15. echo -e "${GREEN}[PROD]${NC} $1"
  16. }
  17. log_warning() {
  18. echo -e "${YELLOW}[PROD]${NC} $1"
  19. }
  20. log_error() {
  21. echo -e "${RED}[PROD]${NC} $1"
  22. }
  23. # 配置变量
  24. DEPLOY_DIR="/opt/tolan"
  25. SERVICE_USER="tolan"
  26. NGINX_CONFIG="/etc/nginx/sites-available/tolan"
  27. SYSTEMD_SERVICE="/etc/systemd/system/tolan-backend.service"
  28. # 检查权限
  29. check_permissions() {
  30. if [[ $EUID -ne 0 ]]; then
  31. log_error "此脚本需要 root 权限运行"
  32. log_info "请使用: sudo $0 $@"
  33. exit 1
  34. fi
  35. }
  36. # 安装系统依赖
  37. install_dependencies() {
  38. log_info "安装系统依赖..."
  39. # 更新包管理器
  40. apt update
  41. # 安装基础依赖
  42. apt install -y \
  43. python3 \
  44. python3-pip \
  45. python3-venv \
  46. nodejs \
  47. npm \
  48. nginx \
  49. ffmpeg \
  50. libsndfile1 \
  51. supervisor \
  52. certbot \
  53. python3-certbot-nginx
  54. log_success "系统依赖安装完成"
  55. }
  56. # 创建部署用户
  57. create_user() {
  58. if ! id "$SERVICE_USER" &>/dev/null; then
  59. log_info "创建部署用户: $SERVICE_USER"
  60. useradd --system --create-home --shell /bin/bash $SERVICE_USER
  61. usermod -aG www-data $SERVICE_USER
  62. log_success "用户 $SERVICE_USER 创建完成"
  63. else
  64. log_info "用户 $SERVICE_USER 已存在"
  65. fi
  66. }
  67. # 部署应用代码
  68. deploy_application() {
  69. log_info "部署应用代码到 $DEPLOY_DIR"
  70. # 创建部署目录
  71. mkdir -p $DEPLOY_DIR
  72. # 复制代码
  73. log_info "复制应用代码..."
  74. cp -r frontend $DEPLOY_DIR/
  75. cp -r backend $DEPLOY_DIR/
  76. cp -r scripts $DEPLOY_DIR/
  77. cp start.sh $DEPLOY_DIR/
  78. # 设置权限
  79. chown -R $SERVICE_USER:$SERVICE_USER $DEPLOY_DIR
  80. chmod +x $DEPLOY_DIR/start.sh
  81. chmod +x $DEPLOY_DIR/scripts/*.sh
  82. log_success "应用代码部署完成"
  83. }
  84. # 构建前端
  85. build_frontend() {
  86. log_info "构建前端应用..."
  87. cd $DEPLOY_DIR/frontend
  88. # 切换到部署用户
  89. sudo -u $SERVICE_USER bash << 'EOF'
  90. # 安装依赖
  91. npm ci --production
  92. # 设置生产环境变量
  93. export REACT_APP_WS_URL=wss://your-domain.com/ws
  94. export REACT_APP_ENV=production
  95. # 构建应用
  96. npm run build
  97. EOF
  98. log_success "前端构建完成"
  99. }
  100. # 设置后端环境
  101. setup_backend() {
  102. log_info "设置后端环境..."
  103. cd $DEPLOY_DIR/backend
  104. # 切换到部署用户
  105. sudo -u $SERVICE_USER bash << 'EOF'
  106. # 创建虚拟环境
  107. python3 -m venv venv
  108. source venv/bin/activate
  109. # 安装依赖
  110. pip install --upgrade pip
  111. pip install -r requirements.txt
  112. # 复制环境配置
  113. if [ ! -f .env ]; then
  114. cp .env.example .env
  115. fi
  116. EOF
  117. log_warning "请编辑 $DEPLOY_DIR/backend/.env 配置生产环境参数"
  118. log_success "后端环境设置完成"
  119. }
  120. # 配置 Nginx
  121. setup_nginx() {
  122. log_info "配置 Nginx..."
  123. cat > $NGINX_CONFIG << 'EOF'
  124. server {
  125. listen 80;
  126. server_name your-domain.com; # 替换为你的域名
  127. # 前端静态文件
  128. location / {
  129. root /opt/tolan/frontend/build;
  130. index index.html index.htm;
  131. try_files $uri $uri/ /index.html;
  132. # 缓存静态资源
  133. location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
  134. expires 1y;
  135. add_header Cache-Control "public, immutable";
  136. }
  137. }
  138. # WebSocket 代理
  139. location /ws {
  140. proxy_pass http://127.0.0.1:8765;
  141. proxy_http_version 1.1;
  142. proxy_set_header Upgrade $http_upgrade;
  143. proxy_set_header Connection "upgrade";
  144. proxy_set_header Host $host;
  145. proxy_set_header X-Real-IP $remote_addr;
  146. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  147. proxy_set_header X-Forwarded-Proto $scheme;
  148. # WebSocket 超时设置
  149. proxy_read_timeout 86400;
  150. proxy_send_timeout 86400;
  151. }
  152. # 健康检查
  153. location /health {
  154. proxy_pass http://127.0.0.1:8765/health;
  155. proxy_set_header Host $host;
  156. proxy_set_header X-Real-IP $remote_addr;
  157. }
  158. # 安全头
  159. add_header X-Frame-Options "SAMEORIGIN" always;
  160. add_header X-XSS-Protection "1; mode=block" always;
  161. add_header X-Content-Type-Options "nosniff" always;
  162. add_header Referrer-Policy "no-referrer-when-downgrade" always;
  163. add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
  164. }
  165. EOF
  166. # 启用站点
  167. ln -sf $NGINX_CONFIG /etc/nginx/sites-enabled/
  168. rm -f /etc/nginx/sites-enabled/default
  169. # 测试配置
  170. nginx -t
  171. log_success "Nginx 配置完成"
  172. }
  173. # 配置 Systemd 服务
  174. setup_systemd() {
  175. log_info "配置 Systemd 服务..."
  176. cat > $SYSTEMD_SERVICE << EOF
  177. [Unit]
  178. Description=Tolan Backend Server
  179. After=network.target
  180. [Service]
  181. Type=exec
  182. User=$SERVICE_USER
  183. Group=$SERVICE_USER
  184. WorkingDirectory=$DEPLOY_DIR/backend
  185. Environment=PATH=$DEPLOY_DIR/backend/venv/bin
  186. EnvironmentFile=$DEPLOY_DIR/backend/.env
  187. ExecStart=$DEPLOY_DIR/backend/venv/bin/python run.py
  188. Restart=always
  189. RestartSec=5
  190. StandardOutput=journal
  191. StandardError=journal
  192. # 安全设置
  193. NoNewPrivileges=yes
  194. PrivateTmp=yes
  195. ProtectSystem=strict
  196. ProtectHome=yes
  197. ReadWritePaths=$DEPLOY_DIR
  198. [Install]
  199. WantedBy=multi-user.target
  200. EOF
  201. # 重载 systemd
  202. systemctl daemon-reload
  203. log_success "Systemd 服务配置完成"
  204. }
  205. # 启动服务
  206. start_services() {
  207. log_info "启动服务..."
  208. # 启动后端服务
  209. systemctl enable tolan-backend
  210. systemctl start tolan-backend
  211. # 启动 Nginx
  212. systemctl enable nginx
  213. systemctl restart nginx
  214. log_success "服务启动完成"
  215. }
  216. # 设置 SSL 证书
  217. setup_ssl() {
  218. local domain=$1
  219. if [ -z "$domain" ]; then
  220. log_warning "跳过 SSL 设置,未提供域名"
  221. return
  222. fi
  223. log_info "为域名 $domain 设置 SSL 证书..."
  224. # 使用 Certbot 获取证书
  225. certbot --nginx -d $domain --non-interactive --agree-tos --email admin@$domain
  226. log_success "SSL 证书设置完成"
  227. }
  228. # 检查服务状态
  229. check_status() {
  230. log_info "检查服务状态..."
  231. # 检查后端服务
  232. if systemctl is-active --quiet tolan-backend; then
  233. log_success "后端服务运行正常"
  234. else
  235. log_error "后端服务异常"
  236. systemctl status tolan-backend --no-pager
  237. fi
  238. # 检查 Nginx
  239. if systemctl is-active --quiet nginx; then
  240. log_success "Nginx 运行正常"
  241. else
  242. log_error "Nginx 异常"
  243. systemctl status nginx --no-pager
  244. fi
  245. # 检查端口
  246. log_info "端口监听状态:"
  247. netstat -tlnp | grep -E "(80|443|8765)"
  248. }
  249. # 显示部署信息
  250. show_deploy_info() {
  251. log_success "🎉 Tolan 生产环境部署完成!"
  252. echo ""
  253. log_info "📁 部署目录: $DEPLOY_DIR"
  254. log_info "👤 运行用户: $SERVICE_USER"
  255. log_info "🌐 Web 服务: http://your-domain.com"
  256. log_info "🔗 WebSocket: ws://your-domain.com/ws"
  257. echo ""
  258. log_info "📋 管理命令:"
  259. log_info " systemctl status tolan-backend # 查看后端状态"
  260. log_info " systemctl restart tolan-backend # 重启后端"
  261. log_info " systemctl status nginx # 查看 Nginx 状态"
  262. log_info " journalctl -u tolan-backend -f # 查看后端日志"
  263. echo ""
  264. log_warning "⚠️ 重要提醒:"
  265. log_warning "1. 请编辑 $DEPLOY_DIR/backend/.env 配置 API 密钥"
  266. log_warning "2. 请在 Nginx 配置中替换域名"
  267. log_warning "3. 建议设置防火墙规则"
  268. log_warning "4. 定期备份数据和配置"
  269. }
  270. # 显示帮助
  271. show_help() {
  272. echo "Tolan 生产环境部署脚本"
  273. echo ""
  274. echo "用法: $0 [命令] [选项]"
  275. echo ""
  276. echo "命令:"
  277. echo " install 完整安装部署"
  278. echo " deploy 部署应用代码"
  279. echo " start 启动服务"
  280. echo " stop 停止服务"
  281. echo " restart 重启服务"
  282. echo " status 查看状态"
  283. echo " ssl <domain> 设置 SSL 证书"
  284. echo " help 显示帮助"
  285. echo ""
  286. echo "示例:"
  287. echo " $0 install # 完整部署"
  288. echo " $0 ssl example.com # 设置 SSL"
  289. echo " $0 status # 查看状态"
  290. }
  291. # 主函数
  292. main() {
  293. case "${1:-install}" in
  294. "install")
  295. check_permissions
  296. log_info "开始 Tolan 生产环境部署..."
  297. install_dependencies
  298. create_user
  299. deploy_application
  300. build_frontend
  301. setup_backend
  302. setup_nginx
  303. setup_systemd
  304. start_services
  305. show_deploy_info
  306. ;;
  307. "deploy")
  308. check_permissions
  309. deploy_application
  310. build_frontend
  311. setup_backend
  312. systemctl restart tolan-backend
  313. systemctl reload nginx
  314. log_success "应用重新部署完成"
  315. ;;
  316. "start")
  317. check_permissions
  318. start_services
  319. ;;
  320. "stop")
  321. check_permissions
  322. systemctl stop tolan-backend
  323. systemctl stop nginx
  324. log_success "服务已停止"
  325. ;;
  326. "restart")
  327. check_permissions
  328. systemctl restart tolan-backend
  329. systemctl restart nginx
  330. log_success "服务已重启"
  331. ;;
  332. "status")
  333. check_status
  334. ;;
  335. "ssl")
  336. check_permissions
  337. setup_ssl "$2"
  338. ;;
  339. "help"|"-h"|"--help")
  340. show_help
  341. ;;
  342. *)
  343. log_error "未知命令: $1"
  344. show_help
  345. exit 1
  346. ;;
  347. esac
  348. }
  349. main "$@"