Here’s a detailed guide on How to Set Up a Node.js App in cPanel:
How to Set Up a Node.js App in cPanel
cPanel provides a feature that allows you to host and manage Node.js applications. This guide will walk you through the steps to set up a Node.js app using the Application Manager in cPanel.
Prerequisites
Before setting up a Node.js app, make sure you have:
- A hosting account that supports Node.js.
- Access to cPanel.
- The app's codebase ready for deployment (e.g., your app files in a
.zip
format or accessible via Git).
Step 1: Log into cPanel
- Open your web browser and go to your cPanel login page (usually
yourdomain.com/cpanel
). - Enter your username and password to log in.
Step 2: Access Application Manager
- Once logged in, scroll down to the Software section in cPanel.
- Click on Application Manager.
Step 3: Create a New Application
- In the Application Manager, click the Create Application button located near the top-right corner.
- You will be presented with the configuration options for your Node.js app.
Step 4: Configure Node.js App
Fill in the following fields to configure your Node.js app:
-
Node.js Version: Select the version of Node.js you want to use from the dropdown menu. Ensure this version matches the version required by your app.
-
Application Mode: Choose between Production or Development mode. Use Production for a live app and Development for testing.
-
Application Root: Enter the directory where your Node.js app files are located. This directory must exist in your hosting file system, such as
public_html/yourapp
. -
Application URL: Enter the URL where you want the app to be accessible, such as
https://yourdomain.com/yourapp
. -
Application Startup File: Enter the filename of your app’s main startup file (typically
app.js
,index.js
, or as per your codebase).
Step 5: Upload App Files
You can upload your app files to the Application Root directory through File Manager or FTP:
- Navigate to File Manager in cPanel.
- Go to the directory you specified as the Application Root.
- Upload the necessary app files and folders (you can upload them in a
.zip
format and extract them, or use FTP to transfer the files).
Step 6: Install Node.js Modules (Dependencies)
- Once the files are uploaded, return to the Application Manager.
- Under the Actions section of your Node.js app, click on NPM Install. This will install all the necessary dependencies listed in your
package.json
file.- Make sure the package.json file is present in the Application Root directory.
Step 7: Restart the Node.js Application
- After installing the dependencies, go back to Application Manager.
- Under your app's Actions section, click on Restart to restart your Node.js app and apply the changes.
Step 8: Test the Application
Once the app is set up and running:
- Open a web browser and visit the Application URL you specified.
- You should now see your Node.js app running. If not, double-check the configuration, file paths, and startup script.
Optional: Setup Environment Variables
You can set environment variables for your app:
- In Application Manager, find your Node.js app and click on the Edit button.
- Scroll down to the Environment Variables section.
- Add any required variables by clicking Add Variable (e.g.,
PORT
,DB_URL
, etc.). - Click Save after adding the variables.
Troubleshooting Tips
- Startup File Issue: If the app isn’t starting, ensure the correct startup file is defined and located in the Application Root.
- Port Conflicts: Most hosting providers do not allow custom port numbers, so your app might need to run on port 80 (for HTTP) or port 443 (for HTTPS).
- Node.js Version: Ensure that the Node.js version you select matches the version required by your app.
Conclusion
By following these steps, you can successfully set up and run a Node.js application on cPanel using the Application Manager. This allows you to deploy your Node.js app in a shared hosting environment and manage it efficiently through cPanel.