You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
7.5 KiB
7.5 KiB
部署指南
📋 文档信息
- 文档版本: v1.0.0
- 创建日期: 2024年
- 最后更新: 2024年
- 文档状态: 草稿
- 维护人员: 运维团队
🎯 部署概述
本文档描述了 Getinge Web 项目的部署流程,包括环境准备、构建打包、部署配置等步骤。
🛠️ 环境要求
服务器环境
- 操作系统: Ubuntu 18.04+ / CentOS 7+ / Windows Server 2016+
- Node.js: 版本 16.x LTS
- Nginx: 版本 1.18+
- 内存: 4GB+
- 硬盘: 20GB+ 可用空间
网络要求
- 公网 IP 地址
- 域名解析配置
- SSL 证书(HTTPS)
- 防火墙端口开放(80, 443, 3000)
🚀 部署流程
1. 环境准备
安装 Node.js
# Ubuntu/Debian
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs
# CentOS/RHEL
curl -fsSL https://rpm.nodesource.com/setup_16.x | sudo bash -
sudo yum install -y nodejs
# 验证安装
node --version
npm --version
安装 Nginx
# Ubuntu/Debian
sudo apt-get update
sudo apt-get install nginx
# CentOS/RHEL
sudo yum install epel-release
sudo yum install nginx
# 启动 Nginx
sudo systemctl start nginx
sudo systemctl enable nginx
安装 PM2
# 全局安装 PM2
sudo npm install -g pm2
# 验证安装
pm2 --version
2. 项目部署
克隆项目
# 创建部署目录
sudo mkdir -p /var/www/getinge-web
sudo chown $USER:$USER /var/www/getinge-web
# 克隆项目
cd /var/www/getinge-web
git clone <项目仓库地址> .
# 安装依赖
npm install --production
环境配置
# 创建环境配置文件
cp .env.example .env
# 编辑环境配置
nano .env
# 配置内容示例
NODE_ENV=production
PORT=3000
API_BASE_URL=https://api.example.com
JWT_SECRET=your-secret-key
构建项目
# 安装构建依赖
npm install
# 构建生产版本
npm run build:prod
# 验证构建结果
ls -la dist/
3. 服务配置
PM2 配置
# 创建 PM2 配置文件
nano ecosystem.config.js
# 配置文件内容
module.exports = {
apps: [{
name: 'getinge-web',
script: 'server.js',
instances: 'max',
exec_mode: 'cluster',
env: {
NODE_ENV: 'production',
PORT: 3000
},
error_file: './logs/err.log',
out_file: './logs/out.log',
log_file: './logs/combined.log',
time: true
}]
};
# 启动服务
pm2 start ecosystem.config.js
pm2 save
pm2 startup
Nginx 配置
# 创建 Nginx 配置文件
sudo nano /etc/nginx/sites-available/getinge-web
# 配置文件内容
server {
listen 80;
server_name your-domain.com;
# 重定向到 HTTPS
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name your-domain.com;
# SSL 配置
ssl_certificate /path/to/your/certificate.crt;
ssl_certificate_key /path/to/your/private.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384;
# 静态文件缓存
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
# API 代理
location /api/ {
proxy_pass http://localhost:3000;
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;
proxy_cache_bypass $http_upgrade;
}
# 前端应用
location / {
root /var/www/getinge-web/dist;
try_files $uri $uri/ /index.html;
# 安全头
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
}
}
# 启用站点
sudo ln -s /etc/nginx/sites-available/getinge-web /etc/nginx/sites-enabled/
# 测试配置
sudo nginx -t
# 重启 Nginx
sudo systemctl restart nginx
4. SSL 证书配置
Let's Encrypt 证书
# 安装 Certbot
sudo apt-get install certbot python3-certbot-nginx
# 获取证书
sudo certbot --nginx -d your-domain.com
# 自动续期
sudo crontab -e
# 添加以下行
0 12 * * * /usr/bin/certbot renew --quiet
🔧 环境配置
开发环境
# 环境变量
NODE_ENV=development
PORT=8080
API_BASE_URL=http://localhost:3000
DEBUG=true
测试环境
# 环境变量
NODE_ENV=testing
PORT=3000
API_BASE_URL=https://test-api.example.com
DEBUG=false
生产环境
# 环境变量
NODE_ENV=production
PORT=3000
API_BASE_URL=https://api.example.com
DEBUG=false
📊 监控配置
PM2 监控
# 查看应用状态
pm2 status
# 查看日志
pm2 logs getinge-web
# 监控面板
pm2 monit
# 重启应用
pm2 restart getinge-web
系统监控
# 安装监控工具
sudo apt-get install htop iotop nethogs
# 查看系统资源
htop
iotop
nethogs
日志管理
# 创建日志目录
mkdir -p /var/www/getinge-web/logs
# 配置日志轮转
sudo nano /etc/logrotate.d/getinge-web
# 配置内容
/var/www/getinge-web/logs/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 644 www-data www-data
postrotate
pm2 reloadLogs
endscript
}
🚀 自动化部署
CI/CD 配置
# .github/workflows/deploy.yml
name: Deploy to Production
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '16'
- name: Install dependencies
run: npm ci
- name: Build project
run: npm run build:prod
- name: Deploy to server
uses: appleboy/ssh-action@v0.1.4
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.KEY }}
script: |
cd /var/www/getinge-web
git pull origin main
npm install --production
npm run build:prod
pm2 restart getinge-web
部署脚本
#!/bin/bash
# deploy.sh
echo "开始部署..."
# 拉取最新代码
git pull origin main
# 安装依赖
npm install --production
# 构建项目
npm run build:prod
# 重启服务
pm2 restart getinge-web
echo "部署完成!"
🔍 故障排查
常见问题
服务无法启动
# 检查端口占用
sudo netstat -tlnp | grep :3000
# 检查日志
pm2 logs getinge-web
# 检查环境变量
pm2 env getinge-web
静态文件无法访问
# 检查文件权限
ls -la /var/www/getinge-web/dist/
# 检查 Nginx 配置
sudo nginx -t
# 检查 Nginx 状态
sudo systemctl status nginx
SSL 证书问题
# 检查证书状态
sudo certbot certificates
# 手动续期
sudo certbot renew --dry-run
# 检查证书文件
sudo ls -la /etc/letsencrypt/live/your-domain.com/
📝 变更记录
| 版本 | 日期 | 变更内容 | 变更人 |
|---|---|---|---|
| v1.0.0 | 2024年 | 初始版本 | 运维团队 |
如有疑问,请联系运维团队