- SSH into the target machine
- Make a deploy_keys folder and change into it
mkdir ~/.ssh/deploy_keys
cd ~/.ssh/deploy_keys
- Create a new key pair
ssh-keygen -t rsa -b 4096 -C "[email protected]"
- NOTE Set a custom path to save the keys in the current directory
sudo chmod 400 id_rsa
- Add key to ssh agent
eval "$(ssh-agent -s)"
ssh-add ./id_rsa
- Add the deploy key to your project on GitHub
- Go to GitHub
- Go to the desired repo, navigate to settings, and find deploy keys
- Create a new deploy key and copy your public key from the server
cat id_rsa.pub
- Copy all of the contents and create the deploy key
- Clone the project on the server and add GitHub to known hosts
- Switch back to root with
cd
git clone [email protected]:<username_or_orgname>/<project_name>.git
- On successful clone, remove the project:
rm -rf <project_name></project_name>
- Switch back to root with
- Add an ssh config to help PM2
nano ~/.ssh/config
- Add the following config with your parameters:
Host github.com HostName github.com User <username_or_orgname> IdentityFile ~/.ssh/deploy_keys/id_rsa
- Save with
ctrl+x
andy
andENTER
- From your local machine, run the pm2 init script (
npm run pm2-init
below)
If you use NVM to manage your node installation, ensure that your .bashrc
NVM activation script is at the top of the file, otherwise pm2 won't be able to access npm
.
More info here.
If your log files get too big, you can risk bricking your server. To avoid this, use a module to rotate your logs.
From the server, run pm2 install pm2-logrotate
.
More info here.
// sample ecosystem.config.js file for managing deployments
require('dotenv').config();
module.exports = {
apps: [
{
name: 'Your App',
script: './index.js',
},
],
deploy: {
production: { //
user: 'ubuntu',
host: process.env.HOST,
key: process.env.KEY_PATH,
ref: 'origin/master',
repo: '[email protected]:<username_or_orgname>/<project_name>.git',
path: '/home/ubuntu/<project_name>',
'post-deploy':
"cp ~/.env ~/<project_name>/source/.env && npm install && pm2 startOrRestart ecosystem.config.js",
},
},
};
Scripts to run from your local machine to interface with the remote pm2 instance.
"pm2-restart": "pm2 startOrRestart ecosystem.config.js",
"pm2-deploy": "pm2 deploy ecosystem.config.js production",
"pm2-logs": "pm2 deploy ecosystem.config.js production exec \"pm2 logs\"",
"pm2-ls": "pm2 deploy ecosystem.config.js production exec \"pm2 ls\"",
"pm2-flush": "pm2 deploy ecosystem.config.js production exec \"pm2 flush\"",
"pm2-init": "pm2 deploy ecosystem.config.js production setup" // run this first to initialize the install
https://github.com/keymetrics/pm2-logrotate
https://developer.github.com/v3/guides/managing-deploy-keys/#deploy-keys
https://pm2.keymetrics.io/docs/usage/deployment/
https://forum.magicmirror.builders/topic/2715/pm2-disable-logs/2
Thank you very much!, it's save my day