imbra - How to Set Up a Next.js Application in iPanel

How to Set Up a Next.js Application in iPanel

Create a Node.js website in iPanel by following the normal website creation process.

1. Upload Your Website Files

Upload your website files using either:

2. Delete the Default index.html File

By default, iPanel creates an index.html file when a website is created.

Delete this file before running your Next.js application.

See: How to delete files or folders in iPanel File Manager

3. Check package.json

Make sure your package.json contains the following in scripts section:

"build": "next build",
"start": "next start"
4. Configure the Startup File

Go to Websites, click Manage on the website you want to configure, then open the Node.js tab. Click Runtime tab,

In the Application startup file field. enter:

server.cjs
5. Create server.cjs

Using File Manager, create a new file named:

server.cjs

See: How to create a new file in iPanel File Manager

Add the following:

const next = require('next');
const http = require('http');

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

app.prepare().then(() => {
    http.createServer((req, res) => {
        handle(req, res);
    }).listen(process.env.PORT);
});

Save the file.

6. Install and Build the Application

Go back to Node.js and open the Command runner tab.

Run:

  1. install
  2. build

Wait for each command to finish before running the next one.

7. Restart the Application

Go back to Runtime and click Restart app.

Your Next.js application should now be running.

Auto Setup

You can skip most of the manual setup by clicking Auto Setup under the Runtime tab.