Some time ago, when the company deployed the project, operation and Maintenance students said a lot of things about the server, immediately felt confused, confused…
Let’s take this project as an exercise and briefly understand how the process of deploying a project to a server works
Preliminary knowledge
pm2
Pm2 is a management tool for starting node processes
Common commands
…
Pm2 start app.js: Starts the service. The entry file is app.js
Pm2 list Displays processes that are started
Pm2 show XXX Displays details about a service
NPM restart [name or ID] : restarts the service
Pm2 MONIT: Monitors services
…
A package.json file for a project
To start the service, you can use Node Run app.js
To start the service with pm2, type pm2 start app.js
To view the running projects, type pm2 list on the command line
Pm2 supports configuration file startup
- Script Start script path
- Exec_mode Specifies the application startup mode, including fork and cluster
- Instances Number of started application instances. This parameter is valid only in cluster mode. The default value is fork
Fork and Cluster modes
Fork: a cluster can start multiple processes
Add a pM2 configuration file, pm2.config.json, to the project
{
"name": "mxx-project"."script": "./index.js"."error_file": "./logs/err.log"."out_file": "./logs/out.log"."log_date_format": "YYYY-MM-DD HH:mm Z"."instances": 3."merge_logs": true."exec_mode": "cluster"."node_args": ""."ignore_watch": ["node_modules"]."env": {
"NODE_ENV": "development"}}Copy the code
Enter on the command line
pm2 start pm2.config.json
Copy the code
Three processes will be created
Note: If your server is multi-core, it is possible to create multiple processes in cluster mode
You can check how many cores your server has
shipit
Shipit is an automated server deployment tool
A simple shipit configuration file
module.exports = shipit => {
require('shipit-deploy')(shipit)
shipit.initConfig({
default: {
workspace: '/tmp/myapp',
deployTo: '/var/myapp',
repositoryUrl: 'Your GitHub address',
ignores: ['.git'.'node_modules'],
keepReleases: 2,
deleteOnRollback: false,
key: '/path/to/key',
shallowClone: true,
},
staging: {
servers: 'Your server address',}})}Copy the code
Connecting to the server
After the purchase of the server to obtain the IP address can try to log in to the server
Generally, you can log in to your own server as root
ssh root@ipipip
Copy the code
Before deploying the project to the server, you are advised to do the ssh-copy-id command to establish trust so that you do not need to re-enter the password
Configure the environment on the server
NPM CNPM node pm2 Git nginx
- Install the NPM
yum install npm
Copy the code
- Install CNPM
npm install -g cnpm --registry=https://registry.npm.taobao.org
Copy the code
- Install the pm2
npm i pm2
Copy the code
- Install the node
npm i n
Copy the code
- Install git
yum install git
Copy the code
- Install the NVM
Wget - qO - https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bashCopy the code
Need to adjust vim ~/.bashrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh"] &&."$NVM_DIR/nvm.sh" # This loads nvm
Copy the code
Specify the default Node version
nvm alias defalut 8
Copy the code
Start a simple service
Let’s write a simple script on the server. I’m creating a new app.js file in the /home/work directory.
cd home
mkdir work
touch app.js
Copy the code
Write a simple service in app.js
const http = require('http')
const port = 3389
http.createServer((req, res) => {
res.end('Hello word! ')
}).listen(port, () => {
console.log(port, 'port')})Copy the code
Start the app. Js
node app.js
Copy the code
Then open your browser, type IP+ port number, and you will see hello Word in the page
– added –
1 Port 3389 is used because port 3389 has been configured in the security group on my server
If you are using a different port make sure you have a security group configured
2 About starting the service
If you are using Node, you will no longer be able to access the service once you exit the server
You can use PM2 instead so that once started, the process will always exist unless an error is reported
Ready to deploy
- Prepare project
The project uses koA1 + React
React project address point I view the GitHub address
Change the startup port number to 3389
- Prepare the PM2 startup file
Create a pm2 folder and write an production.json file to start pm2
- The basic configuration for creating a shipit.js in the project is as follows
module.exports = function (shipit) {
require('shipit-deploy')(shipit)
require('shipit-cnpm')(shipit)
require('shipit-pm')(shipit)
shipit.initConfig({
default: {
workspace: '/tmp/deploy/your-project',
deployTo: '/home/work/your-project',
repositoryUrl: 'https://github.com/youproject.git',
ignores: ['.git'.'node_modules'],
keepReleases: 2,
deleteOnRollback: false,
key: '/path/to/key',
shallowClone: true,
cnpm: {
flags: '--production'.local: false,
npm: 'cnpm',
remote: true
},
pm: {
production: {
path: '/home/work/your-project/current/pm2/production.json'
}
}
},
production: {
servers: ['root@ Your IP address ']}})}Copy the code
Pm2 is started using the configuration file, so the PM parameter is configured to start PM2 according to the file path
- Add two fields to your project’s package.json
"deploy": "shipit production deploy"."rollback": "shipit production rollback".Copy the code
- Push the project to GitHub
- Run NPM run deploy
After the project is deployed, check the project progress on the server
pm2 list
Copy the code
Open the page to see the effect
So far, it’s been deployed
Problems occurred during deployment
- Error 1
Resolution: Forgot to install Git on the server
- Error 2
This is obvious, there is no CNPM installed
- After successful deployment, go to the server to check the project did not start
The original deployment is completed
Then I turned to one of the gods and found it was not in the shipit configuration file
require('shipit-pm')(shipit)
Copy the code
[Smash the table!]
Adjust the file and go online again
Three processes are started
Configure Aliyun according to the project port number
If the port number is not configured in the security group when your project starts, you need to add a security group in the background
You also need to enter port 3389 when accessing the page. Too lazy to enter a port number
Add the default port 80 to the security group and change the port 80 in the startup file app.js. This eliminates the need to enter a port number
So what if you don’t want to modify the app.js file, use NGINX
Configure NGINX
- Install nginx
yum install nginx
View the NGINX configuration file
cat /etc/nginx/nginx.conf
Copy the code
Very simple configuration
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
Copy the code
Change the port number in the project entry file app.js to 9999
Processing Nginx. Conf. Js
Finally, change to (the important thing is that the server part is only shown here)
server { listen 80; server_name localhost; Location / {proxy_pass http://127.0.0.1:9999/; }}Copy the code
Start the Nginx
nginx -s reload
Copy the code
Problem with Nginx configuration
- Restart the Nginx
Nginx keeps reporting errors during restart
Refer to the article for post-processing
nginx -c /etc/nginx/nginx.conf
nginx -s reload
Copy the code
Do ~
Welcome to visit the point where I view the project
Refer to the article
- pm2
- PM2 usage skills
- Summary of pM2 process management tool usage
- Use shipit-deploy for automated multi-server deployment
- Use Shipit to automate Node application deployment
- I met Nginx
- Nginx. pid is lost after nginx restart. How do I restart nginx