{"id":3808,"date":"2016-09-06T10:38:26","date_gmt":"2016-09-06T10:38:26","guid":{"rendered":"http:\/\/www.drmop.com\/?p=3808"},"modified":"2016-09-06T10:44:34","modified_gmt":"2016-09-06T10:44:34","slug":"installing-and-running-node-js-on-a-vps","status":"publish","type":"post","link":"http:\/\/www.drmop.com\/index.php\/2016\/09\/06\/installing-and-running-node-js-on-a-vps\/","title":{"rendered":"Installing and running Node.js on a VPS"},"content":{"rendered":"<h1>Introduction<\/h1>\n<p>I recently had the requirement to implement a global none app store specific leaderboard system that can track scores and players for a mobile game that I am developing in Unity3D. After much investigation node.js seems to be the technology to use to create a server to handle it. I want it to be as quick as possible without having to resort to C++, plus I&#8217;ve been looking for an excuse to play with node.js in more detail. Ok, so I went to <a href=\"https:\/\/www.ovh.co.uk\/vps\/\" rel=\"nofollow\">ovh.net<\/a> and purchased one of their super cheap VPS (Virtual private server). I opted for Debian 8 LAMP (this is the Debian 8 Linux OS with, Apache web server, MySQL and PHP). After reading up the set up instructions, I managed to get logged into my VPS using PUTTY on Windows.<\/p>\n<h1>Installing node.js on a VPS<\/h1>\n<p>First thing to do is install node.js to the VPS. In the Putty terminal type the following:<\/p>\n<p>Download and run the Nodesource PPA (personal package archive) installer script:<br \/>\n<code><br \/>\ncd ~<br \/>\ncurl -sL https:\/\/deb.nodesource.com\/setup_6.x -o nodesource_setup.sh<br \/>\nsudo bash nodesource_setup.sh<br \/>\n<\/code><\/p>\n<p>Install node.js:<br \/>\n<code><br \/>\nsudo apt-get install nodejs<br \/>\n<\/code><\/p>\n<p>Install the build essentials for some packages that require compilation<br \/>\n<code><br \/>\nsudo apt-get install build-essential<br \/>\n<\/code><\/p>\n<h1>Testing out the node.js installation<\/h1>\n<p>Ok, so now we have node.js installed, its time to create a hello world and run it to ensure that it works. In terminal create hello.js:<br \/>\n<code><br \/>\ncd ~<br \/>\nnano hello.js<br \/>\n<\/code><\/p>\n<p>If you do not have the nano text editor installed then you can install it as follows:<br \/>\n<code><br \/>\nsudo apt-get install nano<br \/>\n<\/code><\/p>\n<p>Once the file is open add the following code:<\/p>\n<p>[sourcecode language=&#8221;js&#8221;]<br \/>\n#!\/usr\/bin\/env nodejs<br \/>\nvar http = require(&#8216;http&#8217;);<br \/>\nhttp.createServer(function (req, res)<br \/>\n{<br \/>\n    res.writeHead(200, {&#8216;Content-Type&#8217;: &#8216;text\/plain&#8217;});<br \/>\n    res.end(&#8216;Hello World!\\n&#8217;);<br \/>\n}).listen(8080, &#8216;localhost&#8217;);<br \/>\nconsole.log(&#8216;Server is running at http:\/\/localhost:8080\/&#8217;);<br \/>\n[\/sourcecode]<\/p>\n<p>Make the script executable:<br \/>\n<code><br \/>\nchmod +x .\/hello.js<br \/>\n<\/code><\/p>\n<p>Run the script:<br \/>\n<code><br \/>\n.\/hello.js<br \/>\n<\/code><\/p>\n<p>Note that the script is blocking because it is sat in an infinite loop waiting for connections, so you can no longer type anything into terminal. To test, run another instance of terminal (Putty on Windows) and enter the following to test the script:<br \/>\n<code><br \/>\ncurl http:\/\/localhost:8080<br \/>\n<\/code><br \/>\nYou should see &#8220;Hello World&#8221; printed out which is the response from the hello.js script.<\/p>\n<h1>Installing and using Process Manager (PM2) for node.js<\/h1>\n<p>The next issue we need to look at is how to make our script run in the background so it does not block. To do that we need install a tool called PM2 (process manager for node.js):<br \/>\n<code><br \/>\nsudo npm install -g pm2<br \/>\n<\/code><\/p>\n<p>Once installed we can set our script off running as a background process with:<br \/>\npm2 start hello.js<\/p>\n<p>And to make PM2 re-run when the server reboots:<br \/>\n<code><br \/>\npm2 startup systemd<br \/>\n<\/code><\/p>\n<p>Note that when PM2 restarts it will restart all of its running processes.<\/p>\n<h1>Getting node.js to work with Apache<\/h1>\n<p>The general idea is to run the node.js server on a different port and forward requests to a specific url to this port using a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Reverse_proxy\" rel=\"nofollow\" target=\"_blank\">reverse proxy<\/a>. To do this we need to update an Apache config file httpd.conf. Note that if you do not find this file in the \/etc\/apache2\/ directory then you will need to create it and add the following text:<\/p>\n<p><code> ProxyPass \/node http:\/\/localhost:8000\/<br \/>\nLoadModule proxy_module modules\/mod_proxy.so<br \/>\nLoadModule proxy_http_module modules\/mod_proxy_http.so<br \/>\n<\/code><\/p>\n<p>And add the following line to \/etc\/apache2\/apache2.conf to ensure that the file gets included<\/p>\n<p><code>Include \/etc\/apache2\/httpd.conf<br \/>\n<\/code><\/p>\n<p>Note that it is likely that the proxy module is not enabled on Apache, in which case enable it using:<\/p>\n<p><code>a2enmod proxy_http<br \/>\n<\/code><\/p>\n<p>Now requests to http:\/\/www.yourdomain.com\/node will be forwarded to localhost:8000, adjust the original hello world node.js script to listen on port 8000 and give it a test.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction I recently had the requirement to implement a global none app store specific leaderboard system that can track scores and players for a mobile game that I am developing in Unity3D. After much investigation node.js seems to be the technology to use to create a server to handle it. I want it to be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[626,624,3,625,623],"tags":[751],"class_list":["post-3808","post","type-post","status-publish","format-standard","hentry","category-linux","category-nodejs","category-programming","category-unity3d","category-web-development-2","tag-nodejs"],"_links":{"self":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts\/3808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/comments?post=3808"}],"version-history":[{"count":11,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts\/3808\/revisions"}],"predecessor-version":[{"id":3820,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts\/3808\/revisions\/3820"}],"wp:attachment":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/media?parent=3808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/categories?post=3808"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/tags?post=3808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}