[摘要] 多年前订阅了 Nodejs 的邮件列表,通过邮件列表的频度看整个 Nodejs 的用户还是挺活跃的,最近想体验下 nodejs 究竟是何方神圣,所以对nodejs的环境进行了体验,这次使用的是 nodejs pm2 的组合。
(一)安装nodejs
系统环境为CentOS6.7,所以使用了yum安装
(1)安装nodejs
yum install nodejs npm –enablerepo=epel
(2)安装pm2
由于官方不建议直接使用node启动服务,比较了多种进程管理工具后选择了使用pm2来对nodejs进行管理和维护
pm2的安装很简单:
npm install pm2 -g
(二)使用pm2启动nodejs
(1)准备测试的hello.js文件
cat hello.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello Sudops.com \n');
}).listen(8081, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8081/');
(2)使用pm2第一次运行nodejs

[root@vm03 app]# pm2 start hello.js
-------------
Looking for a complete monitoring and management tool for PM2?
_ _ _ _
| | _____ _ _ _ __ ___ ___| |_ _ __(_) ___ ___ (_) ___
| |/ / _ \ | | | '_ ` _ \ / _ \ __| '__| |/ __/ __| | |/ _ \
| < __/ |_| | | | | | | __/ |_| | | | (__\__ \_| | (_) |
|_|\_\___|\__, |_| |_| |_|\___|\__|_| |_|\___|___(_)_|\___/
|___/
Features
- Real Time Dashboard
- CPU/Memory monitoring
- HTTP monitoring
- Event notification
- Custom value monitoring
- Real Time log display
Checkout
https://keymetrics.io/
-------------
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /root/nodejs/app/hello.js in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬──────────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │
├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼──────────┼──────────┤
│ hello │ 0 │ fork │ 32397 │ online │ 0 │ 0s │ 0% │ 3.8 MB │ disabled │
└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴──────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
(3)pm2的一些命令
[root@vm03 app]# pm2 list ┌──────────┬────┬──────┬─────┬────────┬─────────┬────────┬─────┬───────────┬──────────┐ │ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │ ├──────────┼────┼──────┼─────┼────────┼─────────┼────────┼─────┼───────────┼──────────┤ │ hello │ 0 │ fork │ 331 │ online │ 0 │ 4s │ 0% │ 13.6 MB │ disabled │ └──────────┴────┴──────┴─────┴────────┴─────────┴────────┴─────┴───────────┴──────────┘ Use `pm2 show <id|name>` to get more details about an app [root@vm03 app]# pm2 info 0 Describing process with id 0 - name hello ┌───────────────────┬───────────────────────────────────┐ │ status │ online │ │ name │ hello │ │ restarts │ 0 │ │ uptime │ 19s │ │ script path │ /root/nodejs/app/hello.js │ │ script args │ N/A │ │ error log path │ /root/.pm2/logs/hello-error-0.log │ │ out log path │ /root/.pm2/logs/hello-out-0.log │ │ pid path │ /root/.pm2/pids/hello-0.pid │ │ interpreter │ node │ │ interpreter args │ N/A │ │ script id │ 0 │ │ exec cwd │ /root/nodejs/app │ │ exec mode │ fork_mode │ │ node.js version │ 0.10.48 │ │ watch & reload │ ✘ │ │ unstable restarts │ 0 │ │ created at │ 2017-01-08T11:37:19.828Z │ └───────────────────┴───────────────────────────────────┘ Code metrics value ┌────────────┬────────┐ │ Loop delay │ 0.98ms │ └────────────┴────────┘ Add your own code metrics: http://bit.ly/code-metrics Use `pm2 logs hello [--lines 1000]` to display logs Use `pm2 monit` to monitor CPU and Memory usage hello [root@vm03 app]# pm2 monit ⌬ PM2 monitoring (To go further check out https://app.keymetrics.io) ⌬ PM2 monitoring (To go further check out https://app.keymetrics.io) ● hello [ ] 0 % [0] [fork_mode] [||| ] 16.941 MB
[root@vm03 app]# ps aux|egrep "node|pm2" root 323 0.0 0.3 732236 28732 ? Ssl 19:37 0:01 PM2 v2.2.3: God Daemon (/root/.pm2) root 331 0.0 0.2 728248 17368 ? Ssl 19:37 0:01 node /root/nodejs/app/hello.js
(5)使用Nginx做反向代理
server {
listen 80;
server_name test.nodejstest001.com;
access_log /appdir/logs/nodejs_access.log main;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8081/;
access_log /appdir/logs/nodejs_access.log;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
(7)pm2的一些命令
[root@vm03 app]# pm2 list ┌──────────┬────┬──────┬─────┬────────┬─────────┬────────┬─────┬───────────┬──────────┐ │ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │ ├──────────┼────┼──────┼─────┼────────┼─────────┼────────┼─────┼───────────┼──────────┤ │ hello │ 0 │ fork │ 331 │ online │ 0 │ 4s │ 0% │ 13.6 MB │ disabled │ └──────────┴────┴──────┴─────┴────────┴─────────┴────────┴─────┴───────────┴──────────┘ Use `pm2 show <id|name>` to get more details about an app [root@vm03 app]# pm2 info 0 Describing process with id 0 - name hello ┌───────────────────┬───────────────────────────────────┐ │ status │ online │ │ name │ hello │ │ restarts │ 0 │ │ uptime │ 19s │ │ script path │ /root/nodejs/app/hello.js │ │ script args │ N/A │ │ error log path │ /root/.pm2/logs/hello-error-0.log │ │ out log path │ /root/.pm2/logs/hello-out-0.log │ │ pid path │ /root/.pm2/pids/hello-0.pid │ │ interpreter │ node │ │ interpreter args │ N/A │ │ script id │ 0 │ │ exec cwd │ /root/nodejs/app │ │ exec mode │ fork_mode │ │ node.js version │ 0.10.48 │ │ watch & reload │ ✘ │ │ unstable restarts │ 0 │ │ created at │ 2017-01-08T11:37:19.828Z │ └───────────────────┴───────────────────────────────────┘ Code metrics value ┌────────────┬────────┐ │ Loop delay │ 0.98ms │ └────────────┴────────┘ Add your own code metrics: http://bit.ly/code-metrics Use `pm2 logs hello [--lines 1000]` to display logs Use `pm2 monit` to monitor CPU and Memory usage hello [root@vm03 app]# pm2 monit ⌬ PM2 monitoring (To go further check out https://app.keymetrics.io) ⌬ PM2 monitoring (To go further check out https://app.keymetrics.io) ● hello [ ] 0 % [0] [fork_mode] [||| ] 16.941 MB
Usage: pm2 [cmd] app
Commands:
start [options] <file|json|stdin|app_name|pm_id...> start and daemonize an app
docker:gen [options] <file|config> generate Dockerfile in current folder
docker:dev [options] <file|config> wrap application into Docker container
docker:dist [options] <file|config> <image_name> wrap application into Docker container
trigger <proc_name> <action_name> [params] deploy your json
deploy <file|environment> deploy your json
startOrRestart <json> start or restart JSON file
startOrReload <json> start or gracefully reload JSON file
startOrGracefulReload <json> start or gracefully reload JSON file
stop [options] <id|name|all|json|stdin...> stop a process (to start it again, do pm2 restart <app>)
restart [options] <id|name|all|json|stdin...> restart a process
scale <app_name> <number> scale up/down a process in cluster mode depending on total_number param
snapshot snapshot PM2 memory
profile <command> profile CPU
reload <name|all> reload processes (note that its for app using HTTP/HTTPS)
gracefulReload <name|all> gracefully reload a process. Send a "shutdown" message to close all connections.
id <name> get process id by name
delete <name|id|script|all|json|stdin...> stop and delete a process from pm2 process list
sendSignal <signal> <pm2_id|name> send a system signal to the target process
ping ping pm2 daemon - if not up it will launch it
updatePM2 update in-memory PM2 with local PM2
update (alias) update in-memory PM2 with local PM2
install|module:install <module|git:/> install or update a module and run it forever
module:update <module|git:/> update a module and run it forever
module:generate [app_name] Generate a sample module in current folder
uninstall|module:uninstall <module> stop and uninstall a module
publish|module:publish Publish the module you are currently on
set <key> <value> sets the specified config <key> <value>
multiset <value> multiset eg "key1 val1 key2 val2
get [key] get value for <key>
conf [key] [value] get / set module config values
config <key> [value] get / set module config values
unset <key> clears the specified config <key>
link|interact [secret_key|command] [public_key] [machine_name] linking action to keymetrics.io - command can be stop|info|delete|restart
web launch a health API on 0.0.0.0:9615
dump|save dump all processes for resurrecting them later
resurrect resurrect previously dumped processes
unstartup [platform] disable and clear auto startup - [platform]=systemd,upstart,launchd,rcd
startup [platform] setup script for pm2 at boot - [platform]=systemd,upstart,launchd,rcd
logrotate copy default logrotate configuration
generate generate an ecosystem.json configuration file
ecosystem generate an ecosystem.json configuration file
reset <name|id|all> reset counters for process
describe <id> describe all parameters of a process id
desc <id> (alias) describe all parameters of a process id
info <id> (alias) describe all parameters of a process id
show <id> (alias) describe all parameters of a process id
list|ls [options] list all processes
l (alias) list all processes
status (alias) list all processes
jlist list all processes in JSON format
prettylist print json in a prettified JSON
monit|m launch termcaps monitoring
flush flush logs
reloadLogs reload all logs
logs [options] [id|name] stream logs file. Default stream all logs
kill kill daemon
pull <name> [commit_id] updates repository for a given app
forward <name> updates repository to the next commit for a given app
backward <name> downgrades repository to the previous commit for a given app
gc force PM2 to trigger garbage collection
deepUpdate performs a deep update of PM2
*
Options:
-h, --help output usage information
-V, --version output the version number
-v --version get version
-s --silent hide all messages
-m --mini-list display a compacted list without formatting
-f --force force actions
-n --name <name> set a <name> for script
-i --instances <number> launch [number] instances (for networked app)(load balanced)
-l --log [path] specify entire log file (error and out are both included)
-o --output <path> specify out log file
-e --error <path> specify error log file
-p --pid <pid> specify pid file
-k --kill-timeout <delay> delay before sending final SIGKILL signal to process
--listen-timeout <delay> listen timeout on application reload
--max-memory-restart <memory> specify max memory amount used to autorestart (in octet or use syntax like 100M)
--restart-delay <delay> specify a delay between restarts (in milliseconds)
--env <environment_name> specify environment to get specific env variables (for JSON declaration)
-x --execute-command execute a program using fork system
--max-restarts [count] only restart the script COUNT times
-u --user <username> define user when generating startup script
--hp <home path> define home path when generating startup script
-c --cron <cron_pattern> restart a running process based on a cron pattern
-w --write write configuration in local folder
--interpreter <interpreter> the interpreter pm2 should use for executing app (bash, python...)
--interpreter-args <arguments> interpret options (alias of --node-args)
--log-date-format <momentjs format> add custom prefix timestamp to logs
--no-daemon run pm2 daemon in the foreground if it doesn't exist already
--update-env update environmnent on restart/reload
--source-map-support force source map support
--only <application-name> with json declaration, allow to only act on one application
--disable-source-map-support force source map support
--wait-ready ask pm2 to wait for ready event from your app
--merge-logs merge logs from different instances but keep error and out separated
--watch [paths] watch application folder for changes
--ignore-watch <folders|files> folder/files to be ignored watching, chould be a specific name or regex - e.g. --ignore-watch="test node_modules "some scripts""
--node-args <node_args> space delimited arguments to pass to node in cluster mode - e.g. --node-args="--debug=7001 --trace-deprecation"
--no-color skip colors
--no-vizion start an app without vizion feature (versioning control)
--no-autorestart start an app without automatic restart
--no-treekill Only kill the main process, not detached children
--no-pmx start an app without pmx
--no-automation start an app without pmx
Basic Examples:
Start an app using all CPUs available + set a name :
$ pm2 start app.js -i 0 --name "api"
Restart the previous app launched, by name :
$ pm2 restart api
Stop the app :
$ pm2 stop api
Restart the app that is stopped :
$ pm2 restart api
Remove the app from the process list :
$ pm2 delete api
Kill daemon pm2 :
$ pm2 kill
Update pm2 :
$ npm install pm2@latest -g ; pm2 update
More examples in https://github.com/Unitech/pm2#usagefeatures
Deployment help:
$ pm2 deploy help
跟docker的结合
[root@vm03 app]# pm2 docker:gen hello.Dockerfile New Dockerfile generated in current folder You can now run $ pm2 docker:dev <file|config> [root@vm03 app]# cat Dockerfile FROM mhart/alpine-node:latest RUN apk update && apk add git && rm -rf /var/cache/apk/* RUN npm install pm2@next -g RUN mkdir -p /var/app WORKDIR /var/app COPY ./package.json /var/app RUN npm install ## DEVELOPMENT MODE ENV NODE_ENV=development CMD ["rundev", "start", "--auto-exit", "hello.Dockerfile", "--env", "development"]
(四)NodeJS小结
整体感觉NodeJS简单易用,非常轻量级,适合熟悉js的前、后端开发者,使用pm2管理方便可靠,加上Nginx一层的反向代理增加了web层面的可用性,只是目前还不清楚NodeJS还有哪些坑,作为爱折腾的你来说值得一试!



