Deploy a Next.js application
Suppose you have a Next.js application that you want to deploy under the domain app.domain.com.
Prerequisites
- Server connected to FlashPanel
- Domain DNS pointed to the server
Step 1: Install Required Applications
Make sure NodeJS and PM2 are installed on the server. See the installation guide at Application Management.
WARNING
If you use pnpm or yarn as your package manager, install the corresponding application as well via the Application Management page.
Step 2: Create a Website
- Go to the server management page, click the Sites menu in the sidebar, then click the New Site button
- Enter the Domain Name (e.g.,
app.domain.com) - Enter the Proxy Port: the port your Next.js application runs on (default is
3000) - Click Create Site
Step 3: Install Source Code
Install the source code for the website from Git or upload it yourself. To open this page, go to the site management page and click the Install Source Code menu in the sidebar. See the detailed guide at Source Code Installation.
Step 4: Build the Application
Open the server terminal (via SSH or the terminal feature) and navigate to the website directory to install dependencies and build the application:
cd /home/flashpanel/app.domain.com
# Install dependencies
npm install
# Build the production application
npm run buildTIP
Replace app.domain.com with your actual domain.
Step 5: Start the Application
Option A: Using PM2 Dashboard (Recommended)
If you have installed the PM2 application, you can use the PM2 page on the server management page:
- Click the Add New button
- In Website Isolation Username, select the user that owns the website (
flashpanelby default) - Enter the command:
npm --name my-nextjs-app --cwd /home/flashpanel/app.domain.com -- start -- -p 3000 - Click Start
Option B: Using SSH
You can also start the application manually via SSH:
cd /home/flashpanel/app.domain.com
pm2 start npm --name my-nextjs-app -- start -- -p 3000WARNING
Make sure the port (3000) matches the Proxy Port you configured when creating the website.
Save PM2 Process List
To ensure your application restarts automatically after a server reboot, log in via SSH as the user that owns the website and run:
pm2 startupThis command prints a command that starts with sudo env PATH=.... Run that command as root (you only need to do this once), then save the process list:
pm2 saveStep 6: Install SSL
On the site management page, click the SSL menu and install a certificate for the domain. See SSL.
Step 7: Set Up Deployment Script (Optional)
To automate future deployments, you can configure a Deployment Script for your website. A typical Next.js deployment script looks like:
cd /home/flashpanel/app.domain.com
git pull origin main
npm install
npm run build
pm2 restart my-nextjs-appVerify the Result
- On the PM2 page, the
my-nextjs-appprocess shows theonlinestatus - Visit
https://app.domain.comand check that the pages render correctly, including images and static files
Updating the Application
When you push new code, you need to rebuild and restart:
- If you have configured a Deployment Script, simply click Deploy Now in the Deployments menu of the site management page
- If you use the PM2 page, rebuild manually, then click the Restart button of your process
- Or run via SSH:
cd /home/flashpanel/app.domain.com
git pull origin main
npm install
npm run build
pm2 restart my-nextjs-appEnvironment Variables
Next.js uses .env files for environment configuration. Create or edit the .env file in your website directory:
cd /home/flashpanel/app.domain.com
nano .envAdd your environment variables:
DATABASE_URL=mysql://user:password@localhost:3306/mydb
NEXT_PUBLIC_API_URL=https://api.domain.com
NODE_ENV=productionWARNING
After changing environment variables, you need to rebuild (npm run build) and restart the application for changes to take effect. Variables prefixed with NEXT_PUBLIC_ are embedded at build time.
Troubleshooting
Application Won't Start
- Check PM2 logs: use the View Log button on the PM2 page, or run
pm2 logs my-nextjs-app - Verify the build was successful:
npm run build - Check if the port is already in use:
sudo lsof -i :3000
502 Bad Gateway
- Verify the application is running: check the PM2 page or run
pm2 status - Make sure the application port matches the Proxy Port configured for the website
- Check application logs for startup errors
Images Not Displaying
If your Next.js images or static assets are not displaying correctly, you may need to add a custom Nginx configuration (the snippet below only works with Nginx).
- Go to the site management page
- Click the Nginx menu in the sidebar
- Click the Add Custom Config button and select In Block Server under Position
- Add the following configuration (replace
app.domain.comwith your actual domain):
location ^~ /_next/static {
alias /home/flashpanel/app.domain.com/.next/static;
}- Click Save and then click the Sync button at the top to apply the changes.