Run node.js on cpanel hosting server

Trainee Asked on October 10, 2019 in Cpanel.
Add Comment
1 Answer(s)

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


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 ^$ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$$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.

Reviewer Answered on October 10, 2019.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.