Introduction:
NGINX is a powerful web server that handles high-traffic websites. It’s perfect for hosting websites and applications. AWS EC2 provides scalable computing capacity, making it an ideal platform for NGINX. This guide will help you install NGINX quickly and easily.
Prerequisites:
Before starting, make sure you have:
- An AWS Account
- A running AWS EC2 Linux instance
- SSH access to your server
- Basic Linux commands knowledge
- Root or sudo access
Step-by-Step Installation:
- Launch an EC2 Instance and connect via SSH
Choose an Amazon Machine Image (AMI): Select Amazon Linux 2 AMI.
Select Instance Type: Choose t2.micro for free tier eligibility. Depending on your requirements you can select bigger sizes or type of machines, for the sake of illustration and giving example we are selecting t2.micro instance type which as 1vCPU and 1Gig Memory.
Configure Instance Details: Default settings are fine.
Create a key pair, give a name of the SHS key pair and download the Public Key into your side.
Select networking where EC2 instance is reachable from Internet, in my case, all of the subnets in VPC has a routing table and routing entry towards Internet Gateway (IGW), and EC2 instance will get public IP.
Add Storage: 8 GB is sufficient. Depending on your use case you may need more storage or different type of storage.
Tag Instance: Optional, but helpful for organization.
Configure Security Group: Allow SSH, HTTP, and HTTPS. In the networking settings make sure that you can able to access the machine from your IP address via SSH,HTTP and HTTPS.
Review and Launch: Launch your instance and download the key pair.
Now select your instance and click on Details Tab, you will see more information about the EC2 instance. Since we have enabled auto Public IP settings in Networking, EC2 instance will get public IP.
ssh -i "your-key.pem" ec2-user@your-ec2-public-dns-name
For example i have connected like this, but before connecting, assign correct permissions to SSH Public Key otherwise the remote Linux machine will reject SSH connection request.
chmod 400 nginx-server-sshkey.pem
- Update Your System:
Once you login into AWS EC2 Linux machine, you can start with the following commands for updating and installing NGINX packages.
sudo yum update -y
- Install NGINX:
sudo yum install nginx -y
- Start NGINX:
sudo systemctl start nginx
sudo systemctl enable nginx
Basic Configuration:
- Check NGINX Status:
sudo systemctl status nginx
Security Setup:
- Configure AWS Security Group:
- Open port 80 (HTTP)
- Open port 443 (HTTPS)
- Restrict SSH access to your IP (you can get your local IP from this website https://whatismyipaddress.com/)
- Basic NGINX Security:
- Update default configuration
- Set proper file permissions
- Enable HTTPS
Common Issues:
- NGINX Won’t Start:
- Check error logs:
sudo cat /var/log/nginx/error.log
- Verify ports aren’t in use:
sudo netstat -tulpn
- Cannot Access Website:
- Check security group settings
- Verify NGINX is running
- Confirm instance public IP
Testing:
- Open your browser
- Enter your EC2 public IP
- You should see the NGINX welcome page
Conclusion:
You’ve successfully installed NGINX on your AWS EC2 Linux instance.
Keep your system updated for security and performance.