DEV Community

Sh Raj
Sh Raj

Posted on

Deploying Next.js application on an Amazon EC2 instance (AWS)

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:

"scripts": {
  "build": "next build",
  "start": "next start -p 80"
}
Enter fullscreen mode Exit fullscreen mode

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:
  ssh -i "your-key-pair.pem" ec2-user@ec2-xx-xx-xx-xx.compute-1.amazonaws.com
Enter fullscreen mode Exit fullscreen mode

Step 4: Set Up the Environment

  1. Update Your System:
   sudo yum update -y
Enter fullscreen mode Exit fullscreen mode
  1. Install Node.js (consider using NVM for managing Node.js versions):
   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
Enter fullscreen mode Exit fullscreen mode
  1. Install PM2 to manage and keep your application running:
   npm install pm2 -g
Enter fullscreen mode Exit fullscreen mode

Step 5: Deploy Your Next.js Application

  1. Upload Your Application to your instance using SCP or a Git repository.
  2. Install Dependencies:
   cd your-nextjs-app
   npm install
Enter fullscreen mode Exit fullscreen mode
  1. Build Your Application:
   npm run build
Enter fullscreen mode Exit fullscreen mode
  1. Start Your Application Using PM2:
   pm2 start npm --name "next-app" -- start
Enter fullscreen mode Exit fullscreen mode
  1. Configure PM2 to Start on Boot:
   pm2 startup
   pm2 save
Enter fullscreen mode Exit fullscreen mode

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:
   sudo yum install nginx -y
Enter fullscreen mode Exit fullscreen mode
  1. Configure Nginx: Create a configuration file /etc/nginx/conf.d/your-nextjs-app.conf with the following:
   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;
       }
   }
Enter fullscreen mode Exit fullscreen mode
  1. Restart Nginx:
   sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)