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: [email protected]

相关推荐
文牧之2 小时前
Oracle 的 SEC_CASE_SENSITIVE_LOGON 参数
运维·数据库·oracle
Jay_272 小时前
python项目如何创建docker环境
开发语言·python·docker
Antonio9152 小时前
【Linux】 Linux 进程控制
linux·运维·服务器
---wzy---3 小时前
docker生命周期
java·docker·容器
thinkMoreAndDoMore3 小时前
linux驱动开发(1)-内核模块
linux·运维·驱动开发
不想头秃a3 小时前
JavaEE初阶-网络编程
java·运维·服务器·网络
一ge科研小菜鸡3 小时前
云原生 DevOps 实践路线:构建敏捷、高效、可观测的交付体系
运维·云原生·devops
Chuncheng's blog3 小时前
CentOS 7 如何pip3安装pyaudio?
linux·运维·python·centos
西阳未落3 小时前
Linux(12)——基础IO(下)
linux·运维·服务器