To host a Next.js website on an Ubuntu VPS, install Node.js (current LTS), build the app with next build, run it with a process manager such as PM2, put Nginx in front as a reverse proxy on ports 80 and 443, and secure it with a free Let's Encrypt certificate. You can then automate deployments with GitHub Actions over SSH.
This guide walks through the full setup on a fresh Ubuntu LTS server, using the documentation-reserved IP address 203.0.113.10 and the domain example.com as placeholders. Replace them with your own values.
Connect to the server with SSH from a terminal on Linux, macOS or Windows (PowerShell and Windows Terminal include an SSH client):
Update the system, then create a normal user with sudo rights so you do not run everything as root:
Copy your SSH key to the new user from your local machine, then log in as that user:
Enable the firewall and allow only SSH and web traffic:
Once key login works, consider disabling password login and root login by setting PasswordAuthentication no and PermitRootLogin no in /etc/ssh/sshd_config, then running sudo systemctl restart ssh.
Install Nginx and check that it is running:
Open http://203.0.113.10/ in a browser. You should see the default "Welcome to nginx" page.
Install Node.js with nvm so that you can switch versions without touching system packages. Check the Next.js documentation for the minimum Node.js version your Next.js release needs; the current LTS line is a safe choice.
Install PM2 globally:
You need two separate things: a way for the server to pull your code from GitHub, and a way for GitHub Actions to log in to the server. Use modern Ed25519 keys for both, since they are shorter and stronger than the older RSA default.
Generate a key on the server and add the public half to the repository as a read-only deploy key:
In GitHub, open the repository, go to Settings, then Deploy keys, then Add deploy key, and paste the public key. Leave write access off. Then tell SSH to use that key for GitHub:
Save those lines in ~/.ssh/config, then clone the project:
Generate a second key pair for the CI job. Protect it with the right permissions and authorise the public key:
Copy the private key output. In the repository, open Settings, then Secrets and variables, then Actions, and create secrets named SSH_PRIVATE_KEY, SSH_HOST and SSH_USER. An empty passphrase is common for CI keys, so limit the key to this one account and never reuse it elsewhere. Delete the private key file from the server after you have stored it as a secret.
Open ~/.ssh/authorized_keys with nano if you need to remove duplicate or unknown keys.
Install dependencies, build and start the app on a local port:
Run the command that pm2 startup prints so the app restarts after a reboot. Check the process:
Put environment variables in a .env.production file in the project directory; Next.js reads it at build and run time. Never commit it.
Create /etc/nginx/sites-available/example.com with the following content:
Enable the site, test the configuration and reload Nginx:
Point the DNS A record for your domain at the server IP, wait for it to propagate and open the site in a browser.
Install Certbot and request a certificate. It edits the Nginx configuration and sets up automatic renewal:
Choose the option to redirect HTTP to HTTPS when asked.
Create .github/workflows/deploy.yml in your repository:
Each push to main now updates the site. For larger projects, build in CI and upload the output, or use the output: "standalone" option in next.config.js to produce a smaller server bundle.
Use an active LTS release that your Next.js version supports. The Next.js documentation lists the minimum required version.
Both work. PM2 is quick to set up and gives logs and restarts with one tool, while systemd avoids an extra dependency. Pick one and use it consistently.
Nginx terminates TLS, serves on standard ports, handles compression and rate limiting and keeps the Node process off the public interface.
A VPS gives you control and predictable cost, but you manage updates, security and scaling yourself. A managed platform removes that work.
Create a non-root user, enable the firewall, install Nginx, Node.js and PM2, and run the built Next.js app on a local port. Proxy it through Nginx, secure it with Certbot and use separate Ed25519 keys for pulling code and for GitHub Actions deployments. Keep the system patched and restrict SSH to key-based logins.