How to Deploy a Next.js Application Using PM2 and Nginx
Deploying a Next.js application with PM2 and Nginx provides a robust, production-ready setup that ensures high availability and efficient request handling. In this guide, we will walk through the entire process from setting up your server to deploying and managing your Next.js application.
1. Setting Up Your Server
Ensure your server is updated and install the required dependencies:
sudo apt update && sudo apt upgrade -y sudo apt install curl git -y
Refer to this post to install Node.js and npm:
Install PM2 globally:
npm install -g pm2
2. Clone Your Next.js Project
Clone yours application repository and install dependencies. You can refer to this post for more details.
3. Start Next.js with PM2
Start your Next.js app using PM2:
pm2 start npm --name "nextjs-app" -- start
if you want to run this on specific port
PORT=3200 pm2 start npm --name "next-app" -- start
Save the PM2 process so it starts on reboot:
pm2 save pm2 startup
Check if the application is running:
pm2 list pm2 logs nextjs-app
4. Configure Nginx as a Reverse Proxy
Install Nginx:
sudo apt install nginx -y
Create an Nginx configuration file for your Next.js app:
sudo nano /etc/nginx/sites-available/nextjs
Add the following content (replace yourdomain.com
with your domain or server IP):
server { listen 80; server_name yourdomain.com www.yourdomain.com 127.0.0.1; location / { proxy_pass http://localhost: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; } }
Enable the configuration:
sudo ln -s /etc/nginx/sites-available/nextjs /etc/nginx/sites-enabled/
Test the Nginx configuration:
sudo nginx -t
Restart Nginx:
sudo systemctl restart nginx
5. Secure Your Site with SSL (Let's Encrypt)
Install Certbot for free SSL certificates:
sudo apt install certbot python3-certbot-nginx -y
Obtain and configure an SSL certificate:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
Set up auto-renewal:
sudo certbot renew --dry-run
6. Managing Your Application
Restart your Next.js application:
pm2 restart nextjs-app
Stop the application:
pm2 stop nextjs-app
You can stop using pm2 ID as well
pm2 list ┌────┬─────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐ │ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │ ├────┼─────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤ │ 4 │ application1 │ default │ 0.39.4 │ fork │ 32720 │ 10D │ 0 │ online │ 0% │ 16.2mb │ ubuntu │ disabled │ │ 0 │ application2 │ default │ 0.39.4 │ fork │ 0 │ 0 │ 34 │ errored │ 0% │ 0b │ ubuntu │ disabled │ │ 1 │ application3 │ default │ 0.39.4 │ fork │ 0 │ 0 │ 0 │ stopped │ 0% │ 0b │ ubuntu │ disabled │ │ 2 │ application4 │ default │ 0.39.4 │ fork │ 0 │ 0 │ 0 │ stopped │ 0% │ 0b │ ubuntu │ disabled │ │ 3 │ application5 │ default │ 0.39.4 │ fork │ 0 │ 0 │ 0 │ stopped │ 0% │ 0b │ ubuntu │ disabled │ │ 6 │ application6 │ default │ 0.39.4 │ fork │ 174099 │ 7D │ 0 │ online │ 0% │ 17.6mb │ ubuntu │ disabled │ └────┴─────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘ pm2 stop 4
View pm2 logs:
pm2 logs nextjs-app
View NGINX logs:
sudo journalctl -u nginx --no-pager