Hosting / Deploying Nextjs app using Nginx


1h

Get 200$ in Digital Ocean and Get 100$ in Vultr

make sure you use ubuntu server and once you logged in you can follow following commands.

Install Nginx

sudo apt install nginx

Install node using NVM

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.bashrc
nvm install node
node --version

Clone Next js sample app and setup

git clone https://github.com/fluxxset/Sample_next_js.git  /var/www/Sample_next_js
cd /var/www/Sample_next_js/sample
npm install 
npm install -g pm2
npm run build
pm2 start npm --name "sample" -- start
pm2 ls

test if app is working on port 3000

Configure Nginx

Update nano /etc/nginx/sites-available/default:

nano nano /etc/nginx/sites-available/default

Add the following:

server {
    listen 80;

    location / {
        include proxy_params;
        proxy_pass 127.0.0.1:3000
    }
}

Finally, restart and enable services:

**testing Neginx config **

sudo nginx -t 

Restart Nginx

sudo service nginx restart


Now, go to your browser, type the Domain , and you should see your Nextjs website running.