Deploy apps · 20 min
Deploy a Next.js app
Build and run a Next.js app in production, behind Nginx.
This runs Next.js with its built-in production server, kept alive by PM2, with Nginx in front for your domain and HTTPS.
1. Install Node.js, PM2 and Nginx
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs git nginx
sudo npm install -g pm22. Build your app
cd /opt && sudo git clone https://github.com/you/your-next-app.git
sudo chown -R $USER:$USER your-next-app && cd your-next-app
npm ci
npm run buildPut your environment variables in .env.production before building if your app needs them.
3. Run it with PM2
pm2 start npm --name next-app -- start -- -p 3000
pm2 save && pm2 startup4. Put Nginx in front
Create a site config (replace example.com):
sudo tee /etc/nginx/sites-available/next-app > /dev/null <<'EOF'
server {
listen 80;
server_name example.com www.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
EOF
sudo ln -s /etc/nginx/sites-available/next-app /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx5. Domain and HTTPS
Point your domain to the server, open ports 80 and 443 in the firewall, then add free HTTPS.
Deploying updates
cd /opt/your-next-app && git pull && npm ci && npm run build && pm2 restart next-appStuck? Contact us or open a support ticket from your panel.