Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ilyasdev3/837ad26eec0c565be969c27bb2dbe7dc to your computer and use it in GitHub Desktop.
Save ilyasdev3/837ad26eec0c565be969c27bb2dbe7dc to your computer and use it in GitHub Desktop.
Full terminal commands & configs to set up a Next.js app on a DigitalOcean Ubuntu server (with PM2, NGINX, SSL, and optional GitHub Actions auto-deploy)
# 🚀 Setup Next.js App on DigitalOcean Ubuntu Server
> **Full Terminal Commands Step-by-Step**
### **Login to server**
```bash
ssh root@ip_address
### **Upgrade Server**
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"
sudo ufw allow OpenSSH
sudo ufw enable
### **Install NPM**
sudo apt install npm
### **Install Node.js**
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
exec $SHELL
nvm install --lts
nvm install 20
nvm use 20
nvm alias default 20
### **Install Node.js**
Option 2 (Nodesource):
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
### **Install PM2**
npm install -g pm2
pm2 status
### **[Setup SSH key to pull repo]**
cd /root/.ssh
ssh-keygen -t rsa -b 4096 -C "[email protected]"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub
### **[Setup SSH key to pull repo]**
GitHub Repo Settings > Deploy Keys, or
GitHub Profile Settings > SSH Keys (for all repos).
### **Go to web root**
cd /var/www
### **Create Next.js App OR Clone Existing Repo**
# Option 1:
npx create-next-app@latest my_app
# Option 2:
git clone [email protected]:username/repo_name.git
# Or clone specific branch
git clone -b branch-name [email protected]:username/repo_name.git local-folder-name
### **Install & Build**
cd name_of_app
npm install
npm run build
### **Create NGINX Config**
cd /etc/nginx/sites-available
touch name_of_app
nano name_of_app
# See config in section 07.
### **Enable Config & Remove Default**
sudo ln -s /etc/nginx/sites-available/name_of_app /etc/nginx/sites-enabled/name_of_app
nginx -t
rm /etc/nginx/sites-available/default
rm /etc/nginx/sites-enabled/default
systemctl restart nginx
### **Run with PM2**
cd /var/www/name_of_app
# Default port
pm2 start npm --name name_of_app -- start
# Custom port
pm2 start npm --name name_of_app -- start -- --port=3001
pm2 save
pm2 startup
### **Enable SSL**
sudo certbot --nginx -d domain.com -d www.domain.com
### **02 - NGINX Config for Next.js**
# Place in /etc/nginx/sites-available/name_of_app
server {
listen 80;
server_name domainname.com www.domainname.com;
gzip on;
gzip_proxied any;
gzip_types application/javascript application/x-javascript text/css text/javascript;
gzip_comp_level 5;
gzip_buffers 16 8k;
gzip_min_length 256;
location /_next/static/ {
alias /var/www/name_of_app/.next/static/;
expires 365d;
access_log off;
}
location / {
proxy_pass http://127.0.0.1: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;
}
}
### **Auto Deployment from GitHub**
cd /root/.ssh
ssh-keygen -t rsa -b 4096 -C "[email protected]"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub # Add to GitHub
### **GitHub Actions Deploy File**
# .github/workflows/deploy.yml
name: Deploy Next.js App
on:
push:
branches:
- branch_name
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "20"
- uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Deploy to Server
env:
HOST: XX.XX.XX.XX
USERNAME: root
TARGET_DIR: /var/www/folder_name
APP_NAME: app_name
PORT: 3000
run: |
ssh -o StrictHostKeyChecking=no $USERNAME@$HOST << EOF
export PATH=/root/.nvm/versions/node/v20.12.0/bin:$PATH
cd $TARGET_DIR
git pull origin ${GITHUB_REF#refs/heads/}
npm install
npm run build
if pm2 list | grep -q $APP_NAME; then
pm2 restart $APP_NAME
else
pm2 start npm --name $APP_NAME -- start -- --port=$PORT
fi
pm2 save
EOF
### **Helpful Commands**
pm2 start npm --name name_of_app -- start
systemctl restart nginx
sudo certbot --nginx -d domain.com
@qasim-ali097
Copy link

Amazing Work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment