目录
刚买了阿里云 ECS 服务器,有了设备,可以跑起服务来了
这篇总结我怎么搭建生产可用的 nestjs + redis + mysql 运行环境
准备工作
这里我们将配置放到 github 上,然后在服务器上拉取,方便管理
下面是安装步骤
# 先安装 git
# 使用 yum 直接安装的 git 是老版本的
yum install git
git --version
git version 1.8.3.1
# 升级 git
# 先移除 git 老版本
yum remove git
yum remove git-*
# 添加源
yum install https://packages.endpointdev.com/rhel/7/os/x86_64/endpoint-repo.x86_64.rpm
# 安装新版本 git
yum install git
git -v
git version 2.41.0
配置 ssh key 从 github/gitee 拉取配置
这里可以参考 help.gitee.com/repository/…
# 使用 rsa 或 ed25519 都可以,推荐后者
cd ~/.ssh
ssh-keygen -t ed25519 -C "aliyun-gitee"
# 中间通过三次 回车键 确定即可
# 查看生成的 SSH 公钥
# 复制生成后的 ssh key,通过仓库主页 **「管理」->「部署公钥管理」->「添加部署公钥」** ,将生成的公钥添加到仓库中。
cat id_ed25519.pub
# 通过 `ssh -T` 测试时,输出 Anonymous 即可
ssh -T git@gitee.com
-tkey 类型-C注释
以上为拉取 gitee 仓库示例,github 不区分个人密钥和部署公钥配置更简单。
如此即完成 git 项目拉取,配置及部署就方便很多了。
注意事项
添加了部署公钥,记得clone 仓库时,使用 git 协议,使用 https 协议不行
环境安装
我们前面已经安装了 nodejs 环境,但还需要 mysql 和 redis。
这里使用 docker-compose.yml 配置做安装
# docker-compose.yml
# docker-compose -p hello up -d
version: '3'
services:
mysql:
image: mysql:8
restart: always
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: hello
MYSQL_USER: hello
MYSQL_PASSWORD: 123456
volumes:
# 使用命名数据卷,如不存在,自动创建
- mysql:/var/lib/mysql
ports:
- '3306:3306'
redis:
image: redis:latest
restart: always
ports:
- '6379:6379'
volumes:
mysql:
postgres:
上面的配置直接使用了数据卷做数据持久化
启动 docker-compose
docker-compose -p hello up -d
# 提示命令不存在
-bash: docker-compose: command not found
# 安装 docker-compose
yum -y install epel-release
yum install python3-pip
pip3 install --upgrade pip
copypip3 install docker-compose
# 安装完成
docker-compose --version
# 再次启动,就启动起来了
docker-compose -p hello up -d
# 查看容器
docker containers list
cfa657c1e03 mysql:8 "docker-entrypoint.s…" 26 seconds ago Up 25 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp hello_mysql_1
868b34780df6 redis:latest "docker-entrypoint.s…" 26 seconds ago Up 25 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp hello_redis_1
服务已经跑起来了,nestjs 程序已经可以直接访问了。