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

相关推荐
sj11637394034 分钟前
docker-compose方式启动Kafka Sasl加密认证(无zk)
docker·kafka
likfishdn20 分钟前
Linux的文件与目录管理
linux·运维·服务器
chian-ocean40 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=42 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
Dontla1 小时前
Docker run --add-host参数解析(在容器启动时向/etc/hosts文件中添加自定义的主机名与IP映射)(适用于临时调试或测试)
tcp/ip·docker·容器
西西小飞龙1 小时前
Docker和K8S中pod、services、container的介绍和关系
docker·容器·kubernetes
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
kngines1 小时前
【实战 ES】实战 Elasticsearch:快速上手与深度实践-1.3.1单节点安装(Docker与手动部署)
大数据·elasticsearch·docker
北京耐用通信2 小时前
航空装配自动化神器Ethercat转profient网关搭配机器人精准控制
运维·机器人·自动化
吃汤圆的抹香鲸2 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net