最近购买了一个阿里云轻量应用服务器 部署一个node项目

  1. 独立购买一个阿里云域名 (自己完成备案)
  2. 独立购买一个阿里云服务器(我选的是最便宜的阿里云轻量应用服务器:系统 centos7 应用环境:node.js)
  3. 在阿里云官网>控制台> 阿里云轻量应用服务器>详情:找到公网ip后面修改密码 (ssh 登录用)
  4. 在左边菜单里找到安全>防火墙进入后点击添加规则 再在弹出框里 端口范围直接写上 端口号(例如:8080,2001,3000,)确定添加几个自己定。
  5. 在个人电脑上 自行使用ssh 登录公网ip :ssh root@ip.ip.ip.ip > 输入密码。进行登陆。
  6. 使用nvm 更新node版本 https://www.jianshu.com/p/534522e3a83b,
  7. npm install n -g 用来管理npm版本
  8. npm install pm2 -g 用来管理本地服务
  9. 如下测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
新建项目文件example.js。

cd ~
touch example.js
使用vim编辑器打开项目文件example.js。

yum install vim
vim example.js
输入"i",进入编辑模式,将以下项目文件内容粘贴到文件中。使用"Esc"按钮,退出编辑模式,输入":wq",回车,保存文件内容并退出(这里的0.0.0.0相当于windows的127.0.0.1)

const http = require('http');
const hostname = '0.0.0.0';
const port = 2000; //这里填写端口号与防火墙允许的端口号范围
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}/`);
});
运行项目

node ~/example.js 或者 pm2 start ~/example.js

//也可以用下面的代码后台运行
node ~/example.js &//后台运行
netstat -tpln//查看端口运行情况
在浏览器打开http://IP:80
如果看到Hello World,恭喜部署成功
  1. nginx

    修改配置文件 vim /usr/local/nginx/conf/nginx.conf 
    
    location / {
                root   html;
                index  index.html index.htm;
                proxy_pass http://127.0.0.1:2000;
            }
    
    然后保存,这里代理本地2000端口
    在浏览器打开http://IP
    
  2. 域名解析到 IP 来 然后在浏览器打开http://域名

完成

如有侵权行为,请点击这里联系我删除

如发现疑问或者错误点击反馈