How to deploy Express.JS web application on HawkHost with cPanel?

Here is a step by step guide based on my experience on how to deploy your Express.JS web application with cPanel on HawkHost servers. First, the versions I am using are:

  • cPanel v90.0.16
  • Express.JS v4.16.0
  • Node.JS v12.16.1
  • NPM v6.14.4

Now it’s time to login to your cPanel, and do the following

Upload your application folder to the server: You can do as you prefer but I use FileZilla for this, it’s more convenient than cPanel’s File Manager.

Go to Subdomains:

How_to_deploy_Express_JS_web_application_on_HawkHost_with_cPanel_1

Create a subdomain to access your Node.JS application:

How_to_deploy_Express_JS_web_application_on_HawkHost_with_cPanel_2-2

How_to_deploy_Express_JS_web_application_on_HawkHost_with_cPanel_3

Go to Setup Node.js App on cPanel:

How_to_deploy_Express_JS_web_application_on_HawkHost_with_cPanel_4

Click on Create Application button:

How_to_deploy_Express_JS_web_application_on_HawkHost_with_cPanel_5

Fill in the blanks with the right information and click create: 

How_to_deploy_Express_JS_web_application_on_HawkHost_with_cPanel_6-2

You will see a green message saying that your app was created. Now you are supposed to access application via the its URL. Except that NO! you will get HTTP 503 error.

How_to_deploy_Express_JS_web_application_on_HawkHost_with_cPanel_7

Before continuing, stop the application now!

The question is, why is this happening?

There are several reasons for this error to appear and here I address the ones that caused me that problem and/or the ones that stopped me from deploying the application successfully.

For normal applications without express you may not have a problem but if using express.js 4+ then you will get 503 error.

The problem is that in bin/www file there are some lines causing havoc. One of them is:

server.on('listening', onListening);

If you check the events form the class http.server there is no ‘listening’ event. But the mentioned class extends from net.server and that has a listening event.

So I just commented that line out:

// server.on('listening', onListening);

Of course I also commented the function ‘onListening()’ and everything worked perfectly, this function just prints a debug message specifying the port where the server is actually listening for connections so it’s not really relevant but for some reason (that I currently ignore) it confuses Phusion Passenger and then nothing starts. You end up with error HTTP 503 instead.
/**
 * Event listener for HTTP server "listening" event.
 */

// function onListening() {
//   var addr = server.address();
//   var bind = typeof addr === 'string' ?
//     'pipe ' + addr :
//     'port ' + addr.port;
//   debug('Listening on ' + bind);
// }

While you are in that file (if you don’t need to debug) I would comment this line out as well:

// var debug = require('debug')('web-express-template:server');

IMPORTANT: The other problem is the node_modules folder. Delete your node_modules folder as well because the application will not run.

After all this is done go to the application and click Run NPM Install:

How_to_deploy_Express_JS_web_application_on_HawkHost_with_cPanel_8

Start the application:

How_to_deploy_Express_JS_web_application_on_HawkHost_with_cPanel_9

Now you can access the URL and you would expect to see your application running perfectly! But instead it doesn’t work still !

So Destroy the app, delete the folder on your server and the re-upload without the node_modules folder and create the application again!

Now it should work!

If you have a problem with any file that you are reading from the code just wait a little, maybe Run NPM Install, I don’t know this thing is confusing, unpredictable, unstable, undocumented, and stressful.

I saw a lot of workarounds where you just create server.js and call http.createserver(app).listen(port) but they don’t work for this case. I really wanted to know more about the root cause of the problem. No need to create server.js or anything like that.

I just write down what worked for me. Hope it helps.

Sources

  • https://nodejs.org/api/http.html#http_class_http_server
  • https://nodejs.org/api/net.html#net_class_net_server
  • https://www.version-next.com/how-to-install-setup-node-js-with-cpanel/
  • https://support.hawkhost.com/index.php?/Knowledgebase/Article/View/197/0/how-to-create-a-nodejs-application
  • https://support.hawkhost.com/index.php?/Knowledgebase/Article/View/189
  • https://www.namecheap.com/support/knowledgebase/article.aspx/10047/2182/how-to-work-with-nodejs-app
  • https://github.com/phusion/passenger/wiki/Phusion-Passenger%3A-Node.js-tutorial
  • https://www.phusionpassenger.com/library/walkthroughs/deploy/nodejs/ownserver/apache/enterprise/stretch/deploy_app.html
  • https://www.phusionpassenger.com/library/walkthroughs/deploy/nodejs/

Leave a Comment

Your email address will not be published. Required fields are marked *