linux 下安装node 并使用nginx做域名绑定

时间:2022-05-09 05:19:49

#1 ,home目录下 下载nodejs安装包,解压 并修改文件夹名称

wget https://nodejs.org/dist/v8.11.4/node-v8.11.4-linux-x64.tar.xz

tar xvf node-v8.11.4-linux-x64.tar.xz

mv node-v8.11.4-linux-x64 node-v8

# 2,做软连接配置node 命令 和 npm

[root@node]# ln -s /home/wwwroot/node/node-v8/bin/node /usr/local/bin/
[root@ node]# ln -s /home/wwwroot/node/node-v8/bin/npm /usr/local/bin/
[root@ node]# node -v
v8.11.4

#3,更改node模块文件夹的权限

[root@ node]# chgrp www node_modules/
[root@ node]# chown www node_modules/

#4,配置nginx

upstream nodejs{
  server 127.0.0.1:8001;
  keepalive 64;
}

server {
  listen 80;
  server_name node.domain.com;

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://nodejs;
}

}

#5,重新加载nginx 配置文件

service nginx reload

#6,安装 forever 保证服务崩溃重启 [forever restartall  forever stopall ]

npm install  forever -g

[root@ node]# ln -sf  /home/wwwroot/node/node-v8/bin/forever  /usr/local/bin/forever

[root@ node]# forever stopall
info: No forever processes running
[root@ node]# forever list
info: No forever processes running
[root@ node]# forever start index.js
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: index.js
[root@ node]# forever list
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [0] T0JU /home/wwwroot/node/node-v8/bin/node index.js 12606 12612 /root/.forever/T0JU.log STOPPED