Deploying a Next.js App On AWS EC2 Guide
Tags: webdev, tutorial, devops, aws
Published: 2023-11-21 10:06:34


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.

_<u>Step-by-Step Deploying Next.js App On AWS EC2</u>_

$ Step 1: Sign in to your AWS Management Console

$ Step 2: Launch an EC2 Instance

![EC2 Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/01ibtzn5wvjv66lat753.png)

![Launch Instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c5kl5edqty1thgv6wvq7.png)

![AMI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/flawpy48xinq39mvgi7t.png)

![Machine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gmratfeagk6bcxttmdiw.png)

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

![SSH Key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bfsjz1j2pvia8y1ionof.png)

_`Create a new SSH Key Pair`_

![Network](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1n768b1lnyl054vtk82.png)

_`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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jm4kuky87775s6sfgb8f.png)

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

![EC2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzzw30uatyrgb3r7bbjq.png)

![EC2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hqlw755v3ewylryfyh8i.png)

_`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
```
$ Step 4: Install PM2

```
# sudo npm install pm2 -g
```
$ 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
```
![nextjs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/guef3ysnl84mtssm3p10.png)


_> 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."_