Nestjs构建Certeasy证书自动化平台 - 系统部署

摘要

系统开发完成,最后的任务就是系统的部署。本系统部署简单,可dockerfile 进行构建运行,易可编译之后使用 pm2 ecosystem 运行

环境

  • NodeJS18+
  • MySQL57+
  • Ubuntu24/Centos79
  • Docker

Docker 部署

1.构建镜像
erlang 复制代码
docker build -t certeasy-nest .

构建完成查看本地镜像信息

ruby 复制代码
root@certeasy:/opt/www/certeasy# docker images
REPOSITORY      TAG                IMAGE ID       CREATED        SIZE
certeasy_nest   latest             46d67a42839c   1 hours ago   925MB
2. 镜像运行

使用 docker run 运行

bash 复制代码
docker run --name certeasy_nest_prod \
  -d \
  -p 9527:9527 \
  -v $(pwd)/.certeasy:/app/.certeasy \
  -v $(pwd)/billing:/app/billing \
  -v $(pwd)/google:/app/google \
  -v $(pwd)/template:/app/template \
  -v $(pwd)/.env.production:/app/.env.production \
  -e NODE_ENV=production \
  certeasy_nest:latest

查看运行结果

ruby 复制代码
root@iZj6c97154mi20z7gsnux8Z:/opt/www/certeasy# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED        STATUS        PORTS                                         NAMES
2dbb5855a17e   certeasy_nest:latest   "docker-entrypoint.s..."   43 hours ago   Up 43 hours   0.0.0.0:9530->9527/tcp, [::]:9530->9527/tcp   certeasy_nest_prod
ruby 复制代码
root@certeasy:/opt/www/certeasy# curl 127.0.0.1:9530 && docker logs -f certeasy_nest_prod
2024-09-27 10:08:11: [Nest] 25  - 09/27/2024, 10:08:11 AM     LOG [LoggingInterceptor] request <- [172.17.0.1] GET /
2024-09-27 10:08:11: [Nest] 25  - 09/27/2024, 10:08:11 AM   DEBUG [LoggingInterceptor] <- query: {}
2024-09-27 10:08:11: [Nest] 25  - 09/27/2024, 10:08:11 AM   DEBUG [LoggingInterceptor] <- params: {}
2024-09-27 10:08:11: [Nest] 25  - 09/27/2024, 10:08:11 AM   DEBUG [LoggingInterceptor] <- body: {}
2024-09-27 10:08:11: [Nest] 25  - 09/27/2024, 10:08:11 AM     LOG [LoggingInterceptor] response -> [172.17.0.1] 200 GET / 9ms
2024-09-27 10:08:11: [Nest] 25  - 09/27/2024, 10:08:11 AM   DEBUG [LoggingInterceptor] -> body: {"code":0,"data":{"time":1727402891647,"ip":"172.17.0.1","headers":{"host":"127.0.0.1:9530","user-agent":"curl/7.81.0","accept":"*/*"}},"message":"ok"}

常规部署

1.编译

使用 npm or yarn 进行程序构建,本系统打包使用了webpack,打包结果是单 main 文件

复制代码
yarn build
2. 构建运行目录

在您的服务器指定目录下构建以下文件夹并上传相关文件内容

lua 复制代码
.
├── billing
│   └── config.json
├── bin
│   └── main.js
├── ecosystem.config.json
├── google
│   └── account-key.json
├── logs
│   ├── certeasy-api-err.log
│   └── certeasy-api-out.log
├── node_modules
│   └── ......
├── package.json
├── template
│   ├── notification.tmpl.ejs
│   └── validation.tmpl.ejs
└── yarn.lock
3.上传构建文件 main.jsbin 文件夹
4.运行

运行的前提是你的服务器已经安装好 nodepm2 运行工具

bash 复制代码
# 指定环境变量 production 运行
pm2 start ecosystem.config.json --env production

以上代码指定了 production 环境变量,系统会读取目录下 .env.production 文件相关配置信息

5.部署结果
ruby 复制代码
root@certeasy:/opt/www/certeasy# pm2 ls
┌────┬─────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name            │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├────┼─────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0  │ certeasy-api    │ default     │ 1.0.0   │ cluster │ 496657   │ 46h    │ 0    │ online    │ 0%       │ 162.7mb  │ root     │ enabled  │
│ 1  │ certeasy-api    │ default     │ 1.0.0   │ cluster │ 496664   │ 46h    │ 0    │ online    │ 0%       │ 164.0mb  │ root     │ enabled  │
└────┴─────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
ruby 复制代码
root@certeasy:/opt/www/certeasy# curl 127.0.0.1:9527 && pm2 logs --lines 10
/opt/www/certeasy/logs/certeasy-api-out.log last 10 lines:
0|certeasy | 2024-09-27 10:00:08: [Nest] 496664  - 09/27/2024, 10:00:08 AM   DEBUG [LoggingInterceptor] <- params: {}
0|certeasy | 2024-09-27 10:00:08: [Nest] 496664  - 09/27/2024, 10:00:08 AM   DEBUG [LoggingInterceptor] <- body: {}
0|certeasy | 2024-09-27 10:00:08: [Nest] 496664  - 09/27/2024, 10:00:08 AM     LOG [LoggingInterceptor] response -> [127.0.0.1] 200 GET / 3ms
0|certeasy | 2024-09-27 10:00:08: [Nest] 496664  - 09/27/2024, 10:00:08 AM   DEBUG [LoggingInterceptor] -> body: {"code":0,"data":{"time":1727402408514,"ip":"127.0.0.1","headers":{"host":"127.0.0.1:9527","user-agent":"curl/7.81.0","accept":"*/*"}},"message":"ok"}

注意

  • 打包运行前,请确认你已安装数据库 MySQLRedis
  • 请确认 env 配置文件的内容正确

系列文章

开源

联系

wechat: zuxcloud

Email: zuxing.xu@lettered.cn

相关推荐
Yyyy4822 小时前
Ubuntu安装Jenkis
linux·运维·ubuntu
克莱斯勒ya3 小时前
服务器硬件配置
运维·服务器
GTgiantech3 小时前
精准成本控制与单向通信优化:1X9、SFP单收/单发光模块专业解析
运维·网络
tzhou644523 小时前
Docker容器技术指南
运维·docker·容器
zhuzewennamoamtf3 小时前
Linux SPI设备驱动
android·linux·运维
春日见3 小时前
在虚拟机上面无法正启动机械臂的控制launch文件
linux·运维·服务器·人工智能·驱动开发·ubuntu
松涛和鸣4 小时前
Linux Makefile : From Basic Syntax to Multi-File Project Compilation
linux·运维·服务器·前端·windows·哈希算法
LF3_4 小时前
配置ssh免密登录
运维·ssh
再睡一夏就好5 小时前
深入Linux线程:从轻量级进程到双TCB架构
linux·运维·服务器·c++·学习·架构·线程
小小药5 小时前
09-vmware配置虚机连接互联网-nat模式
linux·运维·centos