{"id":6620,"date":"2024-06-20T06:48:27","date_gmt":"2024-06-20T06:48:27","guid":{"rendered":"https:\/\/www.smarthost.au\/blog\/?p=6620"},"modified":"2024-06-20T06:48:27","modified_gmt":"2024-06-20T06:48:27","slug":"installation-and-launch-of-node-js-on-the-smarthost-eu-hosting-account","status":"publish","type":"post","link":"https:\/\/www.smarthost.au\/blog\/installation-and-launch-of-node-js-on-the-smarthost-eu-hosting-account","title":{"rendered":"Installation and launch of Node.js on the Smarthost.eu hosting account"},"content":{"rendered":"\n<p><strong>How to Install and use Node.js on Smarthost Server Smarthost<\/strong> \u00a0 SSH access is required to use Node.js. Access can be enabled via the Customer Area <a href=\"https:\/\/www.smarthost.eu\/panel\/login\">https:\/\/www.smarthost.eu\/panel\/login<\/a> or all hosting accounts, except for the base-micro package. Click the magnifying glass icon next to the service, and then the Enable SSH button. The package cannot be in the trial version (must be paid for). \u00a0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Installation of Nodejs<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We connect to SSH using the port specified in the client panel. We can use Putty for this purpose.<\/li>\n<\/ol>\n\n\n\n<p>    2. We enter<\/p>\n\n\n\n<p>    cd<\/p>\n\n\n\n<p>    and press enter <\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>We select the Nodejs version we are interested in from the https:\/\/nodejs.org\/dist repository.<br>For the purposes of this guide, nodejs v21 will be used.<\/li>\n<\/ol>\n\n\n\n<p>    4. Now we need to download the selected version of nodejs through the terminal using the command:<\/p>\n\n\n\n<p>     <strong>wget https:\/\/nodejs.org\/dist\/latest-v21.x\/node-v21.1.0-linux-x64.tar.xz<\/strong><\/p>\n\n\n\n<p>    5. Then unpack the downloaded package:<\/p>\n\n\n\n<p>    6. We can change the name of the extracted directory to a more friendly one with the command:<\/p>\n\n\n\n<p>    <strong>mv node-v21.1.0-linux-x64 nodejs<\/strong><\/p>\n\n\n\n<p>    7.  We create a bin directory in our own home directory<\/p>\n\n\n\n<p>    mkdir bin<\/p>\n\n\n\n<p>    8. Copy the Node application there:<\/p>\n\n\n\n<p>    <strong>cp .\/nodejs\/bin\/node .\/bin<\/strong><\/p>\n\n\n\n<p>    9. We enter the bin directory:<\/p>\n\n\n\n<p>   10. We create a symbolic link:<\/p>\n\n\n\n<p>    <strong>ln -s ..\/nodejs\/lib\/node_modules\/npm\/bin\/npm-cli.js npm<\/strong><\/p>\n\n\n\n<p>   11. Node and npm are installed, we can type:<\/p>\n\n\n\n<p>   node &#8211;version<\/p>\n\n\n\n<p>   It should show:<\/p>\n\n\n\n<p>  v21.1.0<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"273\" height=\"35\" src=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/node_version-1.png\" alt=\"\" class=\"wp-image-6621\"\/><\/figure>\n\n\n\n<p>  And for the command:<\/p>\n\n\n\n<p>  npm &#8211;version<\/p>\n\n\n\n<p>  Appears:<\/p>\n\n\n\n<p>  10.2.0<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"280\" height=\"37\" src=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/npm_version-1.png\" alt=\"\" class=\"wp-image-6622\"\/><\/figure>\n\n\n\n<p>The node and npm commands will work from any directory by being placed in the bin directory.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Launching the application<\/strong><\/p>\n\n\n\n<p>First, you need to place your nodejs application in any directory on your hosting.<\/p>\n\n\n\n<p>We go to the directory where we put the application and then enter:<\/p>\n\n\n\n<p><strong>nohup node aplikacja.js &amp;<\/strong><\/p>\n\n\n\n<p>With <em><strong>&amp;<\/strong><\/em> the app will run in the background, while <em><strong>nohup<\/strong><\/em> will keep the app running after logging out. \u00a0<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Alternatively:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Type the command <strong>screen<\/strong> and confirm with enter<\/li>\n\n\n\n<li>We run our application <strong>node application.js<\/strong><\/li>\n\n\n\n<li>Press <strong> ctrl a<\/strong><\/li>\n\n\n\n<li>Then press <strong>d<\/strong><\/li>\n\n\n\n<li>Our screen will go to work in the background<\/li>\n\n\n\n<li>You can get back to it with the command <strong>screen -x<\/strong><\/li>\n<\/ol>\n\n\n\n<p><strong>Disabling the application<\/strong><\/p>\n\n\n\n<p>Enter:<\/p>\n\n\n\n<p>pkill node<\/p>\n\n\n\n<p>and we press enter.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Alternatively:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We return to screen with <strong>screen -x<\/strong><\/li>\n\n\n\n<li>We close our application with <strong>process.exit()<\/strong><\/li>\n\n\n\n<li>We can close the screen with the exit <strong>exit<\/strong> command<\/li>\n<\/ol>\n\n\n\n<p><strong>Redirection to the Node.js application to the selected domain<\/strong><\/p>\n\n\n\n<p>We place the following rules in the .htaccess file (this file is created in the domain directory, e.g. public_html\/client-domain.au):<\/p>\n\n\n\n<p> <\/p>\n\n\n\n<p>DirectoryIndex disabled<br>RewriteEngine On<br>RewriteRule ^$ http:\/\/127.0.0.1:PORT\/ [P,L]<br>RewriteCond %{REQUEST_FILENAME} !-f<br>RewriteCond %{REQUEST_FILENAME} !-d<br>RewriteRule ^(.*)$ http:\/\/127.0.0.1:PORT\/$1 [P,L]<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Instead of PORT, enter a number from the range 49152 to 65534. If the port is occupied by another Client, we select another one until the application will work on it. You should also remember to set the same port in the Node.js application.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>All you have to do now is enter our domain and you will see a page served by Node.js.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"275\" height=\"62\" src=\"https:\/\/www.smarthost.au\/blog\/wp-content\/uploads\/2024\/06\/image-4.png\" alt=\"\" class=\"wp-image-6623\"\/><\/figure>\n\n\n\n<p><strong>Redirect to secure https connection with SSL cPanel<\/strong><\/p>\n\n\n\n<p>We can also redirect traffic with the server to a secure https connection. Attention! We run the Node.js server as http, not https. The https connection is provided by our Apache server. The cPanel certificate is generated automatically after adding a domain via Additional Domains in cPanel, so you don&#8217;t have to worry about installing and configuring an SSL certificate.<\/p>\n\n\n\n<p>Use the following contents of the .htaccess file to force redirection (changing PORT to a number in the range given above):<\/p>\n\n\n\n<p>DirectoryIndex disabled<br>RewriteEngine On<br>RewriteCond %{HTTPS} on<br>RewriteRule ^$ http:\/\/127.0.0.1:PORT\/ [P,L]<br>RewriteCond %{HTTPS} on<br>RewriteCond %{REQUEST_FILENAME} !-f<br>RewriteCond %{REQUEST_FILENAME} !-d<br>RewriteRule ^(.*)$ http:\/\/127.0.0.1:PORT\/$1 [P,L]<br>RewriteCond %{HTTPS} !=on<br>RewriteCond %{REQUEST_URI} !^\/[0-9]+..+.cpaneldcv$<br>RewriteCond %{REQUEST_URI} !^\/[A-F0-9]{32}.txt(?:\\ Comodo\\ DCV)?$<br>RewriteCond %{REQUEST_URI} !^\/.well-known\/acme-challenge\/[0-9a-zA-Z_-]+$<br>RewriteCond %{REQUEST_URI} !^\/.well-known\/pki-validation\/[A-F0-9]{32}.txt(?:\\ Comodo\\ DCV)?$<br>RewriteRule ^ https:\/\/%{HTTP_HOST}%{REQUEST_URI} [L,R=301]<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>WebSocket support<\/strong><\/p>\n\n\n\n<p>For the ws:\/\/ protocol to work properly, you need to create rules in .htaccess, for example, for the socket.io library, add the following lines under the &#8220;RewriteEngine On&#8221; line (changing the PORT to the previously selected number):<\/p>\n\n\n\n<p>RewriteCond %{REQUEST_URI} ^\/socket.io [NC]<br>RewriteCond %{QUERY_STRING} transport=websocket [NC]<br>RewriteRule \/(.*) ws:\/\/localhost:PORT\/$1 [P,L]<\/p>\n\n\n\n<p><strong>Running Node.js from cPanel<\/strong><\/p>\n\n\n\n<p>We recommend running Node.js applications from cPanel. More information on this can be found in our blog post: <a href=\"https:\/\/www.smarthost.au\/blog\/how-to-run-node-js-in-cpanel\">https:\/\/www.smarthost.au\/blog\/how-to-run-node-js-in-cpanel<\/a><\/p>\n\n\n\n<p>Create a free test account on the <strong>Smarthost.au<\/strong> server and check how <strong>Node.js<\/strong> works.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.smarthost.au\/gold-ssd-hosting-plans\">Create a test hosting account with Node.js support<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Install and use Node.js on Smarthost Server Smarthost \u00a0 SSH access is required to use Node.js. Access can be enabled via the Customer Area https:\/\/www.smarthost.eu\/panel\/login or all hosting accounts, except for the base-micro<a class=\"read-more\" href=\"https:\/\/www.smarthost.au\/blog\/installation-and-launch-of-node-js-on-the-smarthost-eu-hosting-account\">Continue reading<\/a><\/p>\n","protected":false},"author":16,"featured_media":6624,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[262,1021,438,256],"tags":[377,119,132,137],"class_list":["post-6620","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpanel-hr","category-guides","category-hosting-pl","category-node-js-hr","tag-cpanel-node-js-pl","tag-guide","tag-node-js","tag-node-js-in-cpanel"],"_links":{"self":[{"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/posts\/6620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/comments?post=6620"}],"version-history":[{"count":1,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/posts\/6620\/revisions"}],"predecessor-version":[{"id":6625,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/posts\/6620\/revisions\/6625"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/media\/6624"}],"wp:attachment":[{"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/media?parent=6620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/categories?post=6620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.smarthost.au\/blog\/wp-json\/wp\/v2\/tags?post=6620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}