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

相关推荐
橘颂TA1 小时前
【Linux】 层层递进,抽丝剥茧:调度队列、命令行参数、环境变量
linux·运维·服务器·c/c++
七夜zippoe1 小时前
压缩与缓存调优实战指南:从0到1根治性能瓶颈(四)
运维·docker·容器
X_szxj2 小时前
Volatility2在kali安装
linux·运维·服务器
Q16849645152 小时前
红帽Linux复习-Vim
linux·运维·vim
少妇的美梦2 小时前
Kubernetes(K8s)YAML 配置文件编写教程
运维·后端
加油_Yeah3 小时前
pycharm 远程连接服务器&添加github copilot
运维·服务器·ide·pycharm·copilot
java_logo3 小时前
Docker 部署 MinerU 教程:打造你的本地 PDF 智能处理中心
linux·运维·人工智能·docker·ai·容器·aigc
LCG元3 小时前
Docker容器化实战:将你的SpringBoot应用一键打包部署(三)-配置告警和自动扩缩容
后端·docker
Dovis(誓平步青云)3 小时前
《剖析 Linux 文件系统:架构、原理与实战操作指南》
linux·运维·服务器
千百元3 小时前
centos查线程数
linux·运维·centos