Run node.js on cpanel hosting server

bb14816 picture bb14816 · May 19, 2017 · Viewed 74.6k times · Source

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.

Answer

Matee Gojra picture Matee Gojra · Nov 23, 2018

Install/Setup NodeJS with CPanel

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

2. Download the NodeJS

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

3. Extract the NodeJS Files

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

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

mv node-v10.0.0-linux nodejs

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

mkdir ~/bin
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, 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.