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:

# clone the repo
$ git clone https://github.com/coreui/coreui-free-bootstrap-admin-template.git my-project
# go into app's directory
$ cd my-project
# install app's dependencies
$ npm install

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.

# Redirect traffic to your port 3000
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)?$ http://127.0.0.1:3000/$1 [P,L]

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.

20 comments

  • Jeff says:

    I’ve been looking for exactly this article! You all are great, thank you.

  • baudouin says:

    I’m stuck at

    Cannot GET /index.php in the final step. I think the redirect worked great but then I can’t figure It out honestly. Also I had to create my own .htaccess.

    Any ideas?

    • Can you add ‘DirectoryIndex’ on top of your htaccess? your final htaccess should look similar to this:

      # Redirect traffic to your port 3000
      DirectoryIndex
      RewriteEngine On
      RewriteBase /
      RewriteRule ^(.*)?$ http://127.0.0.1:3000/$1 [P,L]

      Let me know if this helps or not.

      • Joseph says:

        I had this same problem and have been trying to fix it since last night, tried dozens of different .htaccess configurations I found on stackoverflow, chatted with cloudways support for hours. In the end, the answer was right here, all I had to do was scroll down. I have no idea what it does, but THANK YOU! I finally got wordpress running in public_html and express running in a subfolder

  • Parry says:

    Thanks, this is a helpful post. I got my node js working.

    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.

    What is the easiest way to manage my node?

    • You can surely install pm2 on your Cloudways server since we were getting a lot of requests lately in this regards so we have just published a new guide today on how you can install pm2 or other node modules on your Cloudways server, please refer to the new blog post https://www.thecloudkeeper.io/how-to-install-node-modules-on-cloudways-via-npm/ for more details.

      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.

  • Tobias says:

    Brilliant post. Any chance that it will be possible to upgrade node.js at some pointe? Several of my extension do not work with version 6. (which I must say is also a rather old version by now)

  • Will says:

    Great post!

    I have followed your steps and I have the and express api running on port 3000 using pm2, pm2 is online, the logs are clear of errors and i have created the .htaccess under public_html like so:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^(.*)?$ http://127.0.0.1:3000/$1 [P,L]

    but i cannot access the routes from the URL https://phpstack-XXXXXX-XXXXXX.cloudwaysapps.com/alive

    Is there an extra restart step I am missing? or something to change in 2020?

  • Pehon says:

    I’m getting a 500 internal server error. Not sure why, even though i followed your steps. Any suggestions? Cloudways support is out of their depth here.

  • lalit yadav says:

    Whenever I run the command “node index .js “, my site comes live, but as soon as I close the command shell, my site shows the message “503 Service Unavailable” again. what is the solution?

  • Coder Champ says:

    A really helpful article, proxy setups are usually really helpful. I did set up many applications from Laravel forge like Python, Node, React, Vue. The base is the same which is a digital ocean droplet.

    Thanks for sharing your knowledge.

  • Jerry says:

    I’m getting a 500 internal server error. I have followed all your steps. proxy_http module is also enabled on my server. Any idea why I’m getting this error?

    My .htaccess file:
    # Redirect traffic to your port 5000
    DirectoryIndex
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(.*)?$ http://127.0.0.1:5000/$1 [P,L]

  • Srimovie says:

    Really Good Information

  • Antonio says:

    Hi, I had a problem with the .htaccess
    Finally, this works:

    RewriteEngine On
    RewriteRule ^$ http://127.0.0.1:3000/ [P,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ http://127.0.0.1:3000/$1 [P,L]

    Ref: https://stackoverflow.com/a/53696477

  • Martin says:

    I have two applications: one back-end Node application and one front-end React application, which I would like to respectively run on ports 3000 and 3001. Does this mean “Step 3: Setup mod_proxy” needs to be implemented twice / for both applications? Or only for the Node back-end application?

  • Prasanna says:

    I have a Next js frontend and headless WordPress application. I have updated the .htaccess as mentioned above but the homepage displays WordPress homepage but the paths display nextjs. Is there any way to solve this?

Leave a Reply

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