Skip to content

部署 NEXT·SPRING主题

1.准备环境

服务器需要提前安装:

服务器要求:最低1h1c(建议2h2c)

  • 宝塔面板
  • Nginx
  • MySQL
  • Node.js 18 或更高版本
  • PM2

进入服务器 SSH 后安装:

安装 Node.js

bash
curl -fsSL https://deb.nodesource.com/setup_22.x | bash -
apt install -y nodejs

安装 PM2

bash
npm install -g pm2

2.创建网站和数据库

在宝塔里创建网站:

bash
网站目录:/www/wwwroot/your-domain.com
域名:your-domain.com

在宝塔里创建 MySQL 数据库,并记下:

bash
数据库地址
数据库端口
数据库名
数据库用户名
数据库密码

3.上传文件

前端文件(dist里面的所有内容):

把前端dist打包后的文件上传到网站根目录:

bash
/www/wwwroot/your-domain.com/

也就是让网站根目录里能看到:

text
index.html
assets/
webp/

后端文件(springa-admin文件夹):

然后把后端文件上传到:

text
/www/wwwroot/your-domain.com/springa-admin

后端目录结构大概是:

text
springa-admin/
  package.json
  ecosystem.config.cjs
  migrations/
  scripts/
  src/
  .env.example

不要上传 node_modules,服务器上会重新安装。

4.配置后端

进入后端目录:

bash
cd /www/wwwroot/your-domain.com/springa-admin

创建.env文件

bash
cp .env.example .env

在宝塔springa-admin目录里找到 .env 改成下面这样:

只需改2个地方

1.替换成你的机场后端地址
2.Nginx里的站点目录换成你的

env
PORT=8787

DB_HOST=数据库地址
DB_PORT=3306
DB_DATABASE=数据库名
DB_USER=数据库用户名
DB_PASSWORD=数据库密码
DB_TABLE_PREFIX=Spring_

UPSTREAM_BASE_URL=https://机场后端地址.com

ADMIN_USERNAME=spring
ADMIN_PASSWORD=spring1688
ADMIN_PATH=

CORS_ORIGINS=

NGINX_BLACKLIST_FILE=/www/wwwroot/你的站点目录/.nginx/theme-ip-blacklist.conf
NGINX_BLACKLIST_AUTO_RELOAD=1
NGINX_BIN=/www/server/nginx/sbin/nginx

说明:

  • DB_TABLE_PREFIX=Spring_ 会创建 Spring_settingsSpring_admin_accountsSpring_login_access_logs 等表。
  • UPSTREAM_BASE_URL 填原来的面板后端接口地址,例如 https://panel-api.example.com,不要填本站前端域名。
  • ADMIN_USERNAMEADMIN_PASSWORD 是主题后台首次初始化的管理员账号和密码。
  • ADMIN_PATH 留空会自动生成后台随机路径;如果要手动指定,填 8-128 位字母、数字、下划线或中划线。
  • NGINX_BLACKLIST_AUTO_RELOAD=1 表示后台拉黑 IP 后自动重载 Nginx 配置。

5.为管理后台安装依赖

进入管理后台路径

bash
cd /www/wwwroot/your-domain.com/spring-admin

安装依赖

bash
npm install

初始化数据库

bash
npm run migrate

生成nginx黑名单

bash
npm run nginx:blacklist:sync

看到下面内容就说明数据库初始化成功:

text
spring-admin migrations completed

npm run nginx:blacklist:sync 会生成 Nginx 黑名单文件。这个文件必须存在,否则后面 Nginx 配置里的 include 会检测失败。

6.启动后端服务

进入管理后台路径

bash
cd /www/wwwroot/your-domain.com/spring-admin

启动后台

bash
pm2 start ecosystem.config.cjs

保存当前进程列表

bash
pm2 save

查看后台登录地址和默认账号:

bash
pm2 logs nextspring --lines 80

日志里会出现类似:

text
Admin URL path: /#/admin/admin-xxxxxxxx
Username: spring
Password: spring1688

完整后台访问路径:https://你的站点/#/admin/admin-xxxxxx
访问后台(先不要进后台,完成第7步的Nginx步骤再进入):

text
https://your-domain.com/#/admin/admin-xxxxxxxx

首次登录后必须修改帐号和密码。
后台默认帐号:spring
后台默认密码:spring1688

