feat(deployment): 优化宝塔 PM2 standalone 部署流程并添加一键部署脚本
Some checks failed
lotteryadmin CI / build (push) Has been cancelled

This commit is contained in:
2026-07-07 15:32:19 +08:00
parent 99e644caa1
commit 70250696ea
4 changed files with 30 additions and 6 deletions

View File

@@ -2,8 +2,8 @@
* 管理端lotteryadmin宝塔 PM2 配置
*
* 部署步骤(在服务器项目目录执行):
* 1. 复制 .env.example .env按线上域名填写
* 2. npm ci && npm run build
* 1. 本地 npm run build打包 .next/standalone + .next/static + public 上传服务器
* 2. 服务器:复制 .env.example 为 .env按线上域名填写
* 3. 修改下方 cwd 为服务器上的实际路径(默认即本文件所在目录)
* 4. pm2 start ecosystem.config.cjs
* 5. pm2 save && pm2 startup
@@ -22,8 +22,7 @@ module.exports = {
{
name: "lotteryadmin",
cwd: APP_CWD,
script: "node_modules/next/dist/bin/next",
args: "start -p 3801 -H 127.0.0.1",
script: ".next/standalone/server.js",
interpreter: "node",
exec_mode: "fork",
instances: 1,
@@ -37,6 +36,7 @@ module.exports = {
env: {
NODE_ENV: "production",
HOSTNAME: "0.0.0.0",
PORT: "3801",
},

View File

@@ -5,9 +5,12 @@ import { parseAllowedDevOrigins } from "./src/lib/next-dev-origins";
const allowedDevOrigins = parseAllowedDevOrigins(process.env.ALLOWED_DEV_ORIGINS);
const nextConfig: NextConfig = {
/* config options here */
output: "standalone",
...(allowedDevOrigins.length > 0 ? { allowedDevOrigins } : {}),
reactCompiler: true,
images: {
unoptimized: true,
},
async redirects() {
return [
{

View File

@@ -4,7 +4,7 @@
"private": true,
"scripts": {
"dev": "NEXT_DISABLE_MEM_OVERRIDE=1 NODE_OPTIONS='--max-old-space-size=3072' next dev --port 3801",
"build": "next build",
"build": "next build && cp -r .next/static .next/standalone/.next/static && cp -r public .next/standalone/public && echo '✅ standalone 静态资源已复制完毕'",
"start": "next start --port 3801",
"lint": "eslint"
},

21
scripts/deploy.sh Normal file
View File

@@ -0,0 +1,21 @@
#!/bin/bash
# lotteryadmin 一键部署脚本
# 用法: bash scripts/deploy.sh
set -e
SERVER="root@47.242.205.221"
REMOTE_DIR="/www/wwwroot/lotteryAdmin"
echo "🔨 构建中..."
npm run build
echo "📦 上传到服务器..."
ssh ${SERVER} "mkdir -p ${REMOTE_DIR}/.next/standalone ${REMOTE_DIR}/logs"
rsync -avz --delete .next/standalone/ ${SERVER}:${REMOTE_DIR}/.next/standalone/
rsync -avz ecosystem.config.cjs ${SERVER}:${REMOTE_DIR}/ecosystem.config.cjs
echo "🚀 服务器重启 PM2..."
ssh ${SERVER} "source /etc/profile && source ~/.bashrc && mkdir -p ${REMOTE_DIR}/logs && cd ${REMOTE_DIR} && (pm2 restart lotteryadmin 2>/dev/null || pm2 start ecosystem.config.cjs) && pm2 save"
echo "✅ lotteryAdmin 部署完成!"