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"
}
Ensure all dependencies are correctly specified, and your application works locally.
Step 2: Launch an EC2 Instance
- Log into your AWS Management Console and navigate to the EC2 Dashboard.
- Launch Instance: Choose an AMI (Amazon Machine Image), such as the Amazon Linux 2 AMI.
- Choose an Instance Type: Select the instance type that fits your application's requirements (e.g., t2.micro for small scale).
- Configure Instance Details: Set network and subnet, and ensure Auto-assign Public IP is enabled.
- Add Storage as needed.
- Add Tags: Optional, but useful for naming and managing your instance via the AWS console.
- 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.
- Review and Launch: Review your settings, then launch the instance.
- 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
Step 4: Set Up the Environment
- Update Your System:
sudo yum update -y
- 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
- Install PM2 to manage and keep your application running:
npm install pm2 -g
Step 5: Deploy Your Next.js Application
- Upload Your Application to your instance using SCP or a Git repository.
- Install Dependencies:
cd your-nextjs-app
npm install
- Build Your Application:
npm run build
- Start Your Application Using PM2:
pm2 start npm --name "next-app" -- start
- Configure PM2 to Start on Boot:
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.
- Install Nginx:
sudo yum install nginx -y
-
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;
}
}
- Restart Nginx:
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.