在centos7上进行PM2管理器的部署

因为node启动后,需要在后台运行,这样当我们退出ssh远程服务器对话框的时候,就不会终止node运行的端口,那么怎么来实现这种运行呢?

有一个管理这种服务的工具名为 PM2 (NPM下载连接是:https://www.npmjs.com/package/pm2)
NPM上面对PM2的介绍是:
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.[PM2是具有内置负载平衡器的Node.js应用程序的生产流程管理器。它允许您永远保持应用程序的活动性,在不停机的情况下重新加载它们,并方便执行常见的系统管理任务。]

开始行动:

1. 配置淘宝镜像(方便我们用超音速的速度去下载这个小小的包)

	npm config set registry https://registry.npm.taobao.org

2. 开始执行下载命令

	 npm install -g pm2

在这里插入图片描述
3. 安装完成后,执行pm2命令我是在我的/usr/local/node/lib/node_modules/pm2/bin目录执行的在这里插入图片描述


设置为全局可以依靠软连接,这样即使你在根目录输入PM2命令也会执行的
ln -s /usr/local/node-v10.15.3-linux-x64/lib/node_modules/pm2/bin/pm2 /usr/local/bin/pm2

然后就会看见在 /usr/local/bin目录下看见pm2的软连接
在这里插入图片描述


4. 开始运行Node服务

我这里是运行一个在 /root/下的index.js文件
index.js代码:

const http = require('http');
const port = 3000;
const servername = '0.0.0.0';
const server = http.createServer((req,res)=>{
        res.statusCode=200;
        res.setHeader('Content-Type','text/plain');
        res.end('when you see this message in your screen on the browser that prove you already get success!\n');
});

server.listen(port,servername,()=>{
        console.log(`server already start at the :${port} port`);
});
./pm2 start node /root/index.js

运行结果如图所示:
在这里插入图片描述
然后你会在你的服务器地址3000端口看见你的服务运行了,Nice! 行动Over!
在这里插入图片描述

发布了9 篇原创文章 · 获赞 3 · 访问量 1965

猜你喜欢

转载自blog.csdn.net/qq_41136216/article/details/105479377