{"id":25894,"date":"2020-12-21T02:44:48","date_gmt":"2020-12-20T18:44:48","guid":{"rendered":"https:\/\/web.mwwsb.com.my\/pjci\/?post_type=kb&#038;p=25894"},"modified":"2022-09-08T21:38:51","modified_gmt":"2022-09-08T13:38:51","slug":"how-to-install-and-configure-node-js","status":"publish","type":"kb","link":"https:\/\/www.casbay.com\/guide\/kb\/how-to-install-and-configure-node-js","title":{"rendered":"HOW TO: Install and configure Node.js"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"25894\" class=\"elementor elementor-25894\" data-elementor-post-type=\"kb\">\n\t\t\t\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-0a6d1eb elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"0a6d1eb\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-c9cf92b\" data-id=\"c9cf92b\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-element elementor-element-4c9eb12 elementor-widget elementor-widget-heading\" data-id=\"4c9eb12\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t<h2 class=\"elementor-heading-title elementor-size-medium\">HOW TO: Install and configure Node.js\n<\/h2>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-5d9c1c1 elementor-widget elementor-widget-text-editor\" data-id=\"5d9c1c1\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p>This article describes how to install the <a href=\"https:\/\/nodejs.org\/en\/\" rel=\"noopener\">Node.js<\/a> platform. You can use Node.js to host third-party applications or you can run your own applications.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4bfd3f8 elementor-widget elementor-widget-heading\" data-id=\"4bfd3f8\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">INSTALLATION PREREQUISITES<\/h2>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-1e3bd09 elementor-widget elementor-widget-text-editor\" data-id=\"1e3bd09\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p>There are two prerequisites for running Node.js on a server:<\/p><ul><li>Your account must have the normal shell (not jailshell) enabled. To determine which shell your account is using, log in\u00a0using\u00a0<strong>SSH<\/strong>, and then type the following command:<pre><strong>echo $SHELL<\/strong><\/pre><\/li><li>Node.js currently only works on servers running CentOS 6 or CentOS 7. To determine the CentOS version on your server, log in to your account using SSH, and then type the following command:<pre><strong>uname -r<\/strong><\/pre><p>If the output from this command contains\u00a0<strong>el6<\/strong>\u00a0(for example,\u00a0<em>2.6.32-531.17.1.lve1.2.60.el6.x86_64<\/em>) or\u00a0<strong>el7<\/strong>\u00a0(for example,\u00a0<em>3.10.0-714.10.2.lve1.5.12.el7.x86_64<\/em>) , then your server is running CentOS 6 or CentOS 7. However, if you see\u00a0<strong>el5h<\/strong>\u00a0(for example,\u00a0<em>2.6.32-531.23.3.lve1.2.66.el5h.x86_64<\/em>), then your server is running CentOS 5 and does\u00a0<strong>not<\/strong>\u00a0support Node.js.<\/p><\/li><\/ul>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-e5b7095 elementor-widget elementor-widget-heading\" data-id=\"e5b7095\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">INSTALLING NODE.JS AND NPM<\/h2>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-e53a21b elementor-widget elementor-widget-text-editor\" data-id=\"e53a21b\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p>After you determine that your account meets the installation prerequisites, you can download and install Node.js and npm (the Node.js package manager). To do this, follow these steps:<\/p><ol><li>Log in to your\u00a0server using SSH.<\/li><li>At the command prompt, type the following commands:<pre><strong>cd ~\nwget https:\/\/nodejs.org\/dist\/v4.4.1\/node-v4.4.1-linux-x64.tar.xz<\/strong><\/pre><p>This command downloads the binaries for the Node.js version 4.4.1 LTS (long-term support) release. If you download a newer development version of Node.js, change the version numbers in the following commands to match the version you downloaded.<\/p><\/li><li>To extract the Node.js files, type the following command:<pre><strong>tar xvf node-v4.4.1-linux-x64.tar.xz\n<\/strong><\/pre><\/li><li>Rename the extracted folder to the more convenient\u00a0<em>nodejs<\/em>\u00a0name, type the following command:<pre><strong>mv node-v4.4.1-linux-x64 nodejs\n<\/strong><\/pre><\/li><li>To install the\u00a0<em>node<\/em>\u00a0and\u00a0<em>npm<\/em>\u00a0binaries, type the following commands:<pre><strong>mkdir ~\/bin\ncp nodejs\/bin\/node ~\/bin\ncd ~\/bin\nln -s ..\/nodejs\/lib\/node_modules\/npm\/bin\/npm-cli.js npm\n<\/strong><\/pre><p>After you run these commands, Node.js and npm are installed on your account. To verify this, type the following commands:<\/p><pre><strong>node --version\nnpm --version<\/strong><\/pre><\/li><\/ol>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f00bd2a elementor-widget elementor-widget-heading\" data-id=\"f00bd2a\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">STARTING A NODE.JS APPLICATION<\/h2>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d80827f elementor-widget elementor-widget-text-editor\" data-id=\"d80827f\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p>After you install Node.js, you are ready to run Node.js applications. However, the exact steps to do this vary depending on the application configuration.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ed5f90a elementor-widget elementor-widget-heading\" data-id=\"ed5f90a\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t<h2 class=\"elementor-heading-title elementor-size-medium\">Method #1: Use npm<\/h2>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-239fbce elementor-widget elementor-widget-text-editor\" data-id=\"239fbce\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p><span style=\"font-size: 16px;\">Many third-party and \u201cproduction-ready\u201d applications use the <\/span><em style=\"font-size: 16px;\">npm<\/em><span style=\"font-size: 16px;\">program to start the application, as shown by the following command:<\/span><\/p><pre><strong>nohup npm start --production &amp;\n<\/strong><\/pre><p>The\u00a0<em>&amp;<\/em>\u00a0places the command in the background, and the\u00a0<em>nohup<\/em>\u00a0command ensures that the application continues running even if you log out of the current terminal session.<\/p><p>For this method to work, there must be a valid\u00a0<em>package.json<\/em>\u00a0file for the application. The\u00a0<em>package.json<\/em>\u00a0file contains project metadata that the\u00a0<em>npm<\/em>\u00a0program reads to determine how to start the application, manage its dependencies, and more.<\/p><p>To view the official\u00a0<em>npm<\/em>\u00a0documentation for the\u00a0<em>package.json<\/em>\u00a0file.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-eb79561 elementor-widget elementor-widget-heading\" data-id=\"eb79561\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t<h2 class=\"elementor-heading-title elementor-size-medium\">Method #2: Run node directly<\/h2>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d9796fe elementor-widget elementor-widget-text-editor\" data-id=\"d9796fe\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p>For simple applications, or for any application that does not have a\u00a0<em>package.json<\/em>\u00a0file, you can run the\u00a0<em>node<\/em>\u00a0executable directly and specify the application filename. For example:<\/p><pre><strong>nohup node my_app.js &amp;<\/strong><\/pre><p>However, you lose the benefits of using\u00a0<em>npm<\/em>\u00a0to manage the application.<\/p><p>As above, the\u00a0<em>&amp;<\/em>\u00a0places the command in the background, and the\u00a0<em>nohup<\/em>command ensures that the application continues running even if you log out of the current terminal session.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ed40993 elementor-widget elementor-widget-heading\" data-id=\"ed40993\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">STOPPING A NODE.JS APPLICATION<\/h2>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ca440a1 elementor-widget elementor-widget-text-editor\" data-id=\"ca440a1\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p>To stop a currently running Node.js application, type the following command:<\/p><pre><strong>pkill node<\/strong><\/pre><p>This command immediately stops all running Node.js applications.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-cc84247 elementor-widget elementor-widget-heading\" data-id=\"cc84247\" data-element_type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">INTEGRATING A NODE.JS APPLICATION WITH THE WEB SERVER<\/h2>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b765edc elementor-widget elementor-widget-text-editor\" data-id=\"b765edc\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p>Depending on the type of Node.js application you are running, you may want to be able to access it using a web browser. To do this, you need to select an unused port for the Node.js application to listen on, and then define server rewrite rules that redirect visitors to the application. The following steps demonstrate how to do this:<\/p><ol><li>In a text editor, add the following lines to the\u00a0<em>.htaccess<\/em>\u00a0file in the\u00a0<em>\/home\/username\/public_html<\/em>\u00a0directory, where\u00a0<em>username<\/em>\u00a0represents your account username:<pre>RewriteEngine On\nRewriteRule ^$ http:\/\/127.0.0.1:<em>XXXXX<\/em>\/ [P,L]\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule ^(.*)$ http:\/\/127.0.0.1:<em>XXXXX<\/em>\/$1 [P,L]<\/pre><p>These rewrite rules redirect incoming HTTP visitor requests to the Node.js application running locally on the server.\u00a0<em>127.0.0.1<\/em>\u00a0is the loopback address for\u00a0<em>localhost<\/em>.<\/p><\/li><li>In both\u00a0<strong>RewriteRule<\/strong>\u00a0lines, replace\u00a0<em>XXXXX<\/em>\u00a0with the port on which your Node.js application listens.<br \/>To run a Node.js application on a managed server, you must select an unused port between\u00a0<strong>49152<\/strong>\u00a0and\u00a0<strong>65535\u00a0<\/strong>(inclusive).<\/li><li>Save the changes to the\u00a0<em>.htaccess<\/em>\u00a0file, and then exit the text editor. Visitors to your web site are redirected to the Node.js application listening on the specified port.<\/li><\/ol>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>HOW TO: Install and configure Node.js This article describes how to install the Node.js platform. You can use Node.js to host third-party applications or you can run your own applications. INSTALLATION PREREQUISITES There are two prerequisites for running Node.js on a server: Your account must have the normal shell (not jailshell) enabled. To determine which [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"site-sidebar-layout":"no-sidebar","site-content-layout":"page-builder","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"disabled","ast-breadcrumbs-content":"","ast-featured-img":"disabled","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"default","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}}},"kbtopic":[60],"kbtag":[106],"mkb_version":[],"_links":{"self":[{"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kb\/25894"}],"collection":[{"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kb"}],"about":[{"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/types\/kb"}],"author":[{"embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/comments?post=25894"}],"version-history":[{"count":1,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kb\/25894\/revisions"}],"predecessor-version":[{"id":36606,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kb\/25894\/revisions\/36606"}],"wp:attachment":[{"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/media?parent=25894"}],"wp:term":[{"taxonomy":"kbtopic","embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kbtopic?post=25894"},{"taxonomy":"kbtag","embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/kbtag?post=25894"},{"taxonomy":"mkb_version","embeddable":true,"href":"https:\/\/www.casbay.com\/guide\/wp-json\/wp\/v2\/mkb_version?post=25894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}