Blog
Stay updated with our new news
How to Build and Host a Node.js App on cPanel: A Complete Guide
Hosting Node.js directly inside your shared cPanel account relies on two essential technologies working seamlessly together: the CloudLinux Node.js Selector and Phusion Passenger. The Node.js Selector is the interface within cPanel that creates a secure, isolated virtual environment for your project, allowing you to easily manage Node versions and install dependencies.
Once that environment is set up, Phusion Passenger acts as the engine. It connects the application to the web server, dynamically assigns ports, and handles the background processes—waking the app up when visitors arrive and putting it to sleep when idle to save resources.
In this guide, we will use these built-in tools to build a sample Express.js application from scratch, deploy it to a subdomain, and learn how to debug it like a pro using the cPanel Terminal.
Prerequisites
Before diving into the code, let’s ensure we have:
- An active shared hosting account with Libyan Spider.
- A subdomain ready to go (e.g., node-js.yourdomain.ly). We can create this in the “Domains” section of cPanel.
- Terminal/SSH Access Enabled. We will be using the command line for some powerful debugging. If you haven’t enabled this yet, follow this quick guide: How to Enable/Disable Shell (SSH) Access for cPanel Account.
Step 1: Allocate the Node.js Environment
First, we need to instruct the server to carve out a secure, virtualized environment for our app.
- Log in to cPanel dashboard.
- Scroll down to the Software section and click on Setup Node.js App.
- Click the blue Create Application button.
- Fill out the application details:
- Node.js version: Select a modern version (e.g., 18.x or 20.x).
- Application mode: Choose Development.
- Application root: Type nodeapp. This creates a folder to hold your code safely outside of public web access.
- Application URL: Select your subdomain (e.g., node-js.yourdomain.ly).
- Application startup file: Type app.js.
- Passenger log file: Type /home/yourusername/nodeapp/passenger.log (replace “yourusername” with your actual cPanel username).
- Click Create.
Step 2: Understand Your Folder Structure
Once created, cPanel generates a few folders in your File Manager. It is critical to know what they do:
- nodeapp: This is where our code lives. All the backend files and package.json go here.
- node-js.yourdomain.ly: This is the public document root. Because Phusion Passenger routes traffic directly to our backend, we do not need to put our Node files here. Let’s leave it empty.
- nodevenv: This is the virtual environment created by CloudLinux. We should not upload files or edit anything here, or our application might break.
Let’s open the File Manager, navigate to the nodeapp folder, and delete the default app.js file that cPanel automatically created. We will write our own.
Step 3: Write the Code (Express.js App)
As mentioned earlier, to demonstrate how this environment works, we will be building a sample application using the Express.js framework.
Inside the nodeapp folder, let’s click the + File button at the top of the File Manager to create two new files.
1. package.json
Let’s create this file, right-click it, select Edit, and paste the following. This tells the server our sample app needs the Express framework:
2. app.js
Let’s create the second file, edit it, and paste the following code.
CRITICAL RULE: Because cPanel uses Phusion Passenger, we shouldn’t hardcode a port such as 8080 or 3000 for the server to listen on. We must use process.env.PORT so Passenger can assign a dynamic port or socket on the fly. This applies to all Node.js applications hosted in this environment!
Step 4: Install Dependencies
Finally, our code is ready, but we still need to install the Express package defined in the package.json.
- Let’s go back to the Setup Node.js App screen.
- Click the Pencil Icon (Edit) next to the app.
- Scroll down to Detected configuration files and click Run NPM Install.
Pro-Tip: The Terminal Workaround
Occasionally, cPanel takes a moment to recognize new files, and the Run NPM Install button might be greyed out. We can bypass the UI entirely using the Terminal:
- At the top of the Node.js setup page, copy the Command for entering to virtual environment (it looks like source/home/username/nodevenv/…).
- Go to the cPanel dashboard, open the Terminal, paste the command, and press Enter.
- Type npm install and hit Enter. The terminal will download the required packages instantly!
- Finally, let’s go back to the Node UI and click Restart.
Our app is now live! Visit the subdomain in your browser to see it in action.
Phase 5: Debugging Like a Pro (Terminal Superpowers)
When our app grows, we might encounter the dreaded “503 Service Unavailable” error. Instead of guessing what went wrong, we can use your cPanel Terminal to diagnose the exact issue.
Enter the virtual environment using the source command provided in the cPanel Node UI. Once inside, we have full control.
1. Why doesn’t pm2 work here?
If you are coming from a VPS background, your first instinct might be to type pm2 start app.js. If you do this in cPanel, you will get a command not found error.
Why? Because Phusion Passenger is the process manager. It entirely replaces the need for PM2. Passenger handles the background running, waking, and sleeping of the app automatically.
2. Read the Crash Logs
Did the app crash? We can read the log file defined in Step 1 to see exactly which line of code failed.
If you forget to run npm install, this log will clearly show a Cannot find module ‘express’ error!
3. Verify The Environment
We can use the following commands to make sure CloudLinux is running the correct Node version we selected
4. Inspect Environment Variables
If you added API keys or Database passwords inside the cPanel Node interface, we can verify that the app can actually “see” them:
Or, to see exactly how Node.js reads them:
This will print out a large list of variables, including the NODE_ENV (development/production) and proof that the code is safely sandboxed inside the CL_VIRTUAL_ENV.
Hosting Node.js on shared hosting doesn’t mean flying blind. By understanding the folder structure, using process.env.PORT, and leveraging the terminal for NPM installs and log reading, we can confidently run powerful web applications directly on Libyan Spider hosting account!
Share:
More Articles
From Encryption to Extortion: Inside the Devastating World of Ransomware
Why “Clever” Passwords Are Your Biggest Security Risk.
When the First Step Becomes Easier
Leave a Reply