How I Deployed a Next.js App on AWS Lightsail with Nginx and SSL
Deploying a web application is one of the most important parts of real project development. Building an app locally is only half of the work. The real challenge starts when the project needs to run online with a custom domain, HTTPS, proper server setup, and stable performance.
In one of my projects, I worked on deploying a Next.js application on AWS Lightsail. The setup included Node.js, PM2, Nginx reverse proxy, domain connection, and SSL certificate using Certbot. This article explains the practical flow I followed and the things I learned during the deployment.
Why I Used AWS Lightsail
AWS Lightsail is useful for small and medium projects because it gives a simple virtual server with predictable pricing. Instead of managing many cloud services separately, Lightsail gives a direct server where we can install Node.js, run the app, configure Nginx, and connect a domain.
For my project, I wanted a setup where the web app could run continuously and users could access it through a proper domain with HTTPS. Lightsail was a good choice for this requirement.
Basic Deployment Flow
The deployment process followed this simple flow:
- Create AWS Lightsail instance
- Install Node.js and npm
- Upload or clone project code
- Install project dependencies
- Build the Next.js app
- Run the app using PM2
- Configure Nginx as reverse proxy
- Point domain to server IP
- Add SSL using Certbot
Installing Project Dependencies
After moving the project to the server, the first step was to install dependencies. This is usually done using npm.
npm install
After installation, I created a production build.
npm run build
If the build fails, it usually means there is a TypeScript error, environment variable issue, missing package, or memory problem. In production deployment, fixing build errors is very important.
Running the App with PM2
PM2 helps keep the Node.js app running in the background. If the server restarts or the app crashes, PM2 can restart it automatically.
pm2 start npm --name "my-app" -- start
After starting the app, I checked whether it was running properly.
pm2 status
This step is important because the app should not depend on an open terminal session. PM2 makes the app production-ready.
Configuring Nginx Reverse Proxy
Next.js usually runs on a port like 3000. But users should not access the site
using a port number. They should access it using the domain name. For this, I used Nginx as a
reverse proxy.
Nginx receives traffic from the domain and sends it internally to the running Next.js app. This makes the website accessible through a clean domain.
Connecting the Domain
After the app was running on the server, I connected the domain by updating DNS records. The main record required is usually an A record that points the domain to the server's public IP address.
DNS changes can take some time to update. Sometimes it works in a few minutes, and sometimes it takes longer depending on the domain provider and DNS propagation.
Adding SSL with Certbot
SSL is required to make the website secure and accessible through HTTPS. For this, I used Certbot with Nginx. After SSL setup, the website started opening with HTTPS.
HTTPS is important because users trust secure websites more. It is also important for modern web apps, SEO, and browser compatibility.
Problems I Faced
During deployment, some common problems came up. These included build errors, server memory issues, Nginx configuration mistakes, and domain DNS delays. These problems are normal in real deployments.
The best way to solve them is to check logs carefully. PM2 logs and Nginx logs are very helpful for understanding what is going wrong.
pm2 logs
What I Learned
This deployment taught me that production setup is different from local development. Locally, a project may work fine, but on a server, we need to think about build process, environment variables, ports, reverse proxy, SSL, domain, and monitoring.
I also learned that a stable deployment requires patience. Every error gives useful information, and checking logs is one of the most important skills for a developer.
Conclusion
Deploying a Next.js app on AWS Lightsail gave me practical experience with real server management. I used PM2 to keep the app running, Nginx to connect the app with the domain, and Certbot to enable HTTPS.
This setup is useful for developers who want to deploy real projects without depending only on managed hosting platforms. It gives more control and helps understand how production web apps actually run.