Use PM2 gracefully to get your projects published automatically

Labels (separated by Spaces) : PM2

Shudong. wang/10613.html Shudong. wang/10613.html


Purpose: Execute commands locally and have the server automatically pull your code from Git to the server. Automatic every time, no need to manually log into the server to download the code, or copy it.

Premise: Push your own project to git repository. Of course, if it is your own company project, you do not want to make the project public. Private repositories can be used, and then private repositories on Github are very expensive at $20 per month

Generate key ssh-keygen -t rsa -c '[email protected]' change firewall port vim The/etc/iptables rules using nginx forward reverse proxy Can put the port forwarding to 8080 3000 80 80 at www.shudong.wang:3000/index.htmlCopy the code

The first step is to create a file.json file

The second step is to write the file. json configuration file

{
    "apps": [{
        "name": "student"."script": "app.js"."env": {
            "COMMON_VARIABLE": "true"
        },
        "env_production": {
            "NODE_ENV": "production"}}]."deploy": {
        "production": {
            "user": "ubuntu"."host": ["Host IP"]."port": 22."ref": "origin/master"."repo": "https://git.oschina.net/shudongwang/student.git"."path": "/home/ubuntu/www"."ssh_options": "StrictHostKeyChecking=no"."env": {
                "NODE_ENV": "production"}}}}Copy the code

Part III Implementation

The following command is to publish the local code to the server according to your configuration file, user initialization.

pm2 deploy ecosystem.json production setup

Use the following command to publish pM2 deploy file.json production after changing the code later