Linux server on-line update and maintain a record of the node project

Foreword

First of all, I just developed a front-end Android and spicy chicken, background server-related knowledge involved is relatively small, I do not understand, learn from the following purely personal purposes, certainly a lot of problems with the actual not necessarily the same, light spray, the best Do not spray

Involving technical points: linux common commands, shell scripts, git use, firewall firewall, nginx, infotify, pm2, node and other related knowledge

Since last year, busy in taking the time to write a node with a system as a front-end, back-end servers do not understand the knowledge, so I want to go over the whole process:

Development -> Application domain / server deployment -> on line project

Under the first, simply publish a project has the following three warehouses:

You can see, the local code repository and server code repository is the same, can be relatively bare warehouse push / pull, because both already as native code to run in a local warehouse, warehouse server code to run on the server the reason for not bare warehouse directly when the project ran, but get a code repository, because the bare warehouse is encrypted, we simply do not understand, let alone as a normal program to run, but the clone will be out of normal shows, as to ask why go git, git source code to see.

1. The server creates a bare repository: git init --bare.

2. Create a server code repository, from bare warehouse clone

3. Create a local code repository

Then bind to the server bare warehouse: git remote add origin <username> @ <server public IP>: / <server bare warehouse address>. Username I use the root, if you feel unsafe, you can also create other users and then restrict permissions as root among the highest authority account, so a lot of security operations for other accounts such as git will create an account, to limit its authority only do control code.

4. forever / pm2 code repository server run up

Execution npm start, after nodejs project up and running, enabled by default port 3000, http: // localhost: 3030, we know that after npm start we can not carry out other operations, unless control + c to stop running, because we are not running in the background so limited, in order that it can run in the background, we can use tools, forever or pm2, specifically to see the relevant information, I used in the project is pm2, the following will explain the specific use

5. Use nginx domain name is bound to this project, the expansion firewall firewall

Such as access by shudongpo.com this domain name to our project, you need to bind the domain name and port 3030, so that it can be accessed outside the server project, released this be considered successful, we need to use this step to achieve nginx , nginx will use domain forwarding to port 3000, as follows:

Using yum install nginx: yum install -y nginx.

After installation: nginx -t View profile location.

Use vim editor nginx configuration file to add a server:

    server {
        listen       80;
        server_name  xxx.xxx.cn;
        location / {
            proxy_pass http://localhost:3000;
        }
    }

So that when you visit the xxx.xxx.cn you can access to run up the node project.

Note: Please open port 80 set out in a safe inside their own servers, so can not access; firewall Firewall If your linux server is installed, it is likely your firewall to shut down the port 80, so it can be opened: firewall -cmd --zone = public --add-port = 80 / tcp --permanent.

firewall knowledge expansion:

开放端口,firewall-cmd --zone=public --add-port=80/tcp --permanent
命令含义:
--zone #作用域
--add-port=80/tcp  #添加端口,格式为:端口/通讯协议
--permanent   #永久生效,没有此参数重启后失效
查看已开放端口,firewall-cmd --list-ports
重启,firewall-cmd --reload
查看默认防火墙状态,firewall-cmd --state

After configuration is complete, restart nginx: systemctl start nginx, if it has been turned restart.

Then enter binding domain on the computer can access the project.

6. Use inotify, shell scripts, pm2 automatically update line item [focus]

Now that the project has run up, then what is the problem? After modifying the code on a computer server push to a bare warehouse, but the server code repository we do not pull, so the code is not updated, even if we do not re-run the update program, or when access is not updated, it is necessary to solve the problem:

Going again> code repository update - push computer code - code repository after update> bare warehouse server receives an update

6.1. Code Computer push

Local push to perform

6.2. Bare warehouse server after receiving the update code repository update

This step requires two steps to take: 1 server listens to bare warehouse update; after two bare warehouse updates, automatic update code repository

Let me listen for updates bare warehouse, we can also use git hook implementation details, please see the relevant information on the case inotify me, inofity I used to listen

Installation inotify:

yum install inotify-tools -y

Monitor the implementation of direct write a shell script to run, touch update.sh:


#!/bin/bash
#监听裸仓库xxx.git改变完成后,执行xxx代码仓库进行pull
inotifywait -m -e modify -r /local/git_project/xxx.git/ |
while read events            #读取事件
do
cd /local/node_workspace/xxx #cd到代码仓库目录
git pull                     #pull代码
echo 'xxx.git裸仓库有变动,已pull最新代码到代码仓库'
echo $events                 #打印事件
done

inofity expand knowledge related commands:

inotifywait -m -e modify -r /local/git_project/xxx.git

-m expressed kept listening, or only once, -e modify represented listening event is modify (the contents of the file has been modified), - r represents a recursive monitor, but also monitor the child file

Script finished, but it's just a regular file, we need to give it execute permissions: chomd + x update.sh

Running script: ./ xxx.sh, so there will be a problem, this is not the background run, we can not withdraw once the background, so: ./ xxx.sh & background run, there are still problems, shell scripts process is the child shell terminal process process, when the terminal is closed shell, its children will be turned off, so the script will stop, so: nohup ./xxx.sh &, represent uninterrupted run in the background, that is, to run in the background, it will not shut down shell terminal interrupted

The script ran up, when we push the code on the local computer, the server will listen to inotify bare warehouses changed, then cd to the code repository execution pull, the code will update the code repository

6.3. After the code is updated automatically reload, and let the program run in the background, rather than npm start has been running in the foreground

This step is achieved by means of pm2 need tools to install: npm install pm2 -g

If the automatic reload only let it run in the background, without the need to update the code, you can: pm2 start bin / www

If the automatic reload after listening again to run both code updates in the background, so we need to configure the file, first of all: pm2 ecosystem, generate a configuration file config.js, then modify:

module.exports = {
  apps : [{
    name: 'shopsystem-server-pms',
    script: './bin/www',    #主要是这里,修改启动项

    // Options reference: https://pm2.keymetrics.io/docs/usage/application-declaration/
    args: 'one two',
    instances: 1,
    autorestart: true,
    watch: false,
    watch_options: {
      "usePolling": true
    },
    max_memory_restart: '1G',
    env: {
      NODE_ENV: 'development'
    },
    env_production: {
      NODE_ENV: 'production'
    }
  }],

  deploy : {
    production : {
      user : 'node',
      host : '212.83.163.1',
      ref  : 'origin/master',
      repo : '[email protected]:repo.git',
      path : '/var/www/production',
      'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production'
    }
  }
};

Then: pm2 start config.js

This will automatically monitor all folders and files in the current directory, if there is updated automatically reload

You need to know to please pm2 knowledge: https://pm2.keymetrics.io/docs/usage/application-declaration/

 

At this point, the entire process of project completion

Published 33 original articles · won praise 49 · Views 140,000 +

Guess you like

Origin blog.csdn.net/gsw333/article/details/105176839