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

相关推荐
FGGIT24 分钟前
使用Docker快速本地部署RSSHub结合内网穿透访问RSS订阅源
运维·docker·容器
x66ccff25 分钟前
【linux】轻松掌握文件管理:安装Ranger并设置Micro为默认编辑器
linux·运维·编辑器
2401_8260976234 分钟前
linux-vim的使用
linux·运维·vim
li37149089038 分钟前
生产k8s 应用容器内存溢出OOMKilled问题处理
docker·容器·kubernetes
-纸短情长1 小时前
负载均衡--会话保持失败原因及解决方案(五)
运维·负载均衡
乱蜂朝王1 小时前
Ubuntu篇——Ubuntu20.04备份成ISO镜像文件并安装到其他电脑上(完整步骤)
linux·运维·ubuntu
心之所想,行之将至1 小时前
gitlab集成CI/CD,shell方式部署
运维·ci/cd·gitlab
wumingxiaoyao2 小时前
AWS 容器注册表服务 ECR
docker·云计算·aws·ecr·容器注册服务
莫得等待2 小时前
k8s的一些命令
docker·容器·kubernetes
程序那点事儿3 小时前
CentOS 7 中安装 docker 环境
运维·docker·容器