This article explains how to prepare and migrate a Next.js application so it can run using the Node.js Selector in cPanel. The procedure covers project preparation, packaging, and application setup inside cPanel.
Scope and requirements
- Node.js Selector must be available on the hosting account
- The application must use a custom Node.js server
- Next.js requires Node.js version 18.17 or newer
This workflow applies to application hosting environments such as cPanel hosting, and may require adjustments depending on the application structure.
Preparing a Next.js project for migration
If you already have an existing Next.js project, start from step 5.
Creating a sample project
- Make sure Node.js and npm are installed locally.
- Open a terminal and create a new project:
npx create-next-app@latest
- Name the project
nextappand accept the default options.
Creating a custom server
Next.js must run with a custom server to work with the Node.js Selector.
- In the project root, create a file named
server.js. - Add the following code:
const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const hostname = 'localhost'
const port = 3000
const app = next({ dev, hostname, port })
const handle = app.getRequestHandler()
app.prepare().then(() => {
createServer(async (req, res) => {
try {
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl
if (pathname === '/a') {
await app.render(req, res, '/a', query)
} else if (pathname === '/b') {
await app.render(req, res, '/b', query)
} else {
await handle(req, res, parsedUrl)
}
} catch (err) {
res.statusCode = 500
res.end('internal server error')
}
}).listen(port)
})
- Save the file.
Updating the start script
- Open
package.json. - Replace the
startscript with the following:
"start": "NODE_ENV=production node server.js"
- Save the file.
Building and packaging the project
- Build the application:
npm run build
- Create a ZIP archive, excluding unnecessary files:
zip -r ../nextapp.zip . --exclude "node_modules/*" ".git/*" .gitignore README.md
The archive will be created in the parent directory.
Uploading files to cPanel
- Connectez-vous à cPanel.
- Open File Manager.
- Upload
nextapp.zipto your account. - Create a directory named
nextappinsidepublic_html. - Extract the ZIP file into
public_html/nextapp.
Creating the Node.js application
- On the Outils page, faire un clic Setup Node.js App.
- Faire un clic Create Application.
- Select a Node.js version compatible with Next.js.
- Set Application mode to Production.
- Set Application root à
nextapp. - Select your domain and leave the path empty.
- Set Application startup file à
server.js. - Faire un clic Create.
Installing dependencies and starting the app
- Faire un clic Stop App if it is running.
- Faire un clic Run NPM Install.
- After installation completes, faire un clic Start App.
If the NPM install option is unavailable, reload the Node.js Selector page to refresh the application list.
Verifying deployment
Open your domain in a browser. The Next.js application should load successfully.
Hosting considerations
Node.js applications with higher traffic or scaling requirements may perform better on serveurs cloud. For workloads that require full system level control, un dedicated server provides maximum flexibility for Node.js runtime tuning.