Once you’re familiar with Node.js, you’ll probably write your own little things to run on VPS, such as a personal API service, or your website. The content shared in this post is relatively applicable regardless of the size of your app because our principle is “Run it forever and deploy it Smartly”.

Upload code

The first thing you need to do is upload your app code locally to VPS, we can’t upload it from last century FTP. We use Git to synchronize local repository and remote repository code, you must have a feeling of trial and error, the same here we will use Git to operate.

All of the following can be replaced with the POD tool, but you need to use PM2 to manage the process accordingly. This is a relative general case.

First let’s make sure:

  • /var/repo– This is the directory on the VPS where the Git repo is stored
  • /var/www– This is the VPS website directory

Create an empty REPO on the VPS

$ mkdir -p /var/repo/app.git
$ cd /var/repo/app.git
$ git init --bare
Copy the code

Now you have a Git repository with only version management and no app source files.

Deployed hooks

You need a git hook, which automatically copies the uploaded files to your site directory after every local Git push. Here we use git’s built-in post-receive hook:

$CD hooks # Write content to this file $cat > post-receive # Enter:Copy the code
#! /bin/sh git --work-tree=/var/www/domain.com --git-dir=/var/repo/app.git checkout -f cd /var/www/domain.com npm install # There are also build scripts available, such as NPM Run BuildCopy the code

Finally, press Ctrl+D to confirm the save.

In order for you to have permission to execute this file, you need:

$ chmod +x post-receive
Copy the code

Local configuration

Exit the VPS:

$ exit
Copy the code

Execute in your app directory:

$ git remote add server ssh:[email protected]/var/repo/app.git
Copy the code

Wow, we’re done. Try publishing your Web app, simply by submitting it:

$ git add -A
$ git commit -m "deploy to server"
$ git push server master
Copy the code

Then check /var/www/domain.com, your code is synchronized.

Run your app consistently

You want to run your app persistently, because it’s likely to crash during rush hour, and you want your app to restart automatically when the system restarts.

You can use the Ghost Deployment Guide to do this. It’s quite complete, and I won’t repeat it, but I recommend Supervisor.

You can also consider using docker which is more modern, right? The solution to the problem.

read

Article errata and comments can be directly to the source code address to modify or submit an ISSUE, thank you for reading!