How to run a Node.js app in cPanel

  1. Open "Setup Node.js App" (group "Software").
  2. Click "CREATE APPLICATION".
Node.js applications
Node.js applications
  1. "Node.js version" - pick a recent LTS version.
  2. "Application mode" - Production for a live site, Development for debugging.
  3. "Application root" - the code folder, for example nodeapp. Do not put the code in public_html.
  4. "Application URL" - the domain and path where the app opens. An empty path means the whole domain.
  5. "Application startup file" - the main file, for example app.js.
  6. Click "CREATE".
New Node.js application
New Node.js application

The panel creates the folder, a virtual environment and a test app.js. Upload your code and package.json to the folder.

  1. Open the app in the list (pencil icon).
  2. Click "Run NPM Install" to install dependencies from package.json.
  3. Click "RESTART".
Managing the application
Managing the application

The app must not listen on port 80 or 3000 itself: the web server starts it through Passenger. Use the port from process.env.PORT - in Express that is app.listen(process.env.PORT).

const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('Hello from Node.js'));
app.listen(process.env.PORT);

Click "RESTART" after every code change. Environment variables (API keys and so on) are set under "Environment variables" on the same page.

Questions and answers
The port from process.env.PORT - the web server provides it.
Click "RESTART" in the app card.