Setting Up a Server-Side Rendering (SSR) Next.js Application on cPanel

When building a Next.js application that requires Server-Side Rendering (SSR), you cannot use the next export (Static Export) approach. Instead, you must run a persistent Node.js server process on your hosting environment.

On cPanel, Node.js applications are managed using Phusion Passenger, which acts as a bridge between the Apache web server and your Node.js app. To make this work with Next.js, we need a specific setup and file structure.

This guide walks you through the complete process.


Prerequisites

  • A cPanel hosting account with Node.js support enabled
  • Node.js v18.x or v20.x
  • A locally working Next.js project
  • SSH or File Manager access to cPanel

Step 1: Create the Application Entry Point (server.js)

Next.js normally starts its own internal server using next start, but cPanel requires a custom entry file it can hook into via Phusion Passenger.

In the root of your Next.js project, create a file called server.js:

const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')

const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

const port = process.env.PORT || 8080 // 8080 is recommended on cPanel

app.prepare().then(() => {
  createServer((req, res) => {
    const parsedUrl = parse(req.url, true)
    handle(req, res, parsedUrl)
  }).listen(port, (err) => {
    if (err) throw err
    console.log(`> Ready on http://localhost:${port}`)
  })
})

Why this is needed

  • cPanel expects a Node.js startup file
  • This script manually boots Next.js and forwards all requests to it
  • Phusion Passenger then proxies Apache traffic to this server

Step 2: Build the Application Locally

On your local machine, run:

npm run build

This generates the hidden .next directory, which contains the compiled SSR logic required to run the app in production.

⚠️ Important: Do not run next export. SSR requires the .next folder.


Step 3: Prepare Files for Upload

Create a ZIP archive containing only the required production files:

Required Files & Folders

.next/               (folder)
public/              (folder)
package.json
next.config.js
server.js

💡 You do not need:

  • node_modules
  • .env.local
  • Development files

Step 4: Upload to cPanel

  1. Open cPanel → File Manager
  2. Create a new folder outside public_html for security
    Example:/apps/my-next-app
  3. Upload your ZIP file into this folder
  4. Extract the ZIP contents

Step 5: Register the Application in cPanel

  1. Open Setup Node.js App in cPanel
  2. Click Create Application
  3. Configure as follows:
SettingValue
Node.js version20.x or 24.x
Application modeProduction
Application rootapps/my-next-app
Application URLSelect your domain
Application startup fileserver.js
  1. Click Create

Step 6: Install Dependencies

After the app is created:

  1. Scroll to Detected Configuration Files
  2. Click Run NPM Install

This installs all production dependencies from package.json.


Step 7: Start or Restart the Application

At the top of the Setup Node.js App page:

  • Click Restart Application

If everything is configured correctly, your Next.js SSR app will now be live on your domain.


Common Issues & Tips

Port Conflicts

  • Always use process.env.PORT
  • Avoid hardcoding ports like 3000

Environment Variables

  • Set environment variables directly in Setup Node.js App
  • Do not rely on .env.local files

File Permissions

  • Ensure folders are readable by the Node.js process

Conclusion

Running a Server-Side Rendered Next.js application on cPanel is fully supported, but it requires a custom server.js, a production build, and proper integration with Phusion Passenger. Once configured, you get full SSR benefits while still using shared hosting.


Happy deployments!

Posted in CPanel, Knowledgebase, Web and Email Hosting

Dzinaishe Mpini