In this blog post, we will walk you through the step-by-step process of deploying a Next.js application on an AWS EC2 instance. We will cover the prerequisites, setting up the EC2 instance, installing the necessary dependencies, and deploying the application.
Prerequisites
- An AWS account with an EC2 instance set up (you can use the free tier for this tutorial)
- A Next.js application set up on your local machine
- Basic knowledge of Linux commands and AWS services
Step 1: Setting Up the EC2 Instance
- Log in to your AWS Management Console and navigate to the EC2 dashboard.
- Click on “Launch Instance” and choose the Amazon Linux 2 AMI (HVM) – Kernel 5.x.
- Choose the instance type (e.g., t2.micro) and click “Next: Configure Instance Details”.
- Configure the instance details as desired (e.g., VPC, subnet, security group) and click “Next: Add Storage”.
- Add a new volume (e.g., 30 GB) and click “Next: Add Tags”.
- Add a name tag for your instance (e.g., “Next.js-App”) and click “Next: Configure Security Group”.
- Create a new security group with the following rules:
- SSH (port 22) from your IP address
- HTTP (port 80) from anywhere
- HTTPS (port 443) from anywhere
- Review and launch the instance.
Step 2: Connecting to the EC2 Instance
- Wait for the instance to launch and become available.
- Navigate to the EC2 dashboard and select the instance.
- Click on “Actions” and then “Connect to instance”.
- Follow the instructions to connect to the instance using SSH.
Step 3: Installing Dependencies
- Install Node.js and npm using the following commands:
sudo yum install -y nodejs
sudo yum install -y npm
2. Install the PM2 process manager using the following command:
```
sudo npm install pm2 -g
Step 4: Deploying the Next.js Application
- Clone your Next.js application repository from GitHub or another version control system.
- Navigate to the application directory and install the dependencies using the following command:
npm install
3. Build the application using the following command:
```
npm run build
- Start the application using PM2 with the following command:
pm2 start npm — start
**Step 5: Configuring the Nginx Reverse Proxy**
---------------------------------------------------
1. Install Nginx using the following command:
```
sudo yum install -y nginx
- Create a new Nginx configuration file for your application using the following command:
sudo nano /etc/nginx/conf.d/nextjs.conf
3. Add the following configuration to the file:
```
server {
listen 80;
server_name your_domain.com;
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;
}
}
- Restart Nginx using the following command:
sudo service nginx restart
**Conclusion**
----------
In this blog post, we walked you through the step-by-step process of deploying a Next.js application on an AWS EC2 instance. We covered setting up the EC2 instance, installing dependencies, deploying the application, and configuring the Nginx reverse proxy. With these steps, you should now have a fully functional Next.js application deployed on your AWS EC2 instance.