DEV Community

Zahid Ahmed
Zahid Ahmed

Posted on

Deploying a Next.js App On AWS EC2 Guide

Before diving into the deployment process, let’s highlight some key benefits of hosting your Next.js app on the AWS cloud:

1) Scalability: AWS provides the flexibility to scale your EC2 instances based on demand. This means your Next.js app can handle traffic spikes without compromising performance.

2) Reliability: AWS offers a highly reliable infrastructure with a Service Level Agreement (SLA) that ensures your application is available and operational when your users need it.

3) Global Reach: Deploying on AWS allows you to choose data centers around the world, reducing latency and providing a faster experience for users across different geographical locations.

4) Security: AWS provides robust security measures, including firewalls, encryption, and identity management, ensuring that your Next.js app and user data are well-protected.

5) Cost Efficiency: AWS EC2 instances offer a pay-as-you-go model, allowing you to control costs by selecting the appropriate instance type and scaling resources as needed.

Step-by-Step Deploying Next.js App On AWS EC2

$ Step 1: Sign in to your AWS Management Console

$ Step 2: Launch an EC2 Instance

EC2 Dashboard

Launch Instance

AMI

Machine

Choose an Instance Type. You can select the type of machine, number of vCPUs, and memory that is required.

SSH Key

Create a new SSH Key Pair

Network

Now under Network Settings, Choose the default VPC with Auto-assign public IP in enable mode. I will select an existing security group and will make sure under the Inbound rules of my Devops-SG, HTTP, and HTTPS ports are open. Click on Save rules to proceed.

EC2

Modify rest of the settings as per your need and click on Launch Instance

EC2

EC2

On the next screen you can see a success message after the successful creation of the EC2 instance, click on Connect to instance button. Instance wizard will open, go to SSH client tab and copy the provided chmod and SSH command.

$ Step 3: Install Dependencies

# sudo apt update
# curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
# sudo apt-get install -y nodejs
# node -v
# sudo npm install -g npm@latest
# npm -v
# git clone https://github.com/zahido/nextjs-apps.git
# cd nextjs-apps/
# npm install
# npm run build
Enter fullscreen mode Exit fullscreen mode

$ Step 4: Install PM2

# sudo npm install pm2 -g
Enter fullscreen mode Exit fullscreen mode

$ Step 5: Run Next.js via PM2

# pm2 start npm --name nextjs-apps -- run start -- -p 3000
# pm2 list nextjs-apps
# pm2 stop nextjs-apps
# pm2 start nextjs-apps
# pm2 restart nextjs-apps
# pm2 delete nextjs-apps
Enter fullscreen mode Exit fullscreen mode

nextjs

> Enter the EC2 Instance’s public IP along with port number 3000 in a web browser.

Conclusion
This post has demonstrated the deployment of a Next.js App on AWS EC2 instance. "Together, let's embark on a voyage into the dynamic universe of DevOps, uncovering exhilarating insights. Your active participation is truly valued on this exploration."

Top comments (1)

Collapse
 
mmuller88 profile image
Martin Muller

Nice post :)! I run it on AWS ECS and test it locally with docker-compose. If you are someone else need help with reach out :).