Integrated Tech Solutions

Beginners Guide to deploy NextJs App on AWS EC2 Server

By - Admin December 22, 2024

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

  1. Log in to your AWS Management Console and navigate to the EC2 dashboard.
  2. Click on “Launch Instance” and choose the Amazon Linux 2 AMI (HVM) – Kernel 5.x.
  3. Choose the instance type (e.g., t2.micro) and click “Next: Configure Instance Details”.
  4. Configure the instance details as desired (e.g., VPC, subnet, security group) and click “Next: Add Storage”.
  5. Add a new volume (e.g., 30 GB) and click “Next: Add Tags”.
  6. Add a name tag for your instance (e.g., “Next.js-App”) and click “Next: Configure Security Group”.
  7. Create a new security group with the following rules:
  8. SSH (port 22) from your IP address
  9. HTTP (port 80) from anywhere
  10. HTTPS (port 443) from anywhere
  11. Review and launch the instance.

Step 2: Connecting to the EC2 Instance

  1. Wait for the instance to launch and become available.
  2. Navigate to the EC2 dashboard and select the instance.
  3. Click on “Actions” and then “Connect to instance”.
  4. Follow the instructions to connect to the instance using SSH.

Step 3: Installing Dependencies

  1. 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

  1. Clone your Next.js application repository from GitHub or another version control system.
  2. 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
  1. 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
  1. 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;
    }
}
  1. 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.

Keep Reading

👋 Hi, Find this Helpful? There is More

You Asked,
We made it!

fix japanese keyword hack

Step by Step Video Tutorials on trending topics in software development

Yes I am Curious »

5 Tips to Pick the Most Engaging Live Chat for Your Website

In today’s fast-paced digital world, providing excellent customer service is a key differentiator for businesses. One of the most effective...

Know More »
next js deployed on aws

Beginners Guide to deploy NextJs App on AWS EC2 Server

In this blog post, we will walk you through the step-by-step process of deploying a Next.js application on an AWS...

Know More »