How to Create Custom Route File in Laravel 11
Read More
Deploying a Next.js application to AWS EC2 provides a scalable and reliable way to manage your web application. By automating the deployment process with GitHub Actions, you can easily push updates to your live application. In this tutorial, we will walk you through the step-by-step process of deploying a Next.js app on an EC2 instance using GitHub Actions.
If you haven’t already created a Next.js application, now is the time to do so. Open your terminal and run the following commands:
npx create-next-app@latest my-next-app
cd my-next-app
You can follow the Next.js official documentation for more details on how to create and configure a Next.js project.
Next, you need to create a repository on GitHub to host your application code.
Here is a sample of the basic commands to push your code:
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/your-username/your-repo.git
git push -u origin main
Here's a step-by-step guide with images.
For more details, refer to the AWS EC2 documentation.
Go to your EC2 dashboard, select your instance, and click Connect.
Use the SSH client or EC2 Instance Connect to access your server.
Change the ownership of the key pair using the following command:
sudo chown 600 /path/to/your-key.pem
5. Run the SSH command to connect:
ssh -i "your-key.pem" [email protected]
To allow traffic to your application:
To automate deployment using GitHub Actions:
.github/workflows
folder.deploy.yml
file with the following content:name: Push-to-EC2 instance
on:
push:
branches:
- main
jobs:
deploy:
name: Push to EC2
runs-on: ubuntu-latest
steps:
- name: Checkout the code
uses: actions/checkout@v2
- name: Deploy to my EC2 instance
uses: easingthemes/[email protected]
env:
SSH_PRIVATE_KEY: ${{ secrets.EC2_SSH_KEY }}
SOURCE: "./"
REMOTE_HOST: ${{ secrets.HOST_DNS }}
REMOTE_USER: ${{ secrets.USERNAME }}
TARGET: ${{ secrets.TARGET_DIR }}
- name: Executing remote ssh commands using ssh key
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST_DNS }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
script: |
sudo yum -y update
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
# Add Node.js source for latest version
curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -
# Install Node.js and npm
sudo yum install -y nodejs
# Navigate to the project directory
cd ${{ secrets.TARGET_DIR }}
# Install project dependencies
npm install
# Build the project
npm run build
# Clean up existing files in the web server directory
sudo rm -rf /var/www/html/*
# Move the build files to the web server directory
sudo mv build/* /var/www/html
# Set proper SELinux context for the web content
sudo chcon -R -t httpd_sys_content_t /var/www/html
# Restart Apache to apply changes
sudo systemctl restart httpd
To securely provide sensitive information to your workflow:
EC2_SSH_KEY
: Your EC2 instance SSH private key.HOST_DNS
: Public IPv4 address or DNS of your EC2 instance.USERNAME
: The default user is ec2-user
or ubuntu
.TARGET_DIR
: The directory on the EC2 instance where your app should be deployed (e.g., /home/ubuntu/app
).
Push your changes to GitHub:
git add .
git commit -m "Set up GitHub Actions for deployment"
git push origin main
GitHub Actions will automatically run the deployment workflow. You can check the status of your deployment in the Actions tab of your repository.
After deployment, navigate to the public IPv4 address of your EC2 instance in your browser to verify that your Next.js application is live.
Deploying a Next.js application on AWS EC2 using GitHub Actions is a powerful way to manage your web application efficiently. With this setup, you can automate your deployments, reduce manual errors, and ensure a smooth CI/CD pipeline.
For more in-depth tutorials, check out the GitHub Actions documentation and AWS EC2 best practices.
Feel free to share this guide and follow our blog for more DevOps and cloud deployment tips!
Recent posts form our Blog
0 Comments
Like 0