#Setup NextJS on Ubuntu server (Digital Ocean, EC2,...) Terminal Commands
#based on my YouTube video
#Recommended: An ubuntu server with at least 2 GB memory to handle npm run build
#login to server
ssh root@ip_address
#Upgrade Server - may take a few minutes
sudo apt update
sudo apt upgrade
#Install NGINX and Certbot
sudo apt install nginx certbot python3-certbot-nginx
#Allow Firewall Access
sudo ufw allow "Nginx Full"
ufw allow OpenSSH
ufw enable
#Install NPM - may take a couple of minutes
apt install npm
#install nodejs
#Option 1: install nodejs with nvm
curl -o- https:
exec $SHELL
nvm install --lts
nvm install 20 #or version of choice
nvm use 20
nvm alias default 20
#Option 2:
curl -fsSL https:
sudo apt-get install -y nodejs
#Install pm2
npm install -g pm2
#Check pm2 is working
pm2 status
#[SETUP SSH KEY ON SERVER TO ALLOW CLONE AN EXISTING REPO ONTO YOUR DROPLET]
cd /root/.ssh
ssh-keygen -t rsa -b 4096 -C "username@email.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub
#add public key to github repo settings > deploy key [or add to your profile settings > SSH so you can pull from all repos]
#go to www root
cd /var/www
#Create NextJS App or clone here
Option 1: npx create-next-app@latest name_of_app
Option2: clone with a specific branch. [You need SSH permissions from above for this].
A: git clone git@github.com:username/repo_name.git
or B (by specifying a branch): git clone -b <branch-name-optional> git@github.com:username/repo_name.git <local-name-desired-optional>
#note the name of the directory created for later to launch pm2 app
ls
#Go inside new app directory
cd name_of_app
#Install npm modules for app
npm i
#Build it
npm run build
#Create NGINX config file and edit it
cd /etc/nginx/sites-available
touch name_of_app
nano name_of_app
[SEE OTHER GIST FOR CONFIG FILE (Server Block) CONTENTS]
#https://gist.github.com/oelbaga/5019647715e68815c602ff05cff2416e#file-02-nginx-config-file-for-nextjs-site
#Syslink the file in sites-enabled
sudo ln -s /etc/nginx/sites-available/name_of_app /etc/nginx/sites-enabled/name_of_app
#make Sure NGINX file is good
nginx -t
#remove the default config files
cd /etc/nginx/sites-available
rm default
cd /etc/nginx/sites-enabled
rm default
#restart NGINX to reload config files
systemctl restart nginx
#Go to site directory and launch it with pm2
cd /var/www/name_of_app
#launch app with pm2
#Option 1:
pm2 start npm --name name_of_app -- start
#Option 2: if you need to launch on a different port. It seems this port should match what's used in the server block.
pm2 start npm --name nest -- start -- --port=3001
#save pm2 list
pm2 save
#pm2 restart on reboot
pm2 startup
#Create SSL with letsencryot
sudo certbot --nginx -d domainname.com -d www.domainname.com
#redirect site to www version
Go back in server block where the domain is managed by certbot and add www. To the redirect.
————— helpful commands ————
pm2 start npm --name name_of_app -- start (make sure you're inside the site's directory first)
systemctl restart nginx (restart NGINX)
sudo certbot --nginx -d domainname.com (Add SSL)
ps aux | awk '{print $6/1024 " MB\t\t" $11}' | sort -n
sudo killall next-render-worker-app