node.js
rickatech 2015-05
Related
Elsewhere
Reference [edit]
Updating this page for CentOS 7, systemd, and more modern yum repositories.
RHEL/CentOS 7 Install
rickatech 2015-07
Elsewhere [edit]
Core Node.js functionality has not yet been mainlined into the base RHEL repositories, however the venerable and stable EPEL repository provides a viable workaround. The node community has its own captive package management system called npm not surprisingly. Though npm is not mandatory to be able to run service side javascript, odds are there will be something one needs that is best obtained/updated through that service.
# cat /etc/redhat-release CentOS Linux release 7.1.1503 (Core) # yum install epel-release # yum install nodejs # rpm -qa 36a37 > libuv-0.10.34-1.el7.x86_64 > v8-3.14.5.10-17.el7.x86_64 > nodejs-0.10.36-3.el7.x86_64 > gpg-pubkey-352c64e5-52ae6884 > http-parser-2.0-4.20121128gitcd01361.el7.x86_64 > epel-release-7-5.noarch > c-ares-1.10.0-3.el7.x86_64 # node --version v0.10.36 # yum install npm # rpm -qa 38a39 > nodejs-semver-2.1.0-3.el7.noarch > nodejs-rimraf-2.2.2-1.el7.noarch > ... > gcc-c++-4.8.3-9.el7.x86_64 > npm-1.3.6-5.el7.noarch $ cat example.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/'); $ node example.js [ switch to another local terminal ] $ wget http://localhost:1337/any $ cat any Hello World
Elsewhere
Although Node.js provides a basic framework for running server side javascript in a manner that can accept web/http requests, it does not provide a standard daemon to allow this to occur in the background like venerable web services like Apache/httpd. Fortunately this is something Linux/Unix is extraordinarily gifted at supporting, and with more recent systemd services, it is rather straightforward to place some directives in a daemon file to allow as many javascripts applets to run asynchronous as you like.
# pwd /etc/systemd/system # ls -lh no* nodesvr-ricktest.service $ systemctl status nodesvr-ricktest nodesvr-ricktest.service - Node.js Example Server Loaded: loaded (/home/rickatech/node/sysd) ... $ cat nodesvr-ricktest.service [Unit] Description=Node.js Example Server #Requires=After=mysql.service # Requires mysql service to run first [Service] # ExecStart=/usr/local/bin/node /opt/nodeserver/server.js ExecStart=/bin/node /home/rickatech/node/example.js Restart=always RestartSec=10 # Restart service after 10 seconds if crashed StandardOutput=syslog # Output to syslog StandardError=syslog # Output to syslog # SyslogIdentifier=nodejs-example SyslogIdentifier=nodejs-ricktest #User=User=rickatech #Group= Environment=NODE_ENV=production PORT=1337 [Install] WantedBy=multi-user.target $ cat example.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/');
Elsewhere
For small hosting environments without load balancers and such it can be handy to be able to run straight Apache with PHP and Node.js on the same server. Using ProxyPass allows Apache to pass a particular URL path to a local service. This means Node.js services can be run independently of Apache locally on different ports while Apache can unify them into a URL based directory that serves static web and dynamic PHP requests for directories not mapped to Node.js . One thing to be mindful of is server sessions state - stay tuned for more on that.
<Directory /public/np1/site> # AllowOverride All Options Indexes FollowSymLinks AllowOverride None Require all granted /Directory> VirtualHost *:80> ServerName np1.local.zaptech.org DocumentRoot /public/np1/site # ErrorDocument 503 /maintenance.html ProxyRequests on # ProxyPass /maintenance.html ! ProxyPass /np1/ http://localhost:1337/ </VirtualHost>
node.js
circa 2011-12
# cat /etc/redhat-release CentOS release 6.2 (Final) # wget http://nodejs.tchol.org/repocfg/el/nodejs-stable-release.noarch.rpm Saving to: ?nodejs-stable-release.noarch.rpm? # rpm -qlp nodejs-stable-release.noarch.rpm /etc/pki/rpm-gpg/RPM-GPG-KEY-nodejs-stable-el-5 /etc/yum.repos.d/nodejs-stable.repo # yum localinstall nodejs-stable-release.noarch.rpm Installed: nodejs-stable-release.noarch 0:5-3 # yum list \*node\* nodejs-stable nodejs-stable/primary_db Installed Packages nodejs-stable-release.noarch 5-3 @/nodejs-stable-release.noarch Available Packages ... nodejs.i686 0.6.6-1.el6 nodejs-stable ... # yum install nodejs Installed: nodejs.i686 0:0.6.6-1.el6 Dependency Installed: libicu.i686 0:4.2.1-9.1.el6_2 v8.i686 0:3.6.6.11-1.el6 # cat example.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/'); # nodejs example.js Server running at http://127.0.0.1:1337/ $ wget http://localhost:1337/any $ cat any Hello World # cat serv.js var net = require('net'); var server = net.createServer(function (socket) { socket.write("Echo server\r\n"); socket.pipe(socket); }); server.listen(1337); // any IP address