How to Set Up a Node.js App in cPanel

Node.js is a powerful runtime environment for building scalable and fast applications, especially for backend and full-stack development. cPanel offers a streamlined way to deploy Node.js applications. This guide explains how to set up, manage, and troubleshoot a Node.js application using cPanel.


1. Prerequisites

Before starting, ensure the following:

  • Your hosting plan supports Node.js applications.
  • Your app is ready with a package.json file defining the application dependencies and scripts.
  • You have SSH or cPanel access to manage the app.

2. How to Set Up a Node.js App in cPanel

Step 1: Log in to cPanel

  1. Navigate to yourdomain.com/cpanel or yourdomain.com:2083.
  2. Enter your username and password.

Step 2: Access the Setup Node.js App Tool

  1. Scroll to the Software section in the cPanel dashboard.
  2. Click on Setup Node.js App.

Step 3: Create a New Node.js Application

  1. Click the Create Application button.
  2. Fill out the required details:
    • Node.js Version: Select the version that your application requires.
    • Application Root: Specify the directory where your app files are stored (e.g., my-node-app).
    • Application URL: Choose the domain or subdomain to run your application.
    • Application Startup File: Enter the entry point file of your app (e.g., app.js or server.js).
  3. Click Create to deploy your application.

Step 4: Upload Your App Files

  1. Use the File Manager or FTP to upload your application files to the specified root directory.
  2. Ensure your package.json file is included for dependency management.

Step 5: Install Dependencies

  1. In the Setup Node.js App interface, click Enter to Virtual Environment to open a terminal.
  2. Run the following command to install dependencies:
    npm install
    

Step 6: Test Your Application

  1. In the same terminal, you can test your app locally with:
    node server.js
    
  2. If everything works correctly, restart the application using the Restart button in the Setup Node.js App interface.

3. Managing Your Node.js Application

Start, Stop, or Restart

In the Setup Node.js App section:

  • Use the Start, Stop, or Restart buttons to control your application.

Update Environment Variables

  1. In the app settings, you can define custom environment variables.
  2. Add variables like DB_HOST, API_KEY, etc., to configure your app without modifying the code.

4. Checking Logs

Logs help you monitor and debug your Node.js application:

  1. In the Setup Node.js App section, locate your application.
  2. Find links to:
    • Error Logs: Check for errors during runtime.
    • Access Logs: Monitor incoming requests and responses.

You can also create custom log files in your app using libraries like winston.


5. Common Troubleshooting Tips

App Not Running

  • Cause: Incorrect startup file.
  • Solution: Ensure the file specified in the Startup File field matches your app's entry point (e.g., server.js).

Dependencies Missing

  • Cause: npm install not run.
  • Solution: Open the virtual environment and install dependencies using npm install.

500 Internal Server Error

  • Cause: Application crashed due to code errors.
  • Solution: Check the error logs and fix any issues.

Environment Variable Issues

  • Cause: Missing or incorrect environment variables.
  • Solution: Ensure all required variables are defined in the Environment Variables section.

6. FAQs

Q1: Can I use a database with my Node.js app?

Yes, you can connect to databases like MySQL, MongoDB, or PostgreSQL. Use cPanel's MySQL Database Wizard to create a database and connect using libraries like mysql, mongoose, or pg.

Q2: How do I change the Node.js version?

  1. Stop the current application.
  2. Create a new application with the desired Node.js version.
  3. Upload the app files to the new application root directory.

Q3: Can I run multiple Node.js apps on the same domain?

Yes, by using subdomains or different paths for each application.


7. Advanced Configurations

Custom npm Scripts

Define custom scripts in your package.json to automate tasks:

"scripts": {
  "start": "node server.js",
  "test": "jest"
}

Run them using:

npm run start

Handling SSL with Node.js Apps

If your app runs on HTTPS, ensure your cPanel SSL certificates are properly configured.


Conclusion

cPanel makes it easy to deploy, manage, and troubleshoot Node.js applications. By following this guide, you can get your app up and running quickly. If you encounter issues or need further assistance, reach out to our support team.

Ha estat útil la resposta? 0 Els usuaris han Trobat Això Útil (0 Vots)