Home

How I deployed my Next.js app to DigitalOcean with Namecheap domain

Prerequisites buy digitalocean droplet, namehcheap domain, push a next app to github, connect namecheap domain with digitalocean droplet

Access to a digiatlcoean droplet

ssh root@<IP_ADDRESS> -i keys/key_rsa

Install nginx, nodejs, pm2, certbot, python3-certbot-nginx

sudo apt install -y nodejs npm nginx
sudo apt install certbot python3-certbot-nginx
sudo npm install -g pm2

Pull an app from github

git clone <GITHUB_URL>

Build the app

npm run build

To Setup https I used UFW, or Uncomplicated Firewall.

sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status

Satus command returns

Status: active

To                         Action      From
--                         ------      ----
Nginx HTTPS                ALLOW       Anywhere                  
OpenSSH                    ALLOW       Anywhere                  
Nginx Full                 ALLOW       Anywhere                  
Nginx HTTPS (v6)           ALLOW       Anywhere (v6)             
OpenSSH (v6)               ALLOW       Anywhere (v6)             
Nginx Full (v6)            ALLOW       Anywhere (v6)  

Setup NGINX, open nginx config

sudo nano /etc/nginx/sites-available/NAME_OF_APP

and put content (after applying SSL cert it will be changed)

server {
  listen 80;
  server_name DOMAIN_NAME;
  location / {
    proxy_pass http://localhost:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
}

Create link, test it, restart nginx

sudo ln -s /etc/nginx/sites-available/NAME_OF_APP /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx

Create certificate

sudo ufw allow 'Nginx Full' // Allow HTTPS traffic
sudo certbot --nginx -d DOMAIN_NAME -d www.DOMAIN_NAME
sudo certbot renew --dry-run

Now start nginx and pm2

sudo systemctl restart nginx
systemctl status nginx
pm2 start npm --name NAME_OF_APP -- start
pm2 list

In the browser run

DOMAIN_NAME

it should be opened and run on https