How to Deploy a Next.js App on cPanel efficiently

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

  1. Build the App
    Run the following command locally to create a production-ready build of your app:

    bash
    npm run build

    This will create a .next directory in your project.

  2. Export as Static (Optional)
    If your Next.js app doesn't need server-side rendering (SSR), export it as a static site:

    bash
    npm run export

    This creates an out directory that contains all the static files.


Step 2: Zip and Upload Your App to cPanel

  1. Compress Your Project
    Zip your Next.js app (excluding node_modules and unnecessary files) to minimize upload time.

  2. 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).
  3. Extract the Zip File
    Use the File Manager to extract the uploaded ZIP file into the directory.


Step 3: Set Up Node.js Application

  1. 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.
  2. Install Dependencies
    After setting up, SSH into your cPanel account and navigate to your app's root directory:

    bash
    cd path/to/your-app npm install

Step 4: Configure Your App for Deployment

  1. Modify package.json Scripts
    Ensure the start script in your package.json uses next start:

    json
    "scripts": { "start": "next start" }
  2. 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.
  3. Run the App
    Start the app using the Node.js application manager or SSH:

    bash
    npm start

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.

  1. Create or Edit .htaccess
    Add the following lines to the .htaccess file in the public_html directory:

    apache
    RewriteEngine On RewriteCond %{REQUEST_URI} ^/(your-app-path) RewriteRule ^(.*)$ http://127.0.0.1:3000/$1 [P,L]

    Replace your-app-path with the path of your app and adjust the port if your app runs on a different port.

  2. 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 or npm 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!

  • 1 Benutzer fanden dies hilfreich
War diese Antwort hilfreich?

Verwandte Artikel

How to Fix Error: No specific error was returned with the failed API call

How to Fix Error: No specific error was returned with the failed API call |FIXED| Today in this...

How to Install SSL in cPanel

In this tutorial, you will learn how to install SSL using Let’s Encrypt SSL utility in Cpanel for...

Redirect HTTP Requests to HTTPS

.htaccess 101 – Redirect HTTP Requests to HTTPS Once you have purchased and installed an...