Deployment

Run Next.js on Plesk

Node.js runs smoothly on Plesk. Running the Next.js framework itself, however, comes with a few obstacles.

M
Michael Pacharzyna
5 min read
Run Next.js on Plesk

What is Plesk?

Many hosting providers give you access to Plesk when you rent web hosting from them. Plesk is a Linux and Windows based browser interface for managing domains, email addresses, and websites. You can generally run Laravel, WordPress, and other projects there without issues. Plesk also provides traffic statistics, database access through phpMyAdmin, and more.

When do you use Plesk?

Plesk is particularly useful when you want to manage multiple web spaces, databases, and domains through a graphical interface. It brings everything together in one place, which is convenient but also presents a risk: An error in the Plesk environment can leave hundreds of customers unable to manage their websites.

Personally, I prefer to keep web hosting and databases separate. If you want greater flexibility, you can use a plain server without Plesk. The complexity is considerably higher, however, making that option more suitable for experienced users.

The workaround: Hosting Next.js on Plesk

Plesk expects Node.js projects to have an entry file—a file that starts the project. Next.js does not have such a file. A small start.js file can solve this problem.

Step 1: Enable Node.js in Plesk

First, check whether the Node.js module is enabled in Plesk. Navigate to /modules/nodejs/index.php/domain/index and apply the following settings:

Enable the Node.js module in Plesk
The Node.js module in Plesk, where you configure the startup file, application root, and application mode

Step 2: Create the start.js file

The start.js file must be located in your website's root directory, usually /httpdocs. Its job is to start Next.js manually, much like running 'npx next start' in the terminal.

start.js
// Force application mode
process.env.NODE_ENV = "production";

const app = require("next/dist/cli/next-start");

app.nextStart({
  port: process.env.PORT || 3000,
});

What should you do after changing the code?

After every code change, you need to create a new build manually in Plesk and restart the application. In the Node.js module, open the “Run Node.js Commands” tab.

Plesk Node.js commands (run in this order)
# 1. Install dependencies
npm install

# 2. Create the production build
npm run build

# 3. Then click 'Restart App' in the Plesk dashboard
Run Node.js commands in Plesk
Make sure you have selected the correct Node.js version and, depending on your needs, either npm or yarn.

Common errors and how to avoid them

Most problems are caused by three common sources of error:

1. Locally installed dependencies are missing in Plesk

If packages were only installed locally and were not committed to package-lock.json, 'npm install' will fail in Plesk. Make sure all dependencies are versioned in the repository.

2. Incorrect Node.js version

The Node.js version in the Plesk dashboard and the version in the “Run Node.js Commands” tab must match. Otherwise, errors can occur.

3. Incorrect paths in the code

Paths that work locally can break on the server, especially imports that use relative paths. Use absolute imports with the @ alias consistently.