feat: 添加部署脚本和服务器初始化脚本
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 轻远电力ERP部署脚本
|
||||
# 使用方法: ./deploy.sh
|
||||
|
||||
set -e
|
||||
|
||||
echo "🚀 开始部署轻远电力ERP系统..."
|
||||
|
||||
# 配置
|
||||
APP_DIR="/opt/company-finance-system"
|
||||
BACKUP_DIR="/opt/backups"
|
||||
LOG_DIR="/var/log/company-finance"
|
||||
|
||||
# 创建目录
|
||||
echo "📁 创建目录..."
|
||||
sudo mkdir -p $APP_DIR
|
||||
sudo mkdir -p $BACKUP_DIR
|
||||
sudo mkdir -p $LOG_DIR
|
||||
sudo chown -R ubuntu:ubuntu $APP_DIR
|
||||
sudo chown -R ubuntu:ubuntu $BACKUP_DIR
|
||||
|
||||
# 进入应用目录
|
||||
cd $APP_DIR
|
||||
|
||||
# 备份现有数据(如果存在)
|
||||
if [ -d "$APP_DIR/backend" ]; then
|
||||
echo "💾 备份现有数据..."
|
||||
BACKUP_NAME="backup_$(date +%Y%m%d_%H%M%S)"
|
||||
cp -r $APP_DIR/backend/database.sqlite $BACKUP_DIR/$BACKUP_NAME.sqlite 2>/dev/null || true
|
||||
echo "✅ 数据已备份到 $BACKUP_DIR/$BACKUP_NAME.sqlite"
|
||||
fi
|
||||
|
||||
# 克隆或更新代码
|
||||
echo "📥 下载最新代码..."
|
||||
if [ -d "$APP_DIR/.git" ]; then
|
||||
git pull origin main
|
||||
else
|
||||
git clone https://github.com/a273825743/company-finance-system.git .
|
||||
fi
|
||||
|
||||
# 安装后端依赖
|
||||
echo "📦 安装后端依赖..."
|
||||
cd $APP_DIR/backend
|
||||
npm install
|
||||
|
||||
# 安装前端依赖并构建
|
||||
echo "📦 安装前端依赖并构建..."
|
||||
cd $APP_DIR/frontend
|
||||
npm install
|
||||
npm run build
|
||||
|
||||
# 创建PM2配置文件
|
||||
echo "⚙️ 配置PM2..."
|
||||
cat > $APP_DIR/ecosystem.config.js << 'EOF'
|
||||
module.exports = {
|
||||
apps: [
|
||||
{
|
||||
name: 'company-finance-backend',
|
||||
script: './backend/final-backend.js',
|
||||
cwd: '/opt/company-finance-system',
|
||||
instances: 1,
|
||||
autorestart: true,
|
||||
watch: false,
|
||||
max_memory_restart: '1G',
|
||||
env: {
|
||||
NODE_ENV: 'production',
|
||||
PORT: 3001
|
||||
},
|
||||
error_file: '/var/log/company-finance/err.log',
|
||||
out_file: '/var/log/company-finance/out.log',
|
||||
log_date_format: 'YYYY-MM-DD HH:mm:ss Z'
|
||||
}
|
||||
]
|
||||
};
|
||||
EOF
|
||||
|
||||
# 使用PM2启动/重启服务
|
||||
echo "🚀 启动后端服务..."
|
||||
cd $APP_DIR
|
||||
pm2 startOrRestart ecosystem.config.js
|
||||
|
||||
# 配置Nginx
|
||||
echo "🌐 配置Nginx..."
|
||||
sudo tee /etc/nginx/sites-available/company-finance << 'EOF'
|
||||
server {
|
||||
listen 80;
|
||||
server_name 43.161.248.209;
|
||||
|
||||
# 前端静态文件
|
||||
location / {
|
||||
root /opt/company-finance-system/frontend/dist;
|
||||
index index.html;
|
||||
try_files $uri $uri/ /index.html;
|
||||
|
||||
# PWA支持
|
||||
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:3001;
|
||||
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 /uploads/ {
|
||||
alias /opt/company-finance-system/backend/uploads/;
|
||||
expires 1y;
|
||||
add_header Cache-Control "public";
|
||||
}
|
||||
}
|
||||
EOF
|
||||
|
||||
# 启用Nginx配置
|
||||
sudo ln -sf /etc/nginx/sites-available/company-finance /etc/nginx/sites-enabled/
|
||||
sudo rm -f /etc/nginx/sites-enabled/default
|
||||
sudo nginx -t
|
||||
sudo systemctl restart nginx
|
||||
|
||||
# 保存PM2配置
|
||||
echo "💾 保存PM2配置..."
|
||||
pm2 save
|
||||
pm2 startup systemd -u ubuntu --hp /home/ubuntu
|
||||
|
||||
echo ""
|
||||
echo "✅ 部署完成!"
|
||||
echo ""
|
||||
echo "📱 访问地址: http://43.161.248.209"
|
||||
echo "🔧 后端API: http://43.161.248.209/api"
|
||||
echo "📊 PM2管理: pm2 status"
|
||||
echo "📝 日志查看: pm2 logs"
|
||||
echo ""
|
||||
@@ -0,0 +1,63 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 服务器初始化脚本
|
||||
# 在腾讯云服务器上运行此脚本安装必要软件
|
||||
|
||||
set -e
|
||||
|
||||
echo "🚀 开始初始化服务器..."
|
||||
|
||||
# 更新系统
|
||||
echo "📦 更新系统..."
|
||||
sudo apt update && sudo apt upgrade -y
|
||||
|
||||
# 安装Node.js 18.x
|
||||
echo "📦 安装Node.js..."
|
||||
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
|
||||
sudo apt install -y nodejs
|
||||
|
||||
# 验证Node.js安装
|
||||
node -v
|
||||
npm -v
|
||||
|
||||
# 安装PM2
|
||||
echo "📦 安装PM2..."
|
||||
sudo npm install -g pm2
|
||||
|
||||
# 安装Nginx
|
||||
echo "📦 安装Nginx..."
|
||||
sudo apt install -y nginx
|
||||
|
||||
# 安装Git
|
||||
echo "📦 安装Git..."
|
||||
sudo apt install -y git
|
||||
|
||||
# 配置防火墙
|
||||
echo "🔥 配置防火墙..."
|
||||
sudo ufw allow 22/tcp
|
||||
sudo ufw allow 80/tcp
|
||||
sudo ufw allow 443/tcp
|
||||
sudo ufw allow 3001/tcp
|
||||
sudo ufw --force enable
|
||||
|
||||
# 创建应用目录
|
||||
echo "📁 创建应用目录..."
|
||||
sudo mkdir -p /opt/company-finance-system
|
||||
sudo mkdir -p /opt/backups
|
||||
sudo mkdir -p /var/log/company-finance
|
||||
sudo chown -R ubuntu:ubuntu /opt/company-finance-system
|
||||
sudo chown -R ubuntu:ubuntu /opt/backups
|
||||
|
||||
# 配置Nginx
|
||||
echo "⚙️ 配置Nginx..."
|
||||
sudo systemctl enable nginx
|
||||
|
||||
# 设置时区
|
||||
echo "🕐 设置时区..."
|
||||
sudo timedatectl set-timezone Asia/Shanghai
|
||||
|
||||
echo ""
|
||||
echo "✅ 服务器初始化完成!"
|
||||
echo ""
|
||||
echo "下一步:运行 deploy.sh 部署应用"
|
||||
echo ""
|
||||
Reference in New Issue
Block a user