preface

This article mainly introduces Nodejs in ubuntu deployment online environment related knowledge, including environment configuration, scripting tools, PM2 application, Ngix reverse proxy, etc., constantly improving

Recommend this article https://zhuanlan.zhihu.com/p/23778500

Deploy online projects through PM2

PM2 is a built-in load balancer for Nodejs application for production process management. It allows applications to run continuously, reload in 0 seconds, and perform common system administration tasks easily.

The main features are: 1) built-in load balancing 2) background running 3) 0 second reloading: maintenance and upgrade without downtime, reloading code without losing connection 4) console detection

Official ADDRESS Indicates the official API address

The deployment of

  • Install the PM2 instruction

      $ npm install pm2 -g
    Copy the code
  • Configuration pm2. Json

      {
        "apps" : [{
          "name"        : "www",
          "cwd"         : "/data/source/",
          "script"      : "bin/www",
          "watch"       : true,
          "node_args"   : "--harmony",
          "merge_logs"  : true,
          "error_file"  : "../logs/stderr.log",
          "out_file"    : "../logs/stdout.log",
          "pid_file"   : "../pids/child.pid",
          "exec_mode"  : "cluster_mode",
          "instances"  : 0,
          "log_date_format" : "YYYY-MM-DD HH:mm Z", 
          "env": {
            "NODE_ENV": "staging"
          }
        }]
      }
    Copy the code

From the package.json above we specify a lot of content name: sets the name of the current project to be displayed in pM2 CWD: specifies the source code location of the project error_file: command line error output out_file: command line error output evn.Node_ENV: environment variable

  • Start the project from the PM2 configuration file

    start_node_project.sh

    #! /data/ rm -rf source/* # Unzip code/${tagname} ${tagname}  -d source/Copy the code

    # add time suffix to the compressed package and move it to the specified directory. Used for version rollback mv code/${tagname} code/project_date +%Y%m%d%H% m. zip # Switch to the tool directory CD /data/deploy/ # Start the project pm2 from the configuration file Ps # check node process – ef | grep node

      ./start_node_project
    Copy the code
  • View project health

      pm2 list
    Copy the code

  • Track resource health

      pm2 monit
    Copy the code

  • If we want to view the detailed operating status of an application, we can run:

      pm2 descrbe {appId}
    Copy the code
  • See the log

      pm2 logs
    Copy the code
  • Restart the application

      pm2 restart {appId}
    Copy the code
  • Stop running program

      pm2 stop pm2.json
    Copy the code

Refer to the article

Nodejs deployment mode n/A Pm2 Online nodeJS deployment summary

Nginx reverse proxy

Nginx (” Engine X “) is a high-performance HTTP and reverse proxy server, as well as an IMAP/POP3/SMTP proxy server.

Nginx is used to reverse proxy Nodejs because, despite node.js’ performance, handling static transactions is not its expertise, such as: Gzip encoding, static files, HTTP caching, SSL processing, load balancing and reverse proxy and multi-site proxy can all be done with Nginx to reduce the load on Node.js, and with nginx’s powerful cache to save your site traffic and speed up your site load.

Although Node.js also has some proxy modules, such as HTTP-Proxy, which can realize setting up multiple websites on one server (each domain name maps to the port of different NodeJS process), this basic work should be handed over to NgniX in fact. Let’s take a look at some basic operations.

  • Install Nginx

      apt-get install nginx
    Copy the code
  • Write a simple configuration file/usr/local/nginx/conf/gaidu/nginx. Conf

    server { listen 80; server_name h.gaidu.cn;

      access_log /data/nginx_log/h.gaidu.cn/access.log short;
      error_log /data/nginx_log/h.gaidu.cn/error.log ;
    
      if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot")
      {
      	return 403;
      }
      location /  {
              proxy_pass http://10.1.2.147:9099;
              proxy_set_header  Host "h.gaidu.cn";
              proxy_set_header  X-Real-IP  $remote_addr;
              proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_set_header  X-Forwarded-Proto  $scheme;
      }
    Copy the code

    }

  • Introduce the newly created custom profile into the nginx master profile

    CD/usr/local/nginx/conf, # open nginx. Conf, include in the HTTP to add/usr/local/nginx/conf/gaidu / *Copy the code
  • Restart the nginx

      /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    Copy the code
  • Access through the site

      h.gaidu.cn
    Copy the code
  • Note: Test the NGIx configuration file

      nginx -t -c /usr/local/nginx/conf/gaidu/nginx.conf
    Copy the code

To be continued