node.jshttps://nodejs.org/en/download/stable/
Download
It is recommended to use a stable compiled version, source code is slightly troublesome, compiled directly available, installation super simple, red is the CentOS X64 available address.
Https://nodejs.org/dist/v5.7.1/node-v5.7.1-linux-x64.tar.xz
1. Download the installation package
[[email protected] software]# wget http://nodejs.org/dist/v5.7.1/node-v5.7.1-linux-x64.tar.xz--2016-13L0614:17:04--http://nodejs.org/dist/v5.7.1/node-v5.7.1-linux-x64.tar.xzresolving nodejs.org ...104.20.22.46,104.20.23.46Connecting to Nodejs.org|104.20.22.46|:Connected. HTTP request sent, awaiting response ...OKLength:8374600 (8.0M) [application/x-xz]savingTo: "Node-v5.7.1-linux-x64.tar.xz "100%[====================================================== ===============================================================================================>] 8,374,600 41.5K/s Span class= "Hljs-keyword" >in 2m 22s 2016-03-06 14 :19:37 (57.7 kb/s)-"node-v5. 7.1-linux-x64.tar.xz "saved [8374600/ 8374600]
The original HTTPS link is easy to come down, look at the network situation, you can remove the s after download faster.
2. Unzip the file Tar.xz
Because it is the tar.xz end of the file, to XZ, and then tar a bit.
[[email protected] software]# xz-d NODE-V5.7.1-LINUX-X64.TAR.XZ [[email protected] software]# TAR-XVF Node-v5.7.1-linux-x64.tar ode-v5.7.1-linux-x64/node-v5.7.1-linux-x64/bin/node-v5.7.1-linux-x64/bin/npmnode-v5.7.1-linux-x64/bin/nodenode-v5.7.1-linux-x64/share/node-v5.7.1-linux-x64/share/man/node-v5.7.1-linux-x64/share/man/man1/node-v5.7.1-linux-x64/share/man/man1/node.1node-v5.7.1-linux-x64/share/systemtap/node-v5.7.1-linux-x64/share/systemtap/tapset/node-v5.7.1-linux-x64/share/systemtap/tapset/node.stpnode-v5.7.1-linux-x64/share/doc/node-v5. 7.1-linux-x64/share/doc/node/node-v5. 7.1-linux-x64/share/doc/node/gdbinitnode-v5. 7.1-linux-x64/licensenode-v5. 7.1-linux-x64/include/node-v5. 7.1-linux-x64/include/node/node-v5. 7.1-linux-x64/include/node/zconf.hnode-v5. 7.1-linux-x64/include/node/uv-sunos.h[[email protected] Software]# cd node[[email protected] node]# CD bin[[email protected] bin]< Span class= "Hljs-comment" >#./node-vv5. 7.1
Wait for the decompression to complete, it is recommended to change the extracted directory to node, or other meaningful name, or node upgrade too quickly on the original basis of the upgrade but the file directory display version name does not match.
3. Configure node to run globally
export NODE_HOME=/file/software/nodeexport PATH=$NODE_HOME/bin:$PATH #找到下边这一行添加进去,记得你自己的路径搞对了。export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
Restart it and try it, NODE,NPM can be enabled globally.
Recommend the above global configuration, of course, the configuration of soft links can also be global command line, this is not recommended.
[[email protected] bin]# ln -s /file/software/node/bin/node /usr/local/bin/node[[email protected] bin]# ln -s /file/software/node/bin/npm /usr/local/bin/npm[[email protected] bin]# cd /[[email protected] /]# dirbin boot cgroup dev etc file home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var[[email protected] /]# npm -v3.6.0[[email protected] /]#
The basic environment has been built, but it's not enough.
4, PM2 escort
Forever has been out, seriously recommended PM2 way to run Nodejs, which is the best, not one.
Built-in Load balancer (using Node cluster cluster module)
Running in the background
0 seconds of downtime overload, I understand the general meaning is that maintenance upgrades do not require downtime.
Start-up scripts with Ubuntu and CentOS
Stop an unstable process (avoid infinite loops)
Console detection
Provide HTTP API
Remote control and real-time interface API (Nodejs module, allows interaction with PM2 process Manager)
PM2 official website http://pm2.keymetrics.io/
[[email protected]/]# NPM install-g pm2 start downloading installation files │└──[email protected]0.8├──[email protected]11.2├─┬[email protected]6.0│└──[email protected]0.11├──[email protected]0.1├─┬[email protected]0.9│├──[email protected]3.1│├──[email protected]1.2│├──[email protected]0.1│└──[email protected]0.1├─┬[email protected]3.6│├─┬[email protected]0.5││└──[email protected]1.0│└──[email protected]0.1├─┬[email protected]2.1│├──[email protected]4.2│└──[email protected]0.18├─┬[email protected]1.2│└──[email protected]1.2├──[email protected]6.1├──[email protected]1.0├──[email protected]6.0├─┬[email protected]4.0│└─┬[email protected]1.32│└──[email protected ]0. 0└─┬[email protected]2.12└──[email protected]< Span class= "Hljs-number" >9.0 Runtoplevellifecycles▌ ╢████████████████████████████████████████████████████████████████████████████████NPM WARN Optional Skipping failed Optional Dependency/pm2/chokidar/fsevents:runtoplevellifecycles? ╢████████████████████████████████████████████████████████████████████████████████NPM WARN Notsup not compatible With your operating system or architecture: [email Protected]0. 8runTopLevelLifecycles? ╢████████████████████████████████████████████████████████████████████████████████
Success.
5, test a HelloWorld
Helloworld.js
var http = require(‘http‘);http.createServer(function (req, res) { res.writeHead(200, { ‘Content-Type‘: ‘text/plain‘ }); res.end(‘Hello World\n‘); }).listen(1337, "127.0.0.1"); console.log(‘Server running at http://127.0.0.1:1337/‘);
PM2 Start Try
[[email protected] www]# pm2 start helloworld.js--name ' HelloWorld ' [PM2] spawning PM2 DAEMON[PM2] PM2 successfully d AEMONIZED[PM2] Starting helloworld.js in fork_mode (1 instance) [PM2] Done.┌────────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────────────┬──────────┐│app name│id│mode│ pid│status│restart│uptime│memory│watching│├────────────┼────┼──────┼──────┼────────┼─────────┼────────┼── ───────────┼──────────┤│helloworld│0│fork│2251│online│
0│0s│
14.715 mb│disabled │└────────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────────────┴──────────┘use PM2 show <id|name> ' to get more details about an app[[email protected] Www] #pm2 monit
is not very magical, next put PM2 into the system boot.
[[email protected] www]# pm2 startup centos[PM2] Generating system init script in /etc/init.d/pm2-init.sh[PM2] Making script booting at startup...[PM2] /var/lock/subsys/pm2-init.sh lockfile has been added[PM2] -centos- Using the command: su -c "chmod +x /etc/init.d/pm2-init.sh; chkconfig --add pm2-init.sh"[PM2] Done.[[email protected] www]# pm2 save[PM2] Dumping processes要保存一下!
There's more.
First go to the PM2 official online registration of accounts, charges are better, we look at the free version.
[[email protected] ~]# pm2 link key1 ke2 [62server][Keymetrics.io] Using (Public key: yklukcus7ugg7u6) (Private key: fhgynshuxtahahd)[Keymetrics.io] [Agent created] Agent ACTIVE - Web Access: https://app.keymetrics.io/
Key1 and Key2 are on the website after registration.
The system provides monitoring key
System monitoring 6, supplemental issues, how to open ports
[[Email protected] ~]#/sbin/iptables-i input-p TCP--dport 1337-j accept[[email protected] ~]#/etc/rc.d/init.d/iptables saveiptables:saving firewall rules to/etc/ Sysconfig/iptables:[OK][[email protected] ~]#/etc/ Init.d/iptables statustable:filterchain INPUT (policy ACCEPT) num Target prot opt source Destination 1 ACCEPT TCP--0. 0.0. 0/0 0. 0.0. 0/0 tcp dpt:1337
NODE.JS&PM2 Build node production environment