How to Deploy a Node.js App with cPanel

4.7/5 - (3 votes)

Deploying a Node.js application doesn’t always require complex cloud platforms or VPS servers. Many shared hosting providers now offer Node.js support through cPanel, making it easier than ever to get your application online. In this comprehensive guide, we’ll walk through the entire process of deploying a Node.js app using cPanel’s user-friendly interface.

Prerequisites

Before we begin, ensure you have:

  • A hosting account with cPanel access
  • Access to cPanel with sufficient privileges
  • A Node.js application ready for deployment
  • Basic knowledge of Node.js and npm

Step 1: Enable Node.js Support and Install Node.js 18

Check for Node.js Support

First, log into your cPanel and look for the “Node.js App” or “Node.js Selector” icon in the Software section. If you don’t see this option, you may need to enable Node.js support or install it.

Install Node.js 18 via Node Version Manager (if available)

Many cPanel installations include Node Version Manager. Here’s how to install Node.js 18:

  1. Access Terminal in cPanel
    • Look for “Terminal” in the Advanced section of cPanel
    • Click to open the command-line interface
  2. Check Available Node.js Versions
    # Check if nvm is available
    nvm --version
    
    # List available Node.js versions
    nvm list-remote | grep v18
    
  3. Install Node.js 18
    nvm install 18
    
    nvm use 18
    nvm alias default 18
    
    # Verify installation
    node --version
    npm --version
    

Alternative: Enable Node.js via cPanel Node.js Selector

If your hosting provider offers a Node.js Selector:

  1. Access Node.js Selector
    • Look for “Node.js Selector” or “Setup Node.js App” in cPanel
    • Click on the icon
  2. Enable Node.js for Your Domain
    • Select your domain from the dropdown
    • Choose Node.js version 18.x from available versions
    • Click “Enable” or “Set up”
  3. Configure Node.js Settings
    • Set the desired Node.js version (18.x)
    • Configure any initial environment variables
    • Apply the changes

Manual Installation (Advanced Users)

If Node.js isn’t available through the above methods, you can install it manually:

  1. Download Node.js 18
    # Navigate to your home directory
    cd ~
    
    # Download Node.js 18 LTS (replace with latest 18.x version)
    wget https://nodejs.org/dist/v18.19.0/node-v18.19.0-linux-x64.tar.xz
    
    # Extract the archive
    tar -xf node-v18.19.0-linux-x64.tar.xz
    
    # Move to a permanent location
    mv node-v18.19.0-linux-x64 nodejs
    
  2. Update PATH Environment Variable
    # Add to your .bashrc or .bash_profile
    echo 'export PATH=$HOME/nodejs/bin:$PATH' >> ~/.bashrc
    source ~/.bashrc
    
    # Verify installation
    node --version
    npm --version
  1.  

Step 2: Verify Node.js Support and Configuration

After installation, verify everything is working correctly:

  1. Check Node.js Version
    node --version  # Should show v18.x.x
    npm --version   # Should show compatible npm version
    
  2. Test Node.js Functionality
    # Create a simple test file
    echo 'console.log("Node.js 18 is working!");' > test.js
    node test.js
    rm test.js
    
  3. Verify cPanel Integration
    • Return to cPanel dashboard
    • Look for “Node.js App” or similar icon
    • Ensure it recognizes your Node.js 18 installation

Step 3: Create a New Node.js Application

  1. Access the Node.js App Manager
    • Click on the “Node.js App” icon in cPanel
    • You’ll see the Node.js application management interface
  2. Create Application
    • Click the “Create Application” button
    • Fill in the required fields:
      • Node.js Version: Select version 18.x (the one you just installed)
      • Application Mode: Choose “Production” for live sites
      • Application Root: Specify the directory (e.g., nodejs_app)
      • Application URL: Set your domain or subdomain
      • Application Startup File: Usually app.js or server.js
  3. Configure Application Settings
    • Set any environment variables your app requires
    • Configure the startup file path correctly

Step 4: Upload Your Application Files

There are several ways to upload your Node.js application:

Method 1: File Manager

  1. Open cPanel’s File Manager
  2. Navigate to your application root directory
  3. Upload your application files (excluding node_modules)
  4. Extract if you uploaded a ZIP file

Method 2: FTP/SFTP

  1. Use an FTP client like FileZilla
  2. Connect to your hosting account
  3. Upload files to the application directory

Method 3: Git Repository (if supported)

  1. Some cPanel installations support Git deployment
  2. Clone your repository directly to the application directory

Step 5: Install Dependencies

  1. Return to Node.js App Manager
    • Go back to the Node.js application interface in cPanel
    • Find your created application
  2. Install npm Packages
    • Click on your application name
    • Look for “Run NPM Install” button or similar
    • Click to install all dependencies from package.json
    • Wait for the installation to complete

Alternatively, you can use the Terminal (if available):

cd /home/username/nodejs_app
npm install

Step 6: Configure Environment Variables

  1. Set Environment Variables
    • In the Node.js App Manager, click on your application
    • Look for “Environment Variables” section
    • Add necessary variables like:
      • NODE_ENV=production
      • Database connection strings
      • API keys and secrets
  2. Example Environment Variables
    NODE_ENV=production
    PORT=3000
    DATABASE_URL=your_database_connection_string
    JWT_SECRET=your_jwt_secret
    

Step 7: Update Application Configuration

Ensure your Node.js application is configured for the hosting environment:

  1. Port Configuration
    const port = process.env.PORT || 3000;
    app.listen(port, () => {
      console.log(`Server running on port ${port}`);
    });
    
  2. Database Connections
    • Update database connection strings to use environment variables
    • Ensure your database is accessible from the hosting environment
  3. Static Files
    • Configure static file serving correctly
    • Update any hardcoded paths

Step 8: Start Your Application

  1. Start the Application
    • In the Node.js App Manager, find your application
    • Click “Start” or “Restart” button
    • Check the status to ensure it’s running
  2. Monitor Application Status
    • Green status indicates the app is running
    • Red status means there’s an issue that needs fixing
  1.  

 

Leave a Comment

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

Scroll to Top