Deploying An Express API On CPanel

Table of contents

Deploying an Express API on cPanel can be smooth if you're familiar with both cPanel and Node.js environments. cPanel, primarily known for hosting PHP-based applications, also supports Node.js, allowing you to run server-side JavaScript applications like an Express API. In this guide, we'll walk you through the steps needed to get your Express API up and running on cPanel, covering the configuration of your environment, managing dependencies, and resolving common issues that may arise during deployment. Whether you're migrating an existing API or starting from scratch, this guide will provide you with the knowledge to deploy your Express API efficiently. You can read this first before reading this article. https://cpanel.net/blog/tips-and-tricks/how-to-host-a-node-js-application-with-cpanel/

Steps to Deploy Your Express API

  1. Access Your cPanel Account:

    • Log in to your cPanel account using the credentials provided by your hosting provider.
  2. Set Up Node.js Environment:

    • In cPanel, locate the “Setup Node.js App” or similar option (this might vary depending on your hosting provider). Note you need to have a domain where your express API will point to.

    • Create a new application. You’ll be asked to select the Node.js version and set the application root, startup file (usually app.js or index.js), and the app URL.

    • Click “Create” to set up the environment.

  3. Upload Your Express API Files:

    • Use the File Manager in cPanel to upload your project files.

    • Alternatively, you can upload your files via FTP or directly from your version control system (like Git).

    • Ensure that all the necessary files, including node_modules, are either uploaded or can be installed on the server.

  4. Install Dependencies:

    • After uploading your files, go to the Terminal or Command Line Interface in cPanel. If you don't have a file, you can use this https://github.com/Adedoyin-Emmanuel/server-test fork and download the source code.

    • Navigate to your application’s directory and run npm install to install all the dependencies listed in your package.json file.

  5. Configure Environment Variables (if necessary):

    • Set up any environment variables your application requires by using the “Environment Variables” section within the Node.js setup in cPanel.
  6. Change Default Port Config

    • This is the takeaway from this article. You want to ensure your express API is not listening on any port.

    •   app.listen(() => {
          console.log(`API STARTED SUCCESSFULLY`);
        });
      
        // or this
      
        app.listen();
      

      You can make these changes on the Cpanel File Manager or even before uploading your files. This step is very important.

After this, you can go back to the NodeJS application and restart the server. Now visit the domain or subdomain that you provided when creating the express API. You should be able to access your endpoints and all.

Something like this

Conclusion.

I know this article might not be the most impressive one you've read. I had to write it quickly because I'm working on my project, and the launch date is almost here. More info about that later. This problem made me spend my entire day with customer support, so I thought of writing it to help others. I should have added pictures and more details. That's why I recommend you read this article first: https://cpanel.net/blog/tips-and-tricks/how-to-host-a-node-js-application-with-cpanel/. It will help you understand how cPanel works. The key takeaway from this article is that you should not specify a port for your Express API to listen to. I hope this article helps. This video was inspired.by https://www.youtube.com/watch?v=qGvteC3dZnk."

1
Subscribe to my newsletter

Read articles from Adedoyin Emmanuel Adeniyi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Adedoyin Emmanuel Adeniyi
Adedoyin Emmanuel Adeniyi

I love building open-source projects. In my free time, I play CODM.