Deploying a Next.js app on cPanel can be done efficiently by following these steps. Ensure your cPanel account has support for Node.js and SSH access for a smoother deployment.
Step 1: Prepare Your Next.js App
-
Build the App
Run the following command locally to create a production-ready build of your app:This will create a
.next
directory in your project. -
Export as Static (Optional)
If your Next.js app doesn't need server-side rendering (SSR), export it as a static site:This creates an
out
directory that contains all the static files.
Step 2: Zip and Upload Your App to cPanel
-
Compress Your Project
Zip your Next.js app (excludingnode_modules
and unnecessary files) to minimize upload time. -
Upload via cPanel
- Log in to your cPanel.
- Go to the File Manager.
- Upload your zipped project to the desired directory (e.g.,
public_html/your-app
).
-
Extract the Zip File
Use the File Manager to extract the uploaded ZIP file into the directory.
Step 3: Set Up Node.js Application
-
Enable Node.js
- In cPanel, go to Setup Node.js App.
- Click on Create Application.
- Choose the app root directory (where your app files are located).
- Set the environment to "Production" and specify the Node.js version compatible with your Next.js app.
-
Install Dependencies
After setting up, SSH into your cPanel account and navigate to your app's root directory:
Step 4: Configure Your App for Deployment
-
Modify
package.json
Scripts
Ensure thestart
script in yourpackage.json
usesnext start
: -
Set Environment Variables
Add necessary environment variables in your.env
file and reference them in cPanel:- Go to Setup Node.js App.
- Add your environment variables under the "Environment Variables" section.
-
Run the App
Start the app using the Node.js application manager or SSH:
Step 5: Set Up a Reverse Proxy
By default, Node.js apps won't be directly accessible via your domain. You'll need to configure a reverse proxy to route traffic from your domain to the Node.js app.
-
Create or Edit
.htaccess
Add the following lines to the.htaccess
file in thepublic_html
directory:Replace
your-app-path
with the path of your app and adjust the port if your app runs on a different port. -
Restart the App
Restart your Node.js application from the Node.js Application Manager in cPanel.
Step 6: Verify the Deployment
- Visit your domain or subdomain to ensure the Next.js app is working properly.
- Check the browser console and Network tab for errors.
- If using SSR, ensure server-side rendering is functioning as expected.
Optional: Deploy as a Static Site
If you've exported your app using npm run export
, upload the out
directory to public_html
and your app will work like a regular static site without requiring Node.js.
Troubleshooting Tips
- White Screen or Errors: Check your app logs via the Node.js Application Manager or SSH (
node app.js
ornpm start
). - Dependency Errors: Ensure all dependencies are correctly installed with
npm install
. - Performance Issues: Use static export or implement caching strategies.
Let me know if you need help with any specific step!