I’m attempting to deploy a NextJS app on my shared hosting server using the cPanel Setup Node.JS App section, but when I start the build – despite getting ready on http://localhost:3000
– the site throws a 503 error.
I’ve uploaded the build folder alongside the next.config.js
, package-lock.json
, package.json
and server.js
to the application root, and this is my current file structure:
next_main
build (.next folder)
node_modules
next.config.js
package-lock.json
package.json
server.js
This is my server.js
file (exactly the same as what Next provided in their custom server docs):
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 (request, response) => {
try{
const parsedURL = parse(request.url, true);
const { pathname, query } = parsedURL;
switch(pathname){
case "/a":
case "/b":
await app.render(request, response, pathname, query);
break;
default:
await handle(request, response, parsedURL);
}
} catch(error){
console.error("Error occurred.", request.url, error);
response.statusCode = 500;
response.end("Internal server error.");
}
}).listen(port, error => {
if(error) throw error;
console.log(`> Ready on http://${hostname}:${port}`);
});
}).catch(error => {
if(error) throw error;
});
Failed to load next.config.js
was also output in my stderr file, despite next.config.js
being provided.
I’ve attached the current settings I have applied in my cPanel.
Please note that I do not have root access to the terminal, and am restricted to the next_main
environment when running any NPM scripts.
2
Answers
Make sure you add all environmental variables in the .env file. add your variable here
I’m not sure if it would have solved your exact problem, but I was also getting a 503 error when attempting to setup a new cPanel Node JS app. I discovered that I needed to:
NOT specify a specific port to run on, but let the OS automatically assign an available port. Somehow cPanel is able to pick this up and send traffic to it.
Switch the Node JS version. I found out, through trial and error, that only some Node JS versions would actually function via this cPanel setup, while others would fail with a 503 error and no further error information (frustratingly). By switching versions and restarting the app, I eventually found which versions worked and which didn’t.
Be sure that you have your NPM packages installed correctly. You might need to use the button inside cPanel that says Run NPM Install
If you’re still struggling to troubleshoot after that, I would suggest creating a new Node app through the interface, which should create a basic HTTP server script for you. Test that out, and slowly apply commands and packages to the script until you discover which item breaks it.
You can also look for
stderr.log
in your application folder, although this will only show Node JS errors, and not anything from LiteSpeed/Apache or Passenger. I found that many of my 503 errors showed nothing in the log, so it may not be very helpful.