Deploy Next.js to VPS Step by Step
This guide presumes that your next.js stack is:
- Next.js 16
- Bun
- Drizzle ORM
- MySQL database
- Docker
- Github Repo
- Caddy Web Server
- VPS
- Domain
Step 1:
Go to your vps terminal and create your project folder structure.
Step 1.1
First, go to this directory:
cd /var/www/Step 1.2
Now, create a folder inside /var/www/ and name should be: project-name or projectname (LowerCase)
mkdir project-nameNow, go to the created folder:
cd project-nameNow, inside this folder, we have to create 3 folders which will seperate our code, data and scripts which prevents data loss every time source code updates via git pull on github actions.
mkdir -p data scripts webStep 2:
Now, we have to connect our vps to our github in order to clone our project repo in our created folder inside web directory: /var/www/project-name/web/
Step 2.1
Generate an SSH key on the VPS:
Check whether one already exists:
ls -la ~/.sshIf you don't have a key, generate one and just press enter:
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 -C "your-email@example.com"Start SSH Agent:
eval "$(ssh-agent -s)"Add Key to SSH Agent
ssh-add ~/.ssh/id_ed25519Copy public key output:
cat ~/.ssh/id_ed25519.pubAdd this public key to Github Account Level: GitHub → Settings → SSH and GPG keys → New SSH key Give any title like: ssh-vps-key and paste public key without any extra spaces and click Add SSH key button.
Trust Github Host in VPS:
ssh-keyscan github.com >> ~/.ssh/known_hosts
chmod 644 ~/.ssh/known_hostsTest the connection:
ssh -T git@github.comExpected:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Now, your github account is conneted with your vps.