1. Introduction to the general deployment method of nodejs application
Finally, the nodejs application deployment is launched, copy the source code to the directory through git
Copy the code as follows:/root/deploy/movie
Then make the command:
The code copy is as follows:
~ cd /root/deploy/movie
node ./app.js
In the above method, the nodejs program will be run in the current console interface, and the application will also be stopped once the console is completed. Let's change the command to let the program run in the background
The code copy is as follows:
~ node ./app.js &
[1] 21333
[2013-06-21 09:38:30.696] [INFO] console - Start App: http://VeVB.COM
[2013-06-21 09:38:30.700] [INFO] console - Express server listening on port 3000
This way the program will be started in the background. The process is running normally, and I don't have to do too much.
What if I want to stop this program? Find the nodejs system process and kill it.
The code copy is as follows:
~ ps -aux|grep node
root 21333 0.6 3.7 909200 38292 pts/0 Sl 09:38 0:00 node app.js
~ kill -9 21333
Direct violent solution. How great it would be if it could start and close the nodejs application like a system service! Next, we will use upstart to encapsulate nodejs applications into system services.
2. Encapsulate the application into an upstart task script
The code copy is as follows:
~ vi /etc/init/nodejs-moive.conf
description "node.js VeVB.COM"
start on startup
stop on shutdown
script
export HOME="/root/deploy/movie"
echo $$ > /var/run/moiveme.pid
export NODE_ENV=production
exec /usr/bin/node /root/deploy/movie/server.js
#Log output
#exec /usr/bin/node /root/deploy/movie/server.js >> /var/log/moiveme.log 2>&1
end script
pre-start script
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/moiveme.log
end script
pre-stop script
rm /var/run/moiveme.pid
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/moiveme.log
end script
3. Use upstart to manage nodejs applications
Start the nodejs-moive application (the task script above), process ID: 21257
The code copy is as follows:
~ start nodejs-moive
nodejs-moive start/running, process 21257
~ tail -f /var/log/moiveme.log
[2013-06-21T09:21:17.122Z] (moive.me) Starting
~ ps aux|grep node
root 21257 8.0 3.7 909204 37824 ? Ssl 09:21 0:00 /usr/bin/node /root/deploy/movie/server.js
Check the running status, process 21257 is running normally
The code copy is as follows:
~ status nodejs-moive
nodejs-moive start/running, process 21257
Kill nodejs application process 21257, and manage through upstart, the nodejs-moive application will automatically restart
The code copy is as follows:
~ kill -9 21257
#Automatically restart log
~ tail -f /var/log/moiveme.log
[2013-06-21T09:21:33.662Z] (moive.me) Starting
# Check the system process and find that the ID has changed
~ ps -aux|grep node
root 21280 9.1 3.7 909204 37704 ? Ssl 09:21 0:00 /usr/bin/node /root/deploy/movie/server.js
# Check the process status, the process ID has indeed changed, and it is automatically completed
~ status nodejs-moive
nodejs-moive start/running, process 21280
This makes it very convenient for us to manage nodejs applications in the form of system services through upstart. It will be easy to operate and maintain! !