登录后台后,可以进入“面板接口”页面修改你的面板类型。

7、配置 Nginx

网站没开cf的用这个配置:

替换下方配置里的站点目录为你的站点目录

在宝塔网站的 Nginx 配置里,大概在第9行,找到 root 在后面加入下面内容。

nginx
    include /www/wwwroot/你的站点目录/.nginx/theme-ip-blacklist.conf;

    location ^~ /.nginx/ {
    return 404;
    }

    location ^~ /api/ {
    proxy_pass http://127.0.0.1:1688;
    proxy_http_version 1.1;
    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;
    }

    # 如果你修改了订阅路径,请替换下方的/s/为你的实际订阅路径
    location ^~ /s/ {
    proxy_pass http://127.0.0.1:1688;
    proxy_http_version 1.1;
    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;
    }

如果你的网站开启了 Cloudflare CDN,请用这个配置。

替换下方配置里的站点目录为你的站点目录

在宝塔网站的 Nginx 配置里,大概在第9行,找到 root 在后面加入下面内容。

nginx
    real_ip_header CF-Connecting-IP;
    real_ip_recursive on;

    set_real_ip_from 173.245.48.0/20;
    set_real_ip_from 103.21.244.0/22;
    set_real_ip_from 103.22.200.0/22;
    set_real_ip_from 103.31.4.0/22;
    set_real_ip_from 141.101.64.0/18;
    set_real_ip_from 108.162.192.0/18;
    set_real_ip_from 190.93.240.0/20;
    set_real_ip_from 188.114.96.0/20;
    set_real_ip_from 197.234.240.0/22;
    set_real_ip_from 198.41.128.0/17;
    set_real_ip_from 162.158.0.0/15;
    set_real_ip_from 104.16.0.0/13;
    set_real_ip_from 104.24.0.0/14;
    set_real_ip_from 172.64.0.0/13;
    set_real_ip_from 131.0.72.0/22;
    set_real_ip_from 2400:cb00::/32;
    set_real_ip_from 2606:4700::/32;
    set_real_ip_from 2803:f800::/32;
    set_real_ip_from 2405:b500::/32;
    set_real_ip_from 2405:8100::/32;
    set_real_ip_from 2a06:98c0::/29;
    set_real_ip_from 2c0f:f248::/32;

    include /www/wwwroot/你的站点目录/.nginx/theme-ip-blacklist.conf;

    location ^~ /.nginx/ {
    return 404;
    }

    location ^~ /api/ {
    proxy_pass http://127.0.0.1:1688;
    proxy_http_version 1.1;
    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;
    }

    # 如果你修改了订阅路径,请替换下方的/s/为你的实际订阅路径
    location ^~ /s/ {
    proxy_pass http://127.0.0.1:1688;
    proxy_http_version 1.1;
    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;
    }

8、重载 Nginx

宝塔环境用这个命令:

bash
/www/server/nginx/sbin/nginx -t && /www/server/nginx/sbin/nginx -s reload

如果提示 syntax is oktest is successful,说明配置没有问题。

9、访问后台查看是否部署成功

后台管理地址:

text
https://your-domain.com/#/admin/admin-xxxxxxxx

10、以后的更新版本

  • 1.打包前端dist文件(不懂打包就看第一篇-打包构建教程,从第二步开始)
  • 2.上传前端dist文件
  • 3.上传后端的spring-admin文件夹(不要上传node_modules文件夹 和 .env文件)

然后按这个顺序执行(一行一行执行):

bash
cd /www/wwwroot/your-domain.com/spring-admin
npm install
npm run migrate
npm run nginx:blacklist:sync
pm2 restart nextspring --update-env
/www/server/nginx/sbin/nginx -t && /www/server/nginx/sbin/nginx -s reload

11、常见问题

1. 忘记后台路径或密码

进入后端目录执行:

bash
cd /www/wwwroot/your-domain.com/spring-admin
npm run admin:reset
pm2 restart nextspring --update-env
pm2 logs nextspring --lines 80

日志里会重新输出后台路径和默认账号密码。

2. 如何卸载

进入后端目录执行:

bash
cd /www/wwwroot/your-domain.com/spring-admin
pm2 delete nextspring
pm2 save --force
rm -rf /www/wwwroot/你的站点目录

闭眼入,一眼定情!