使用Nginx和pm2经过ip+端口访问你的服务器

1.前言

昨天看到网易云音乐node.js版API,可是由于对node.js的认识只在一些语句上面,苦于不知咋用.结果发现原来就直接node app.js就能在本地服务器localhost中运行.因而就想把他挂到个人vps上面.弄了一下午弄好了.把此次的踩坑经验发上来php

个人vps是centos 6版本的html

​ 具体步骤:node

1. node.js npm 环境配置
	2. git配置(个人vps在作gitpage的时候已经配置了,因此就不写了)
	3. pm2
	4. Nginx
复制代码

2. 配置过程

1. node.js配置

参考linux

因为linux上安装文件是真的有不少方式,没具体学过linux的我真的晕晕了,因为我对node的版本没有要求,就直接安装了具体版本号.nginx

推荐如下操做在 /opt 目录下进行git

下载压缩包

wget http://developer.jpanj.com/node-v10.15.3-linux-x64.tar.xz
复制代码

解压为 tar 包

xz -d node-v10.15.3-linux-x64.tar.xz
复制代码

若是xz命令不存在的话,能够查看博客或者根据一下步骤一步步敲github

wget http://tukaani.org/xz/xz-5.2.2.tar.gz`
复制代码
`tar -zxf xz-5.2.2.tar.gz`
复制代码
./configure  
make && make install
复制代码

解压

tar -xvf node-v10.15.3-linux-x64.tar
复制代码

当前目录下软链一个 node 目录出来

这样作的好处是,将来升级版本很是方便,只须要更新这个软链就行npm

ln -s ./node-v10.15.3-linux-x64 ./node
复制代码

经过软连接,将可执行程序放入系统环境变量的路径中

  • 查看当前系统中都有哪些环境变量路径
# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
复制代码

能够看到个人列表中有:vim

  • /usr/local/bin
  • /usr/bin

你们约定成俗逻辑是:windows

  • /usr/bin下面的都是系统预装的可执行程序,会随着系统升级而改变。
  • /usr/local/bin 目录是给用户放置本身的可执行程序的地方

因此我推荐将软链放在 /usr/local/bin 目录下:

ln -s /opt/node/bin/node /usr/local/bin/node
ln -s /opt/node/bin/npm /usr/local/bin/npm
复制代码

可是在这一步我遇到了-bash: cd: src: Too many levels of symbolic links的问题,后续解决方案是我cd ~以后 ln -s /opt/node/bin/node /usr/local/bin/node 来完成的。

若是遇到file exists的问题,就到你软链到的那个地址的命令文件删掉

检查是否安装成功

[root@dc8 ~]# node -v
v10.15.3
[root@dc8 ~]# npm -v
6.4.1
复制代码

2. pm2

参考

pm2的做用是可以将你的node后台项目持久得挂载在后台而且可以自动地帮你负载均衡之类的,适合我这种傻瓜嘻嘻

安装pm2

npm i -g pm2

经过软连接,将可执行程序放入系统环境变量的路径中

由于不像windows,npm下载以后不可以直接在全局环境之中使用

因此也要像以前同样

ln -s /opt/node/bin/pm2 /usr/local/bin/pm2
复制代码

接下来就能将node项目一直挂载在后台了

使用

pm2 start app.js //启动pm2项目
pm2 stop all //中止全部项目

PORT=4000 pm2 start app.js //使用的这个网易云后台程序,在项目目录下启动,更多命令内容建议看参考
复制代码

3. Nginx

参考1

参考2

1.第一步,在/etc/yum.repos.d/目录下建立一个源配置文件nginx.repo:

cd /etc/yum.repos.d/
复制代码
vim nginx.repo
复制代码

填写以下内容:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
复制代码

保存,则会产生一个/etc/yum.repos.d/nginx.repo文件。

下面直接执行以下指令便可自动安装好Nginx:

yum install nginx -y
复制代码

安装完成,下面直接就能够启动Nginx了:

/etc/init.d/nginx start
复制代码

如今Nginx已经启动了,直接访问服务器就能看到Nginx欢迎页面了的。

若是还没法访问,则需配置一下Linux防火墙。

iptables -I INPUT 5 -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
复制代码

这里注意 以后Nginx要监听哪一个端口就必须开放哪一个端口,我在这里踩了一下坑 如今咱们释放的是默认的80端口

service iptables save
复制代码
service iptables restart
复制代码

Nginx的命令以及配置文件位置:

/etc/init.d/nginx start # 启动Nginx服务
 
/etc/init.d/nginx stop # 中止Nginx服务

/etc/nginx/nginx.reload # 重启Nginx服务
 
/etc/nginx/nginx.conf # Nginx配置文件位置


chkconfig nginx on    #设为开机启动
复制代码

至此,Nginx已经所有配置安装完成。

2.一台主机上适应多个服务

在你的nginx经过代理的方式转发请求:进入nginx文件夹下的conf.d文件夹 cd conf.d

新建一个.conf后缀文件(在这个文件夹下面其实已经有一个默认的 default.conf配置文件了)参考:wiki.nginx.org/FullExample

//taotao.config 个人新建的配置文件
server {
  listen 4001;#你但愿在外网访问的端口,记得必定要关掉防火墙
  location / {
      proxy_pass http://127.0.0.1:4030;#你在本机上运行的端口
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_cache_bypass $http_upgrade;
  }

   # listen 80;
   # server_name localhost;

    #charset koi8-r;
    #access_log /var/log/nginx/host.access.log main;

   # location / {
    # root /usr/share/nginx/html;
     # index index.html index.htm;
   # }

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    # proxy_pass http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    # root html;
    # fastcgi_pass 127.0.0.1:9000;
    # fastcgi_index index.php;
    # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    # include fastcgi_params;
    #}
server {
  listen 80;
  location / {
      proxy_pass http://127.0.0.1:3000;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_cache_bypass $http_upgrade;
  }

   # listen 80;
   # server_name localhost;

    #charset koi8-r;
    #access_log /var/log/nginx/host.access.log main;

   # location / {
    # root /usr/share/nginx/html;
     # index index.html index.htm;
   # }

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    # proxy_pass http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    # root html;
    # fastcgi_pass 127.0.0.1:9000;
    # fastcgi_index index.php;
    # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    # include fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    # deny all;
    #}
}

复制代码

配置好以后重启一下nginx服务器就行啦

到这里,一个简单的能在外网访问的node项目就完成了,但愿会对你们有用

相关文章
相关标签/搜索