StarDomain

Running Node.js Applications on Shared Hosting

Running Node.js Applications on Shared Hosting

Node.js is a powerful JavaScript runtime that enables you to build fast, scalable web applications and APIs. All {{COMPANY_NAME}} shared hosting plans include Node.js support through CloudLinux, allowing you to run Node.js applications without needing a VPS or dedicated server. This guide covers everything you need to know about deploying and managing Node.js applications on shared hosting.

Node.js on Shared Hosting

Our shared hosting platform supports Node.js through CloudLinux's Node.js Selector, which provides:

  1. Multiple Node.js versions — choose from a range of supported versions
  2. Application management — start, stop, restart, and monitor your applications
  3. npm support — install packages and manage dependencies
  4. Passenger integration — production-ready application serving
  5. No VPS required — run Node.js applications on your shared hosting plan

Tip: You do NOT need to upgrade to a VPS to run Node.js applications. Our shared hosting includes full Node.js support through CloudLinux.

Setting Up a Node.js Application in cPanel

Step 1: Access the Node.js Selector

  1. Log in to cPanel
  2. Navigate to the Software section
  3. Click Setup Node.js App

Step 2: Create a New Application

  1. Click Create Application
  2. Configure the application settings:

- Node.js version: Select your desired version (e.g., 18.x, 20.x)

- Application mode: Choose Production for live sites or Development for testing

- Application root: The directory containing your application (e.g., myapp)

- Application URL: The URL path for your application (e.g., your domain root or a subdirectory)

- Application startup file: The entry point of your application (e.g., app.js, server.js, or index.js)

  1. Click Create

Step 3: Install Dependencies

After creating the application:

  1. Click Run NPM Install from the application management page
  2. This reads your package.json and installs all listed dependencies
  3. Wait for the installation to complete

Step 4: Start the Application

  1. Your application should start automatically after creation
  2. Use the Start App / Stop App / Restart App buttons to manage it
  3. Visit your application URL to verify it is running

Setting Up Node.js in DirectAdmin

Using the Node.js Selector

  1. Log in to DirectAdmin
  2. Go to Extra Features > Node.js Selector (or look under Advanced Features)
  3. Click Create Application
  4. Fill in the settings:

- Node.js version: Choose from available versions

- Application root: Directory path

- Application URL: Domain or subdomain

- Startup file: Entry point file

  1. Click Create
  2. Run npm install from the interface

Using SSH (Alternative Method)

  1. SSH into your hosting account
  2. Navigate to your application directory
  3. Select the Node.js version:
bash
# List available versions
nodejs_selector --list

# Use a specific version
nodejs_selector --set-version 20
  1. Install dependencies and start:
bash
npm install

Creating a Basic Node.js Application

Here is a minimal Express.js application to get started:

package.json

json
{
  "name": "my-website",
  "version": "1.0.0",
  "description": "My Node.js website",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "^4.18.0"
  }
}

app.js

javascript
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;

app.get('/', (req, res) => {
  res.send('Hello from Node.js on shared hosting!');
});

app.listen(port, () => {
  console.log(`Application running on port ${port}`);
});

Important: On shared hosting with Passenger, you do not need to specify a port. Passenger handles the port assignment. Your app should listen on the port provided in process.env.PORT.

Environment Variables

Manage environment variables for your application:

Through the Control Panel

  1. Go to the Node.js application management page
  2. Look for the Environment Variables section
  3. Add key-value pairs (e.g., DATABASE_URL, API_KEY)
  4. Click Save and restart the application

Using a .env File

  1. Create a .env file in your application root:
DATABASE_URL=mysql://user:pass@localhost/dbname
API_KEY=your-api-key-here
NODE_ENV=production
  1. Use the dotenv package in your application:
javascript
require('dotenv').config();
console.log(process.env.DATABASE_URL);
  1. Add .env to your .gitignore to keep secrets out of version control

Managing npm Packages

Installing Packages via Control Panel

  1. Edit your package.json to add dependencies
  2. Click Run NPM Install in the application management page

Installing Packages via SSH

bash
cd ~/myapp
npm install package-name --save

Updating Packages

bash
npm update

Troubleshooting Node.js Applications

Application Not Starting

  1. Check the application log file for errors
  2. Verify the startup file path is correct
  3. Ensure all dependencies are installed (npm install)
  4. Check for syntax errors in your code
  5. Verify the Node.js version is compatible with your application

503 Service Unavailable

  • The application may have crashed. Check logs and restart.
  • Ensure your application does not exceed memory limits on shared hosting.
  • Verify the application is listening on the correct port.

Module Not Found Errors

  • Run npm install to ensure all dependencies are present
  • Check that the module is listed in package.json
  • Verify the correct Node.js version is selected

Application Running Slowly

  • Shared hosting has resource limits. Optimize your application for memory and CPU usage.
  • Use caching where possible (e.g., Redis, in-memory caching)
  • Minimize the number of npm packages
  • Consider using a build step to bundle your application

Changes Not Reflecting

  • Restart the application after making code changes
  • Clear any caching your application uses
  • Check that you are editing files in the correct directory

Best Practices for Node.js on Shared Hosting

  1. Use production mode: Set NODE_ENV=production for better performance
  2. Handle errors gracefully: Uncaught exceptions can crash your application
  3. Monitor memory usage: Shared hosting has memory limits; keep your application lean
  4. Use process.env.PORT: Let the hosting environment assign the port
  5. Keep dependencies minimal: Only install packages you actually need
  6. Use a .gitignore: Exclude node_modules and .env from version control
  7. Test locally first: Always test changes on your local machine before deploying
  8. Use logging: Implement proper logging to diagnose issues quickly

Limitations on Shared Hosting

Be aware of these limitations:

  • Memory limits: Applications must stay within your plan's memory allocation
  • CPU limits: Long-running CPU-intensive tasks may be throttled
  • No root access: You cannot install system-level packages
  • Port restrictions: You cannot bind to arbitrary ports; use the assigned port
  • Background processes: Long-running background jobs may be killed

For applications that exceed these limits, consider upgrading to a VPS plan for dedicated resources.

  • Running Python Applications on Shared Hosting
  • How to Access Your Account via SSH
  • Deploying Your Website with Git

Need help with your Node.js application? Contact our support team at {{SUPPORT_EMAIL}} or visit {{SUPPORT_URL}}.