Ubuntu deployment uses PM2 to deploy nodejs project + PM2 and nginx to boot


Nodejs installation

  1. apt update–Update software source
  2. apt install nodejs–Install nodejs, but this version is 4.2.6 (only use nodejs – V to view, node is invalid)
  3. apt install npm–Install NPM
  4. npm install n -g–Install module N and upgrade nodejs with this module (be sure to add – G)
  5. n stable–Upgrade nodejs to the latest version (you can view the version with node – V at this time)

Openresty installation

  1. apt install libpcre3-dev libssl-dev perl make build-essential curl–Install development library
  2. wget https://openresty.org/download/openresty-–Download openresty installation package
  3. tar -xzvf openresty-–Unzip the installation package
  4. cd openresty-–Enter file
  5. ./configure --prefix=/usr/server/openrest–Install openresty to / usr / server / openresty (create the directory yourself)
  6. make, make install–Compile and install
  7. usr/server/openresty/nginx/sbin/nginx–Enable nginx

PM2 boot project and boot auto start

1.npm install pm2 -g–Command line global installation of PM2

2. I use express to create projects

CD usr / server / spider news / bin -- find the project address first
pm2 start www
pm2 save
pm2 startup

Nginx is added to system services and bootstrap

1. Enter the / etc / init. D directory to create the nginx script (change the path to the address of your nginx)


test -x $PROGRAM || exit 0
case "$1" in
     log_begin_msg "Starting Nginx server"
     log_end_msg 0
     PID=`cat /usr/server/openresty/nginx/logs/nginx.pid`
     log_begin_msg "Stopping Nginx server"
     if [ ! -z "$PID" ]; then
        kill -15 $PID
     log_end_msg 0
     $0 stop
     $0 start
     log_success_msg "Usage: service nginx {start|stop|restart}"
     exit 1
exit 0

2. Then run the following command:

sudo chmod +x nginx
sudo update-rc.d nginx defaults

Then boot nginx will automatically start, at the same time you can use the following command

sudo service nginx start
sudo service nginx stop