This is the 11th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021

To prepare

Install dependencies

Install PM2 globally on both your local and remote sites

npm install pm2 -g
Copy the code

Checking the Remote Server

  1. Update git Version

My Git version is 2.9.0; Otherwise, Git pull cannot be performed automatically and the code will not be up to date every time you deploy it

  1. perform/usr/bin/gitWhether the git command prompt will pop up, if not associated with the path
Whereis git sudo ln -s [real git location] /usr/bin/gitCopy the code

Bash: git: command not found

  1. Check whether the PM2 versions of the local and remote devices are the same

Basic configuration

// ecosystem.config.js

const config = {
    apps: {
        name: 'app name'.script: './bin/www'.// Service open entry file
    },
    deploy: {
        production: {
            user: 'root'.// Remote server user name
            host: ['101.34.36.247'].// Remote server IP address
            ref: 'origin/master'.// Git repository branch automatically pulled by PM2
            repo: '[email protected]:mytac/stock-tracker-server.git'.// Git address automatically pulled by PM2
            path: '/root/my-server'.// The pulled code is stored in the remote directory location}},}module.exports = config
Copy the code

After adding the above file, execute it locally

pm2 deploy production setup
Copy the code

If your config file name is not file.config. js, write the name of the config file after “deploy”. Enter the server password as directed.

After performing

pm2 deploy production
Copy the code

Enter the password as prompted. After the command output is successful, run the command on the remote device

pm2 ls
Copy the code

Execute pM2 ls several times until the status of several applications becomes stable. If online is displayed at last, see figure

The deployment is successful. You can check whether the deployment is successful online.

Error screen

If status displays error, execute

pm2 logs
Copy the code

You can locate specific problems. You may not be able to start services locally, or you may not have node_modules.

If pM2 logs cannot accurately locate the problem, try shutting down and restarting the PM2 first

pm2 kill

pm2 start
Copy the code

, check again for errors.

Avoid close operation

During deployment, you may be asked to enter the password of the server several times. In this case, secure Shell (SSH) secret free login is required.

A pair of SSH private keys and public keys must be generated on both the client and server

ssh-keygen
Copy the code

Then copy the public key of the client and paste it to the /root/.ssh/authorized_keys file on the server, and change the permission of the file to 600

chmod 600 authorized_keys
Copy the code

Then go to the client and type the command line

Ssh-copy-id -i Public key of the client root@remoteIpCopy the code

Try it at this point

ssh root@remoteIp
Copy the code

If you can directly enter the configuration success, if not, it should be a permission problem.

After successful password-free login, change PasswordAuthentication to no in /etc/ssh/sshd_config on the server and restart the SSHD service

systemctl restart sshd.service
Copy the code

And then you can log in without encryption. (See Article 2 of the Bibliography for details)

reference

  1. Pm2 official document
  2. SSH disable Remote password login — Windows &Linux client certificate login & Window cannot find ssh-keygen