next.jsdeploymentcyber-panel

How to deploy a next.js app to Cyberpanel


I am trying to deploy a full next js app in Cyberpanel

So i am trying to deploy a next js app to CyberPanel, and after following the only documentation they have on this (https://community.cyberpanel.net/t/how-to-host-nextjs-app-on-cyberpanel/44403/2) it did not work, the webiste is still showing me the 404 page, while I'm running the app (using pm2). The only thing i didnt do is that a ran it on the default port (3000).


Solution

  • I've tried so many things and here is the only way that worked for me.

    1. Create the handler

    2. Redirect the requests

    (from "http://127.0.0.1:<CUSTOM_PORT>" to the domain/sub-domain)

    context / {
      type                    proxy
      handler                 <HANDLER_NAME>
      addDefaultCharset       off
    }
    
    // The <HANDLER_NAME> should be the name that
    // we gave to our handler, for example if we named it nextjs:
    
    context / {
      type                    proxy
      handler                 nextjs
      addDefaultCharset       off
    }
    

    3. Allow the port to be used

    I don't know if this is the case for everyone, but for me it only worked after I've done this. Try not doing it, if it doesn't work come back to it.
    By default, the port is not allowed to be used.
    Access your server by SSH and execute this command:

    sudo iptables -A INPUT -p tcp --dport <PORT> -j ACCEPT
    
    // If we used the port 11300:
    
    sudo iptables -A INPUT -p tcp --dport 11300 -j ACCEPT
    

    4. Start your app

    Now go to the SSH and start your app on the custom port that we have set.
    (In NextJS, you can either change the start script in package.json to next start -p <PORT>, meaning next start -p 11300 and then run it normally with npm start, or you can run directy the command npm start -- -p 11300).

    Run it normally with npm to test if everything is working fine.
    Then you can start it with pm2 with pm2 start "npm start -- -p 11300".

    PS: This works for any Nodejs app not only NextJS.

    I hope you find this helpful.