How to host a nodejs app on Cloudways

In today’s post, I am going to share another amazing guide on how you can install and run a node js application on Cloudways,

Before we start let’s see what is node js and npm. so that everyone knows what I am talking about.

NodeJS:

NodeJS is a server-side JS environment for web apps. For those who want to start writing server-side code using JS instead of PHP, Python, and Ruby, this is great news.

NodeJS Package Manager (NPM):

NPM is the dependency management tool that comes with NodeJS. NPM is to NodeJS what pip is to Python or ruby-gems is to Ruby. It makes it easy to download and manage NodeJS modules, removing a lot of hassle for the users

Node js application usually run on a specific port like 3000, 4000 or any other port that you can think of. they are not normally running on the default port of the web server (i.e. 80, 443) and they won’t be accessible like https://yourdomain.com instead they will be accessible like https://yourdomain.com:3000 in your web browser.

The challenge with Cloudways is that they have extra security on their servers and since they are offering managed cloud hosting they have firewall rules and they block all ports for an inbound connection from outside environment besides the default one that is necessary. So how you will host the node js app on the server and access it?

The answer is we are gonna reroute the traffic from default port (i.e from 80 or 443) to port on which your app is running (let’s assume port 3000 for this tutorial) using Apache mod_proxy module.

If you are confused with my statement above, then this figure below will help you in building your concept.

So are you ready to get started then let’s go.

Step 1: Install a PHP Application.

First of all, let’s install a PHP app on Cloudways, you can refer to the official guide here which explains how to add a new app on your existing Cloudways servers.

After installing a PHP app, goto the application’s public_html directory and upload your node js project, since I don’t have a node js project of my own I am gonna use a Bootstrap Admin Template named CoreUI. CoreUI is an Open Source Bootstrap Admin Template that many developers used to build an admin interface for their apps.

Step 2: Setup your NodeJS app

If you refer to the official installation guide of CoreUI which is very easy I easily deploy the app using the 3 commands mentioned below:

After the app was installed you will notice that it asks me to run the node js app using the commands below:

# serve with hot reload at localhost:3000.
$ npm run serve

You will notice that once I run this command it will start running my app on port 3000 and my node js application should run on port 3000 as you can see in the output below from the app

This is the part where you have deployed your app successfully on Cloudways but you can’t access it as port 3000 is blocked from the firewall.

Step 3: Setup mod_proxy

Kindly note that by default mode proxy is enabled on Cloudways if not you can ask Cloudways support to check it an enable it for you. once you have enabled the app simply add the below rule in the root public_html htaccess of your application.

Now after you have added the rule in your htaccess again run the app and then visit your application like https://yourdomain.com or your Cloudways default URL which will be like https://phpstack-XXXXXX-XXXXXX.cloudwaysapps.com/ you should see your node app running like below:

So in this tutorial, you have successfully learn how you can run a node js app on Cloudways, let me know in the comment section if this guide was useful for you or not.

However, I cannot figure out how to make it run automatically (including restart if the server restarts). I’ve read that PM2 is a process manager that could manage my node is but Cloudways doesn’t allow PM2.

Also in regards to making sure that pm2 starts automatically on server reboot, we suggest you to reach out to Cloudways support we think they should be able to help you in this regard. we will also try to come up with a solution and will post our update soon.