Deploying Next.js application on an Amazon EC2 instance (AWS)
Tags: nextjs, aws, ec2, abotwrotethis
Published: 2024-05-14 09:19:11

Deploying a Next.js application on an Amazon EC2 instance gives you full control over the web server environment and is a great choice if you need more flexibility and scalability than what platform services like AWS Amplify offer. Here’s how to deploy a Next.js application on an Amazon EC2 instance:

### Step 1: Prepare Your Next.js Application
Make sure your application is ready for production. You should have a `package.json` with at least these scripts:

```json
"scripts": {
  "build": "next build",
  "start": "next start -p 80"
}
```

Ensure all dependencies are correctly specified, and your application works locally.

### Step 2: Launch an EC2 Instance
1. **Log into your AWS Management Console** and navigate to the EC2 Dashboard.
2. **Launch Instance**: Choose an AMI (Amazon Machine Image), such as the Amazon Linux 2 AMI.
3. **Choose an Instance Type**: Select the instance type that fits your application's requirements (e.g., t2.micro for small scale).
4. **Configure Instance Details**: Set network and subnet, and ensure Auto-assign Public IP is enabled.
5. **Add Storage** as needed.
6. **Add Tags**: Optional, but useful for naming and managing your instance via the AWS console.
7. **Configure Security Group**: Open TCP port 80 for HTTP traffic, and TCP port 22 for SSH. You might also want to open port 443 if you plan to use HTTPS.
8. **Review and Launch**: Review your settings, then launch the instance.
9. **Create a Key Pair**: Save the key pair (.pem file) to a secure location. This key is needed to SSH into your instance.

### Step 3: Connect to Your Instance
- Use the key pair and public DNS provided by AWS to connect to your instance via SSH. For example:
  ```bash
  ssh -i "your-key-pair.pem" ec2-user@ec2-xx-xx-xx-xx.compute-1.amazonaws.com
  ```

### Step 4: Set Up the Environment
1. **Update Your System**:
   ```bash
   sudo yum update -y
   ```
2. **Install Node.js** (consider using NVM for managing Node.js versions):
   ```bash
   curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
   source ~/.bashrc
   nvm install node  # Install the latest version of node
   ```
3. **Install PM2** to manage and keep your application running:
   ```bash
   npm install pm2 -g
   ```

### Step 5: Deploy Your Next.js Application
1. **Upload Your Application** to your instance using SCP or a Git repository.
2. **Install Dependencies**:
   ```bash
   cd your-nextjs-app
   npm install
   ```
3. **Build Your Application**:
   ```bash
   npm run build
   ```
4. **Start Your Application Using PM2**:
   ```bash
   pm2 start npm --name "next-app" -- start
   ```
5. **Configure PM2 to Start on Boot**:
   ```bash
   pm2 startup
   pm2 save
   ```

### Step 6: Configure a Web Server (Optional)
For production environments, you may want to use a web server like Nginx as a reverse proxy to serve your Next.js app, handle SSL/TLS, and manage static files efficiently.

1. **Install Nginx**:
   ```bash
   sudo yum install nginx -y
   ```
2. **Configure Nginx**: Create a configuration file `/etc/nginx/conf.d/your-nextjs-app.conf` with the following:
   ```nginx
   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;
       }
   }
   ```
3. **Restart Nginx**:
   ```bash
   sudo systemctl restart nginx
   ```

### Step 7: Secure Your Application
- Consider configuring HTTPS using Let's Encrypt or a similar service.
- Regularly update your software and monitor your instance for unusual activity.

By following these steps, your Next.js application will be up and running on an AWS EC2 instance, providing you with the flexibility to manage and scale your environment as needed.