NestJS 部署与维护 | 您所在的位置:网站首页 › nestjs社区 › NestJS 部署与维护 |
我们可以把nestjs的工程传到服务器上,然后直接进行npm操作。 也可以在本地打包出nestjs的包,将dist目录传到服务器上。 在这里为了方便演示,我们描述的是将工程传到服务器后的操作。 传工程文件,可以用github/gitee当作中间桥梁,也可以用vscode的插件传输,或者使用scp命令。 同理,dist目录也可以传到github/gitee上,服务器直接使用编译好的文件。 直接启动启动前,进入工程目录,先安装一次。 $ npm install用npm run命令启动服务 # 开发模式 $ npm run start # 观察模式 $ npm run start:dev在服务器上,运行正式环境(生产环境 production) # 启动生产环境前 先start $ npm run start # 生产环境 $ npm run start:prod使用npm run运行的程序,可以ctrl + c停止。 run起来后,编译后的文件在dist目录里。 使用pm2管理pm2是常用的node进程管理工具,它可以提供node.js应用管理,如自动重载、性能监控、负载均衡等。 在服务器上,我们使用npm安装这个工具 npm install pm2安装完毕后,查看一下版本 $ pm2 -v 4.4.1 pm2启动服务我们知道,nestjs编译后的文件在dist目录里,入口文件是dist/main.js 用pm2启动服务之前,先把该装的库用npm装好。运行npm run start:prod,然后ctrl + c停止。 接下来用pm2启动 pm2 start dist/main.js --name="这里是自定义的名字" 查看服务使用命令pm2 list查看服务 例如在某个服务器上 $ pm2 list ┌─────┬───────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐ │ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │ ├─────┼───────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤ │ 3 │ rustfisher-debug │ default │ 1.0.0 │ fork │ 12260 │ 2M │ 0 │ online │ 0% │ 39.7mb │ ubuntu │ disabled │ │ 2 │ rustfisher-release │ default │ 1.0.0 │ fork │ 12090 │ 2M │ 46 │ online │ 0% │ 44.6mb │ ubuntu │ disabled │ │ 0 │ index │ default │ 1.0.0 │ fork │ 0 │ 0 │ 68 │ stopped │ 0% │ 0b │ ubuntu │ disabled │ │ 1 │ index │ default │ 1.0.0 │ fork │ 0 │ 0 │ 15 │ stopped │ 0% │ 0b │ ubuntu │ disabled │ │ 4 │ an.rustfisher │ default │ 0.0.1 │ fork │ 32252 │ 74m │ 0 │ online │ 0% │ 51.5mb │ ubuntu │ disabled │ └─────┴───────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘ 停止服务想要停止某个服务,使用pm2 stop + 服务名(name)或者id 例如 pm2 stop rustfisher-debug执行完毕后,会自动运行一次pm2 list 重启服务用pm2 restart + 服务名(name)或者id pm2 restart rustfisher-debug或者 pm2 restart 3 日志管理默认情况下,pm2的日志存放在~/.pm2/log里。每个启动的服务有对应的log文件。 例如,在某个服务器上 $ ls .pm2/logs/ app-error.log dr-debug-error.log dr-release-error.log index-error.log www-error.log app-out.log dr-debug-out.log dr-release-out.log index-out.log www-out.log nohup后台启动我们也可用nohup后台启动。 进入工程目录,运行 nohup npm run start:prod >> nohup.log &查看进程 $ ps -aux | grep node ubuntu 11826 0.0 1.2 674064 10988 ? Ssl Mar16 0:00 /usr/local/bin/node /usr/local/lib/node_modules/npm/node_modules/update-notifier/check.js {"pkg":{"name":"npm","version":"6.14.6"}} ubuntu 16478 0.0 0.0 4512 788 pts/3 S 11:49 0:00 sh -c node dist/main ubuntu 16479 0.4 5.1 578840 45228 pts/3 Sl 11:49 0:00 node dist/main ubuntu 16959 0.0 0.1 13232 928 pts/3 S+ 11:52 0:00 grep --color=auto node可以看到有2个相关的进程16478和16479 查看端口占用情况。假设我们工程监听的是8090端口。 $ netstat -anp | grep 8090 (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) tcp 0 0 127.0.0.1:52788 127.0.0.1:8090 TIME_WAIT - tcp6 0 0 :::8090 :::* LISTEN 16479/node结合一看,进程16479是我们后台运行的服务没错了。 停止后台进程 sudo kill 16479kill然后再查询进程和端口占用情况,就没有相关的进程了。 总结可以用不同的方式后台启动服务。 启动生产环境之前,要先同步代码并编译。 |
CopyRight 2018-2019 实验室设备网 版权所有 |