阿里CentOS云服务器-部署node环境

xshell连接阿里云服务器(CentOS)

  • 公网ip等已输入(默认端口22)
  • 用户名 root,连接时出现 ssh服务器拒绝了密码,请再试一次
  • 在阿里云控制台, 重置密码后,重启服务器,就可以愉快
    在这里插入图片描述
    在这里插入图片描述

安装node环境

默认安装到 bin目录下。

部署node项目

  • 首次测试
    新建 apptest.js (这里是直接在home目录下,新建的测试例子)
const http = require('http');
const hostname = '你的私有ip'; // 我被这里坑了,默认填写了 127.0.0.1 ... o(╥﹏╥)o 
const port = 3000;
const server = http.createServer((req, res) => {
	res.statusCode = 200;
	res.setHeader('Content-Type', 'text/plain');
	res.end('Hello World\n');
});
server.listen(port, hostname, () => {
	console.log(`Server running at http://${hostname}:${port}/`);
});
  • xshell命令行启动node服务器

通过linux自带工具 nohup

nohup node apptest.js > test.log 2>&1 &

新建 shell 脚本 run.sh

// 使用vi命令新建run.sh文件
vi run.sh
// 点击 i 进入 insert 模式
// 输入脚本内容
// 点击esc退出编辑模式
// 输入 :wq 保存并退出
// 脚本内容
nohup node apptest.js > test.log 2>&1 &
// 启动服务器,进程被守护
sh run.sh
  • 停止node服务

查找进程id

ps -ef | grep node

在这里插入图片描述
强制stop进程

kill -9 进程id

npm install 很慢的问题解决

  • 查看下载源
npm config get registry

https://registry.npmjs.org/ // 此为国外地址

  • 更改下载源 淘宝镜像
npm config set registry http://registry.npm.taobao.org
发布了19 篇原创文章 · 获赞 19 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/an_xinyu/article/details/84033519