How to Deploy a Next js App on a VPS : A Step-by-Step Guide
Posted By
kamlesh paulon
Dec 9, 2024Deploying a Next.js app on a Virtual Private Server (VPS) can be straightforward with the right tools. This guide will walk you through how to deploy a Next js app using PM2 to manage the Node.js process and Caddy as the web server for handling HTTPS and reverse proxy.
Table of contents
- Prerequisites
- 1. Update Your System:
- 2. Install NVM (Node Version Manager):
- 3. Verify Node.js and npm Installation:
- 4 . Clone Your Project Repository:
- 5. Install PM2 globally:
- 6. Create a
ecosystem.config.js
file: - 7. Save the PM2 process list and configure PM2 to start on boot.
- 8. Setup Caddy to reverse proxy domain to nextjs app:
Prerequisites
- A VPS running Ubuntu or a similar Linux distribution.
- A domain name pointed to your VPS IP address.
- Basic knowledge of using the terminal.
1. Update Your System:
2. Install NVM (Node Version Manager):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash
For the latest version, refer to the official NVM repository.
- Add NVM to Your Shell Profile:
- Reload Your Shell Configuration:
- Verify NVM Installation:
- Install Node.js LTS:
3. Verify Node.js and npm Installation:
4 . Clone Your Project Repository:
5. Install PM2 globally:
6. Create a ecosystem.config.js
file:
You can adjust based on your need
- Then run
it will start your application on background and it will keep running, pm2 also have so many feature you can learn more from there official website https://pm2.keymetrics.io/
7. Save the PM2 process list and configure PM2 to start on boot.
8. Setup Caddy to reverse proxy domain to nextjs app:
If this not work you can alway get updated code from official website https://caddyserver.com/docs/install#debian-ubuntu-raspbian
- Add the following configuration (replace
your_domain
with your actual domain):
- then restart caddy
- Secure Your App with HTTPS:
Caddy will automatically manage SSL/TLS certificates for you. Just make sure your domain’s DNS is correctly pointed to your VPS IP.