CentOS7搭建Node服务器,配置Nginx、forever

1、 前提准备

  • 阿里云服务器安装CentOS7系统,安装NodeJs环境;
  • 在ECS上装好系统后,去netsarang官网下载个人免费版的XShell和XFtp,管理自己的服务器;

    XShell和XFtp需要用ECS提供的外网IP和密码配置好(分别采用ssh和sftp的方式)
    2、 安装NodeJs

  • 下载NodeJs(源码安装)
    一种方式是在NodeJs官网自己下载好Node源码包,然后通过XFtp上传到自己的服务器
    另一种方式是在XShell中采用wget
1
[root@*** ~]# wget https://npm.taobao.org/mirrors/node/v8.2.1/node-v8.2.1.tar.gz
  • 解压

    1
    [root@*** ~]# tar zxf node-v8.2.1.tar.gz
  • 编译安装

    1
    2
    3
    4
    [root@*** ~]# cd node-v8.2.1
    [root@*** node-v8.2.1]# ./configure --prefix=/usr/local/node
    [root@*** node-v8.2.1]# make
    [root@*** node-v8.2.1]# make install
  • 配置Node环境

    1
    [root@*** node-v8.2.1]# vi /etc/profile

    使用vim打开profile,按键盘i进入到vim编辑模式,在文本的最后插入以下内容:

    1
    2
    3
    export NODE_HOME=/usr/local/node
    export PATH=$NODE_HOME/bin:$PATH
    export NODE_PATH=$NODE_HOME/lib/node_modules:$PATH

    输入完成之后,按ESC退出编辑模式;
    然后再输入:wq,退出并保存我们刚刚编辑过得文件;
    输入以下命令让刚刚的变动生效:

    1
    [root@*** node-v8.2.1]# source /etc/profile
  • 测试安装是否生效

    1
    [root@*** node-v8.2.1]# node -v

    出现版本号v8.2.1,则说明安装成功;

  • 总结

    1
    2
    3
    ./configure
    // 需要进入到解压好的node文件夹操作
    --prefix=/usr/local/node

    此处是把Node安装到/usr/local/node目录下(一般安装的都是在/usr/local目录下);
    这个命令也可以不要,但可能会导致目录混乱;
    如果系统是CentOS低版本,安装NodeJs过程中可能会提示gcc-c++版本过低,需要升级;

3、 配置一个简单的Web项目

  • 写一个测试脚本
    1
    2
    [root@*** node-v8.2.1]# cd ..
    [root@*** ~]# vi test.js

进入vim后,按i进入编辑模式,写个简单的脚本:

1
2
3
4
5
6
7
8
9
10
11
const http = require('http');
const hostname = '0.0.0.0';
const port = 80;
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}`);
});

按Esc退出编辑,输入:wq退出并保存;

1
[root@*** ~]# node test.js

提示:Server running at http://0.0.0.0:80/则说明成功了;

  • 在外网测试访问
    在浏览器输入在即的公网IP,出现Hello World,则说明Web服务搭建成功了!
  • 总结
    若出现不能访问的情况,则需要看下防火墙;
    CentOs7默认防火墙是关闭的,需要在Ecs的安全组入网端配置一下(防止端口被屏蔽);

4、 安装Nginx

  • 安装依赖

    1
    [root@*** ~]# yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
  • 下载nginx源码包

    1
    [root@*** ~]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
  • 解压、配置、编译、安装nginx

    1
    2
    3
    4
    5
    [root@*** ~]# tar zxf nginx-1.12.1.tar.gz
    [root@*** ~]# cd nginx-1.12.1
    [root@*** nginx-1.12.1]# ./configure --prefix=/usr/local/nginx
    [root@*** nginx-1.12.1]# make
    [root@*** nginx-1.12.1]# make install
  • 配置nginx反向代理
    进入/usr/local/nginx/conf目录,打开文件nginx.conf(可以vim打开,也可以在编辑器打开)
    在http对象下,将模块配置一修改为模块配置二

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    // 配置一
    server{
    listen 80;
    server_name localhost;
    location / {
    root html;
    index index.html index.htm;
    }
    }
    // 配置二
    server{
    listen 80;
    server_name localhost;
    location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Nginx-Proxy true;
    proxy_set_header Connection "";
    proxy_pass http://127.0.0.1:3000; # 此处配置为node默认的3000端口
    }
    }
  • 启动nginx服务

    1
    2
    [root@*** ~]# cd /usr/local/nginx/sbin
    [root@*** sbin]# ./nginx

5、 安装forever
可以让进程在终端关闭之后,继续运行;

  • 安装

    1
    [root@*** ~]# npm install forever -g
  • 启动

    1
    [root@*** ~]# forever start /root/app/index.js
  • 关闭
    方式一:

    1
    [root@*** ~]# forever stop /root/app/index.js

    方式二:
    查看forever list - 守护进程列表

    1
    2
    3
    4
    [root@*** ~]# forever list
    info: Forever processes running
    data: uid command script forever pid id logfile uptime
    data:[0] UGhW /usr/local/node/bin/node /root/app/index.js 22288 22294 /root/.forever/UGhW.log 0:3:57:32.789

    根据uid 选择关闭;当前是0则stop 0即可;

    1
    [root@*** ~]# forever stop 0
  • 输出日志和错误

    1
    [root@*** ~]# forever start -l forever.log -o out.log -e err.log /root/app/index.js

6、 一些Linux基本操作

  • 删除文件夹

    1
    rm -rf /usr/local/node

    将会删除掉/usr/local/node目录及其包含的而所有文件、文件夹

  • 删除文件

    1
    rm -f /usr/local/node/ini.log

    将会删除掉/usr/local/node目录下的文件ini.log

  • 查看端口号

    1
    2
    netstat -tln
    netstat -tln | grep 80

    前者是查看端口使用情况,后者是只查看端口80的使用情况

  • 查看端口是哪个程序占用的?哪个进程?

    1
    2
    3
    lsof -i:80
    COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
    Ali 11836 root 18u IPv4 13553 0t0 TCP iZw:44452->106.11.68.13:http (CLOSE_WAIT)
  • 关闭进程 (根据PID)

    1
    2
    kill -9 进程ID
    kill -9 11836

7、 参考文档