Knowledgebase: PHP / Programming
How to Create a Node.JS Application
Posted by Tony B., Last modified by Tony B. on 29 December 2017 01:37 PM
At Hawk Host it is possible to create persistent Node.JS applications utilizing several of our built in features.  We will walk you through how to create a node.js hello world but the same can be applied to applications such as Ghost

1) You'll need to create a nodejs folder within your accounts home directory which can be completed by FTP/SFTP or throuhg the cPanel file manager.  After this is created you will create an app.js file inside your nodejs folder with the following contents:

var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World\n');

console.log('Server started');

Replace 12345 with a randomly generated 5 digit number and record this as it'll be necessary later on.

2)  You now need to create a PHP file which will be used to manage your application.  Create a cron.php file with the following contents:


$host = '';
$port = '12345';
$checkconn = @fsockopen($host, $port, $errno, $errstr, 5);
     exec('/usr/bin/node /home/'.get_current_user().'/nodejs/app.js > /dev/null 2>&1 &');

Change the 12345 to contain the port number you randomly generated previously.

3)  You will now need to login to cPanel and open the cron jobs option

You will need to create a cron job with the following settings:

The cronjob command is

/usr/local/bin/php /home/username/nodejs/cron.php > /dev/null 2>&1

Replace username with your accounts username.

4) The final step is to make it possible for the web server to connect to your Node.JS application.  Navigate to where you wish for your application to load from which in our case will be the public_html folder.  Create a .htaccess file with the following contents:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$$1 [P,L]

Replace 12345 with your random port you generated and upon visiting the location your Node.JS application should now load.
(2 vote(s))
Not helpful

Comments (0)
Post a new comment
Full Name:
CAPTCHA Verification 
Please enter the text you see in the image into the textbox below (we use this to prevent automated submissions).