DEV Community

Deploy NestJS and NextJS application in same server using pm2 and Nginx

This guide details deploying a Next.js frontend and a NestJS backend on a single server using PM2 and Nginx as a reverse proxy. The Next.js application runs on port 3001, accessible via the root path, while the NestJS backend runs on port 8000, accessed via '/api'. PM2 manages both applications, ensuring smooth operation. Nginx acts as a reverse proxy, routing requests to the appropriate port based on the URL path. The backend is deployed using `pm2 start dist/main.js`, specifying the port in `src/main.ts`. The frontend is deployed using `pm2 start npm -- start`, utilizing the `start` script in `package.json`. An Nginx configuration file is created, directing '/' to port 3001 and '/api' to port 8000. A symbolic link enables the Nginx configuration. Finally, `nginx -t` tests the configuration, and `systemctl restart nginx` restarts the server. The deployed applications are then accessible via a specified domain name. This setup provides a robust and efficient deployment strategy for interconnected frontend and backend applications.
favicon
dev.to
dev.to
Image for the article: Deploy NestJS and NextJS application in same server using pm2 and Nginx