sudo apt install git-all
ssh-keygen -t rsa -b 4096 -C "server.ip"
cat ~/.ssh/id_rsa.pub
Copy nội dung của public key và tạo deploy keys cho dự án bằng cách truy cập repo dự án, nhấn Settings
, chọn Deploy Keys
ở sidebar, sau đó nhấn nút Add Deploy Key
.
Hoặc truy cập đường link https://github.com/<usename>/<project_name>/settings/keys
Dán public keys vào, ghi chú thông tin server để tiện quản lý, sau đó nhấn Add key
Để thao tác được trên server chúng ta cần có quyền truy cập vào server, hình thức phổ biến nhất là sử dụng SSH.
Copy SSH public key trên máy local cần access vào server.
Tại server, paste public key này vào file ~/.ssh/authorized_keys
, nếu server chưa có file này hãy tạo mới.
cd
mkdir ~/.ssh
touch ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Lưu ý node và pm2 cần được cài trên cả server mà máy local. Install node.js
sudo apt-get install nodejs npm
Install pm2
sudo npm install pm2 -g
CLI autocompletion
pm2 completion install
Suggest nên cài thêm yarn
vì bundle bằng yarn nhanh hơn npm.
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && sudo apt install yarn
Generate file ecosystem.config.js
bằng lệnh
pm2 init
template của nó như sau
module.exports = {
apps : [{
script: 'index.js',
watch: '.'
}, {
script: './service-worker/',
watch: ['./service-worker']
}],
deploy : {
production : {
user : 'SSH_USERNAME',
host : 'SSH_HOSTMACHINE',
ref : 'origin/master',
repo : 'GIT_REPOSITORY',
path : 'DESTINATION_PATH',
'pre-deploy-local': '',
'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production',
'pre-setup': ''
}
}
};
Về các options bên trong file ecosystem
các bạn có thể xem đầy đủ document trên trang chủ của pm2.
Ở đây mình sẽ example 2 file ecosystem
cho 2 trường hợp phổ biến sau
Ứng dụng single page, example với Vue
const production = {
user: 'deploy',
host: '192.168.10.10',
ref: 'origin/master',
repo: '[email protected]:trandaison/trandaison.github.io.git',
path: '/home/deploy/apps_www',
fetch: '--all',
'post-deploy': 'yarn install && yarn build production',
};
module.exports = {
apps: [{
name: 'trandaison.github.io',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
}],
deploy: {
dev: {
...production,
ref: 'origin/develop',
path: '/home/deploy/apps_dev',
"post-deploy": 'yarn install && NODE_ENV=development yarn build',
},
staging: {
...production,
ref: 'origin/staging',
path: '/home/deploy/apps_stg',
"post-deploy": 'yarn install && yarn build',
},
production,
}
};
Ứng dụng node, example với Nuxt.js (Vue Server Side Render)
module.exports = {
apps: [
{
name: "API",
script: "yarn",
cwd: "/home/deploy/nuxt-app/current",
error_file: "/home/deploy/nuxt-app/logs/web.err.log",
out_file: "/home/deploy/nuxt-app/logs/web.out.log",
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
args: "start",
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: "300M",
env: {
NODE_ENV: "development"
},
env_production: {
NODE_ENV: "production"
}
}
],
deploy: {
production: {
user: "deploy",
host: "192.168.13.3",
ref: "origin/develop",
repo: "[email protected]:trandaison/trandaison.github.io.git",
path: "/home/deploy/trandaison.github.io/",
"pre-deploy": "git fetch --all",
"post-deploy": "yarn install && yarn build && pm2 reload ecosystem.config.js --env production"
}
}
};
Trong lần deploy đầu tiên đòi hỏi bạn phải setup trước, từ máy local, chyạ lệnh sau
pm2 deploy production setup
Sau đó có thể deploy rồi
pm2 deploy production
sudo apt update
sudo apt install nginx
Tiếp theo, cần một vài bước thiết lập cấu hình nginx cho ứng dụng của bạn
server {
listen 8080;
server_name 172.25.32.10;
root /home/deploy/apps_www/current/dist;
access_log /home/deploy/apps_www/logs/nginx_access.log;
error_log /home/deploy/apps_www/logs/nginx_error.log;
try_files $uri $uri/ /index.html;
}
Tham khảo thêm tại đây.
⚠️ Mẹo:/home/deploy/apps_www/
là path của project nằm trong config ởecosystem
phía trên, thay đổi nó cho phù hợp với cài đặt của bạn. Để lấy đường đẫn dến thư mục hiện tại bạn có thể dùng lệnhpwd
.
Trước khi restart lại nginx, nếu có thực hiện chỉnh sửa cấu hình nginx thì khuyến khích bạn nên kiểm tra syntax trước khi restart.
nginx -t nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
Khởi động lại nginx để apply cấu hình vừa thay đổi.
sudo service nginx restart