Run node.js on cpanel hosting server

Viewed 110812

It is a simple node.js code.

var http = require('http');
http.createServer(function(req, res) {
    res.writeHead(200, { 'Content-Type' : 'text/plain'});
    res.end('Hello World!');
}).listen(8080);

I uploaded it on cpanel hosting server and installed node.js and run it. If a server is normal server I can check script result by accessing 'http://{serverip}:8080'. But on cpanel is hosting domain and sub domain and every domain is matched by every sites. Even http://{serverip} is not valid url. How can I access my node.js result? Kindly teach me. Thanks. bingbing.

6 Answers

Install/Setup NodeJS with CPanel

1. Log in to your account using SSH (if it is not enabled for your account, contact the support team).

2. Download Node.js

wget https://nodejs.org/dist/latest/node-v10.0.0-linux-arm64.tar.xz

3. Extract the Node.js files

tar xvf node-v10.0.0-linux-arm64.tar.xz

4.Now rename the folder to "nodejs". To do this, type the following command

mv node-v10.0.0-linux nodejs

5. Now to install the node and npm binaries, type the following commands:

mkdir ~/bin <br> cp nodejs/bin/node ~/bin
cd ~/bin
ln -s
../nodejs/lib/node_modules/npm/bin/npm-cli.js npm

6. Node.js and npm are installed on your account. To verify this, type the following commands

node --version npm --version

The ~/bin directory is in your path by default, which means you can run node and npm from any directory in your account.

7. Start Node.js Application

nohup node my_app.js &

8. Stop the Application

pkill node

9. Integrating a Node.js application with the web server(optional)

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.

In a text editor, add the following lines to the .htaccess file in the/home/username/public_html directory, where username represents your account username:

RewriteEngine On
RewriteRule ^$ http://127.0.0.1:XXXXX/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:XXXXX/$1 [P,L]

In both RewriteRule lines, replace XXXXX with the port on which your Node.js application listens. To run a Node.js application on a managed server, you must select an unused port, and the port number must be between 49152 and 65535(inclusive). Save the changes to the .htaccess file, and then exit the text editor. Visitors to your website are redirected to the Node.js application listening on the specified port.

If your application fails to start, the port you chose may already be in use. Check the application log for error codes like EADDRINUSE that indicate the port is in use. If it is, select a different port number, update your application’s configuration and the .htaccess file, and then try again.


Yes it's possible, but it has few dependencies which may or may not be supported by either your cpanel hosting provider or the plan you opt in for.

Below steps that I'm mentioning is just for a demo purpose. If you are a student or just want to play with it you can try it out. I'm not a security expert so from security point of view how good it is I really don't know.


So with that being said let's see how I configured it. I have hostinger cpanel hosting subscription and following are the steps:

Enable SSH ACCESS

ssh access in hostinger cpanel

enter image description here

Connect to shared machine via ssh

enter image description here

enter image description here

Check your linux distro and download & setup node js

In my case following are the commands for that:

Downloading node & extracting it using curl
curl https://nodejs.org/dist/v12.18.3/node-v12.18.3-linux-x64.tar.gz |tar xz

enter image description here enter image description here

This will download & extract node & create a directory. You can confirm that using ls command as shown in the image below.

enter image description here

At this point you can check the versions as shown below

enter image description here

as you can see for the node command it's okay but for the npm command we have modify it as follows

./node-v12.18.3-linux-x64/bin/node ./node-v12.18.3-linux-x64/lib/node_modules/npm/bin/npm-cli.js --version

enter image description here

Further we can create alias to make life little easier

check the below images for that:

enter image description here

enter image description here

I tried using bashrc/bash_profile but somehow it didn't work .

And that's all node server running on a shared cpanel machine.


Now I wanted to have an express js based rest api support in this case. The problem with that is it will be locally hosted on the port I'll give. Check the below example:

var express=require('express')

var app=express()

app.get('/', function (req, res) {
    res.send('hosting node js base express api using php & shared hosting a great way to start yjtools')
  })

console.log("listening yjtools node server on port 49876...")
app.listen(49876)

The problem here is even though it will execute I'll not be able to access it over the network. This is because we only get fixed predefined ports (like 80,21,3306 etc.) which are allowed/open on the shared cpanel machine. Due to this the express app I hosted will only available locally on 49876 port.

Let's see what do we have:

  • An express js based app hosted locally on cpanel machine.
  • Php based hosted Apache server available over http/https.

So we can make use of php with redirect rule set and curl to bridge the gap. Following are the changes I did to make it work:

In .htaccess file add a redirect rule, say domain/api is what I want my rest api path to be.

RewriteRule api/(.*)$ api/api.php?request=$1 [QSA,NC,L]

In the api/api.php file (this is the path I choose you can choose any path)

  <?php
echo "Hello ".$_REQUEST['username'];


echo '<hr>';

$curl = curl_init('http://127.0.0.1:49976/');
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//Get the full response
$resp = curl_exec($curl);
if($resp === false) {
    //If couldn't connect, try increasing usleep
    echo 'Error: ' . curl_error($curl);
} else {
    //Split response headers and body
    list($head, $body) = explode("\r\n\r\n", $resp, 2);
    $headarr = explode("\n", $head);
    //Print headers
    foreach($headarr as $headval) {
        header($headval);
    }
    //Print body
    echo $body;
}
//Close connection
curl_close($curl);
?>

And on the ssh prompt just run the app.js file

node api/app.js

Below are the images for this working in action:

enter image description here

enter image description here

Here is the similar thing which I referred for my program, so we can also make this node call via php itself.


Now I have express based rest api support , angular app hosted and mysql for database everything on cpanel.

cPanel Version 80 has nodejs 10.x support: https://documentation.cpanel.net/display/80Docs/80+Release+Notes#id-80ReleaseNotes-InstallanduseNode.jsapplications

Install and use Node.js applications

You can now install and use Node.js applications on your server. To use Node.js, install the ea-nodejs10 module in the Additional Packages section of WHM's EasyApache 4 interface (WHM >> Home >> Software >> EasyApache 4).

You can register Node.js applications in cPanel's Application Manager interface (cPanel >> Home >> Software >> Application Manager). For more information, read our Guide to Node.js Installations documentation.

For Application Manager to be enabled: https://documentation.cpanel.net/display/80Docs/Application+Manager

Your hosting provider must enable the Application Manager feature in WHM's Feature Manager interface (WHM >> Home >> Packages >> Feature Manager).

Your hosting provider must install the following Apache modules:

The ea-ruby24-mod_passengermodule. Note: This module disables Apache's mod_userdir module.

The ea-apache24-mod_env module. Note: This module allows you to add environment variables when you register your application. For more information about environment variables, read the Environment Variables section below.

The ea-nodejs10 module if you want to register a Node.js™ application.

You can see how application manager looks like in this Youtube video: https://www.youtube.com/watch?v=ATxMYzLbRco

anyone who wants to know how to deploy node js app to Cpanel this is a good source for him, this explains thoroughly how to deploy node js app to Cpanel please check this

Related