Deploy nextjs in nginx with PM2

Step by Step

In next.config.mjs comment // output: 'export'

$ npm run build

 In next.config.mjs Uncomment // output: 'export'

$ npm run build

Move nextjs directory to /var/www/ in VPS without node_modules (It's Big files)

$ npm i

$ cd /etc/nginx/sites-available

server {

    listen 80;

    server_name proj.consultechbd.com;  # Replace with your actual domain name

    location / {

        #root   /var/www/nextjs-paikari/out;

        #index  index.html;

        proxy_pass http://localhost:3000;

        proxy_http_version 1.1;

        proxy_set_header Upgrade $http_upgrade;

        proxy_set_header Connection $connection_upgrade;

        proxy_set_header Host $host;

        proxy_cache_bypass $http_upgrade;

    }

    location /_next/static/ {

        alias /var/www/nextjs-paikari/out/_next/static/;

        #expires 1d;

        access_log off;

    }

    location /static/ {

        alias /var/www/nextjs-paikari/out/static/;

        #expires 1d;

        access_log off;

    }

}


Install PM2 globally:

$ sudo npm install -g pm2

cd /nextjs-directory

$ pm2 start "npm start"

Configure PM2 to Start on Boot

$ pm2 startup systemd

$ pm2 save


Reference:

1) https://www.dhiwise.com/post/how-to-host-your-nextjs-nginx-application-efficiently

2) https://www.youtube.com/watch?v=4ODoUevt5u8



Comments

Popular posts from this blog

Generate SHA-1 and SHA-256 for Debug and Release android app

JAVA_HOME setup and Integrate with Android Studio Project in Ubuntu

Transfer file to Server and vice versa