preface
Record some problems encountered in the process and correction knowledge;
The previous use of NUXT 1.4, just do the memo, interested to see, no interest stop;
The problem
The development mode is normal, but static resources cannot be found in deployment mode
Since I’m using Nginx, this requires nginx static resource identification
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
expires 7d;
access_log off;
}
location ~ .*\.(js|css)? $ {
expires 7d;
access_log off;
}
Copy the code
The CSS background image is missing
Change the path ~/assets in background to ~assets;
Template is still written as ~/assets
CentOS installnode-sass
The problem of hanging up
Those without SCSS can be ignored
At first, I thought it was a lack of compilation environment. I checked all of these things with make.
Finally, it was found that there was a problem with the wall. This module went to the source of CNPM and went smoothly
In the personal directory of the deployment user, perform the following operations
Write an NPM environment configuration file
vim ~/.npmrc
# write, these several rely on go to the domestic CNPM source
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
phantomjs_cdnurl=https://npm.taobao.org/mirrors/phantomjs/
electron_mirror=https://npm.taobao.org/mirrors/electron/
registry=https://registry.npm.taobao.org/
Copy the code
Hot deployment problem
Conventional posture
- local
git
Push -> Run to line to pull (no hook written) - Pack (Pack again) :
nuxt build
- Restarting the service (
pm2
Restart service) :pm2 restart id|name
And on the server packaging,CPU all kinds of running full, I tidy up a little, so that the maintenance is a little more controllable
Simplify the posture
- Pack locally, pack locally
git
To submit, pm2
Deploy, auto pull, reload process
To get right to the point, I’m using nuxt + KOA, and actually this one doesn’t talk about KOA either
package.json
# deploy is "scripts": {"dev": "Cross-env NODE_ENV=development HOST=0.0.0.0 nodemon server/index.js --watch server", "build": "nuxt build", "start": "node server/index.js", "generate": "nuxt generate" }Copy the code
Write a file.config.js configuration file in the project root directory,
module.exports = {
apps: [{name: 'nuxt2-sx-share'.script: 'npm'.args: 'run start'.watch: ['.nuxt'].// Monitor the output directory
watch_options: {
usePolling: true
},
exec_mode:'cluster'.env: {
HOST: '0.0.0.0'.PORT: 4444.NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'.HOST: '0.0.0.0'.PORT: 4444
},
output: './logs/console.log'.error: './logs/consoleError.log'}].deploy: {
production: {
// SSH user
user: 'crper'.// SSH host
host: ['xxx'].// SSH options with no command-line flag, see 'man ssh'
// can be either a single string or an array of strings
ssh_options: 'StrictHostKeyChecking=no'.// GIT remote/branch
ref: 'origin/master'.// GIT remote
repo: '[email protected]:lqh/nuxt-sx-mobile-share.git'.// path in the server
path: '/data/xixi/nuxt-sx-mobile-share'.// Pre-setup command or path to a script on your local machine
'pre-setup': 'ls -la'.'pre-deploy':'git pull'.// deploy hook
'post-deploy':
'npm install && pm2 reload ecosystem.config.js --env production'}}}Copy the code
The configuration file consists of two parts: apps(information about starting applications, environment variables, process execution mode, etc.) and Deploy (deployment region).
SSH configuration and warehouse information are not mentioned
Here we focus on deployment. My script uses three hooks, initialization, predeployment, and push execution
pre-setup
: is called for initialization. I’m just showing the directory structure herepre-deploy
Before deploying, execute, this hook is normally not needed heregit pull
Because every timeupdate
I’m going to make a changepost-deploy
: Accepts push-triggered hooks, installs dependencies and reloads services
After writing this configuration file, as long as your server permissions (including user groups) are correctly configured, the server needs to install PM2 (start service).
I wrote four aliases myself
#pm2
alias pm2init="pm2 deploy ecosystem.config.js production setup"
alias pm2prod="pm2 deploy ecosystem.config.js production "
alias pm2up="pm2 deploy ecosystem.config.js production update"
alias pm2rev="pm2 deploy ecosystem.config.js production revert"
Copy the code
Start the deployment
Install a global PM2 locally
- Deployment initialization:
pm2init
That triggers the pull item, clones to the corresponding location and what happensshare
andsource
(Code here) - Start the service (if the first step is successful, the inside will automatically start, otherwise manually start, error):
pm2prod
- Update overloaded service:
pm2up
rendering
Official deployment documentation
Ecosystem configurable items
conclusion
You ask why I don’t do persistent integration… Who wouldn’t want to do it under conditions?
If there is something wrong, you can leave a message and correct it in time. Thank you for reading