1. Introduction

Yesterday I saw the API of netease Cloud Music Node. js version, but I didn’t know how to use it because my understanding of Node. js is only in some sentences. It turns out that nodeapp.js can be run directly from the local server localhost. I thought I’d hook him up to my VPS. I worked on it all afternoon. Post this experience

My VPS is centos 6 version

Specific steps:

1. Node.js NPM configuration 2. Git configuration (my VPS is already configured when I do gitPage, so I won't write it) 3Copy the code

2. Configuration process

1. The node. Js configuration

reference

Since there are really many ways to install files on Linux, I am really confused because I have no requirements for the version of Node, so I directly installed the specific version number.

You are advised to perform the following operations in the /opt directory

Downloading compressed packages

Wget HTTP: / / http://developer.jpanj.com/node-v10.15.3-linux-x64.tar.xzCopy the code

Decompress the package to a tar file

xz -dThe node - v10.15.3 - Linux - x64. Tar. XzCopy the code

If the xz command does not exist, check the blog or follow the steps to type

Wget http://tukaani.org/xz/xz-5.2.2.tar.gz `Copy the code
` tar ZXF xz - 5.2.2. Tar. Gz `Copy the code
./configure  
make && make install
Copy the code

Unpack the

The tar XVF - node - v10.15.3 - Linux - x64. TarCopy the code

A soft link in the current directorynodeThe directory out

The advantage of this is that it is very convenient to update the soft chain in the future

ln -s. / node - v10.15.3 - Linux - x64. / nodeCopy the code

Through soft links, executables are placed in paths to system environment variables

  • View the environment variable paths in the current system
# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
Copy the code

You can see in my list:

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

The conventional logic is:

  • /usr/binThe following are pre-installed executables that will change as the system is upgraded.
  • /usr/local/binA directory is a place for users to put their own executable programs

So I recommend placing the soft chain in /usr/local/bin:

ln -s /opt/node/bin/node /usr/local/bin/node
ln -s /opt/node/bin/npm /usr/local/bin/npm
Copy the code

But in this step I encountered -bash: CD: SRC: The problem of Too many levels of — symbolic links, the subsequent solution is after my CD ~ ln -s/opt/node/bin/node/usr/local/bin/node.

If file exists, delete the command file at the address to which you softlink

Check whether the installation is successful

[root@dc8 ~]# node -vV10.15.3 [root @ dc8 ~]# npm -v
6.4.1
Copy the code

2. pm2

reference

Pm2 is designed to keep your Node projects permanently mounted in the background and automatically load balance them

Install the pm2

npm i -g pm2

Through soft links, executables are placed in paths to system environment variables

Unlike Windows, NPM cannot be used directly in the global environment once it is downloaded

So we have to do what we did before

ln -s /opt/node/bin/pm2 /usr/local/bin/pm2
Copy the code

The Node project can then always be mounted in the background

use

Pm2 start app.js // Start the pM2 project pm2 stop all // Stop all projects PORT=4000 pm2 start app.js // Use the netease cloud background program, start it in the project directoryCopy the code

3. Nginx

Reference 1

Reference 2

Step 1: create a source configuration file nginx.repo in /etc/ymp.repos. d/ :

cd /etc/yum.repos.d/
Copy the code
vim nginx.repo
Copy the code

Fill in the following:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
Copy the code

Save, will create a/etc/yum repos. D/nginx. Repo file.

To automatically install Nginx, run the following command:

yum install nginx -y
Copy the code

Nginx is now ready to start:

/etc/init.d/nginx start
Copy the code

Now that Nginx is up and running, you can access the Nginx welcome page directly from the server.

If not, configure a Linux firewall.

iptables -I INPUT 5 -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
Copy the code

Note here that Nginx will then have to open any port that it listens on. I stepped on a hole here and now we’re releasing the default port 80

service iptables save
Copy the code
service iptables restart
Copy the code

Nginx commands and configuration file locations:

/etc/init.d/nginx start # Start Nginx service
 
/etc/init.d/nginx stop # Stop Nginx service

/etc/nginx/nginx.reload Restart the Nginx service
 
/etc/nginx/nginx.conf # Nginx configuration file location


chkconfig nginx on    Set to boot
Copy the code

At this point, Nginx has been configured and installed.

2. Multiple services can be configured on a host

Forward requests through proxies in your nginx: go to the conf.d folder CD conf.d in the nginx folder

Create a new one. The conf suffix file (under the folder is already has a default default. Conf configuration file) reference: wiki.nginx.org/FullExample

// taoto. config my new configuration file server {listen 4001;Make sure to turn off the firewall for ports you want to access from the extranetLocation / {proxy_pass http://127.0.0.1:4030;The port you want to run on this machineProxy_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;
    #}
}

Copy the code

Once configured, restart the nginx server

At this point, a simple Extranet accessible Node project is complete, hopefully useful to you