Deploying a Next.js application on a Virtual Private Server (VPS) involves several key steps, from setting up your server environment to configuring a reverse proxy and process manager. This guide focuses on using an Ubuntu-based VPS, Nginx as a reverse proxy, and PM2 for process management, along with Certbot for SSL certificates.
Prerequisites:
- A VPS running Ubuntu (e.g., 20.04, 22.04, or 24.04).
- A registered domain name with its DNS records pointing to your VPS's IP address.
- Basic knowledge of the Linux command line.
- Your Next.js application ready for production (pushed to a Git repository is ideal).
Step 1: Connect to Your VPS and Update the System
-
Connect via SSH: Open your terminal and connect to your VPS using SSH:
bash ssh your_username@your_vps_ipReplaceyour_vps_ipwith the actual IP address of your VPS. -
Update and Upgrade System Packages: It's crucial to ensure your server's packages are up-to-date for security and stability.
bash sudo apt update && sudo apt upgrade -y
Step 2: Install Node.js and npm/yarn
Next.js requires Node.js. You can install Node.js using Node Version Manager (NVM) for flexible version management, or directly from Ubuntu's repositories. NVM is generally recommended.
Option A: Install Node.js using NVM (Recommended)
1. Install NVM:
bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
(Note: The version v0.39.7 is from a search result. You may check the NVM GitHub page for the latest version.)
2. Activate NVM:
You might need to close and reopen your terminal, or run one of the following commands depending on your shell:
bash
source ~/.bashrc
# Or source ~/.profile or source ~/.zshrc
3. Install Node.js LTS version:
bash
nvm install --lts
Alternatively, install a specific version, e.g., nvm install 20.
4. Verify Installation:
bash
node -v
npm -v
This should display the installed Node.js and npm versions.
Option B: Install Node.js and npm from Ubuntu Repositories
1. Install Node.js:
bash
sudo apt install nodejs -y
2. Install npm:
bash
sudo apt install npm -y
3. Verify Installation:
bash
node -v
npm -v
Step 3: Deploy Your Next.js Application
-
Clone Your Repository: Navigate to a suitable directory (e.g.,
/var/www/) and clone your Next.js project from your Git repository.bash sudo mkdir -p /var/www/your-app-name sudo chown -R your_username:your_username /var/www/your-app-name cd /var/www/your-app-name git clone https://github.com/your-username/your-nextjs-app.git .If your repository is private, ensure you have set up SSH keys or use a personal access token for cloning via HTTPS. -
Install Dependencies and Build: Install your project's dependencies and then build the Next.js application for production.
bash npm install # or yarn install npm run build # or yarn buildThenpm run buildcommand creates an optimized production build of your application.
Step 4: Install and Configure PM2 (Process Manager)
PM2 is a production process manager for Node.js applications that keeps them alive indefinitely and enables easy management.
npm install -g pm2
Start your Next.js application with PM2. Next.js applications typically run on localhost:3000 by default.
pm2 start npm --name "my-nextjs-app" -- start
Replace "my-nextjs-app" with a descriptive name for your application.
To ensure PM2 restarts your application on server reboot:
pm2 startup systemd
pm2 save
Step 5: Configure Nginx as a Reverse Proxy
Nginx will act as a reverse proxy, forwarding requests from your domain to your Next.js application running on port 3000.
sudo apt install nginx -y
Create a new Nginx configuration file for your site. Replace your_domain.com with your actual domain.
sudo nano /etc/nginx/sites-available/your_domain.com
Add the following configuration:
server {
listen 80;
server_name your_domain.com www.your_domain.com;
location / {
proxy_pass http://localhost:3000; # Next.js default port
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;
}
}
Enable the site by creating a symlink to sites-enabled and test the Nginx configuration:
sudo ln -s /etc/nginx/sites-available/your_domain.com /etc/nginx/sites-enabled/
sudo nginx -t
If the test is successful, restart Nginx:
sudo systemctl restart nginx
Step 6: Configure Firewall (UFW)
If you're using UFW (Uncomplicated Firewall), allow HTTP (port 80) and HTTPS (port 443) traffic.
sudo ufw allow 'Nginx Full'
sudo ufw enable
Step 7: (Optional) Set up SSL with Certbot (Let's Encrypt)
To secure your application with HTTPS, use Certbot to obtain and configure SSL certificates from Let's Encrypt.
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your_domain.com -d www.your_domain.com
Follow the prompts. Certbot will automatically modify your Nginx configuration and set up automatic certificate renewal.
Step 8: Verify Your Deployment
Your Next.js application should now be accessible via your domain name (or VPS IP address if you skipped domain setup) over HTTPS. Open your web browser and navigate to https://your_domain.com.
(Optional) Set up CI/CD
For automated deployments, consider setting up a Continuous Integration/Continuous Deployment (CI/CD) pipeline using tools like GitHub Actions, GitLab CI, or Jenkins. This would automate the steps of cloning, installing, building, and restarting your application on the VPS whenever you push new code to your repository.
Your Next.js application should now be accessible via your domain name!