Setting up a basic Nginx webpage on AWS EC2 Instance

This guide walks through the process of deploying a simple web page using Nginx on an AWS EC2 instance, including proper setup, and best practices. It is your starter kit for future seamless deployments. Prerequisites AWS account with appropriate permissions Basic understanding of Linux commands Web browser access to AWS Management Console Step 1: Creating an EC2 Instance Navigate to AWS Console Go to aws.amazon.com and sign in Search for "EC2" in the top search bar Select "EC2" from the services list Launch Instance Configuration Click "Launch Instance" button Enter a meaningful name for your instance Select Ubuntu Server as the Amazon Machine Image (AMI) Alternative: Amazon Linux 2 (uses yum instead of apt) Choose instance type (t2.micro is eligible for free tier) Create or select a key pair for SSH access Network Security Configuration Create a new security group or select existing Add inbound rules: HTTP (Port 80) HTTPS (Port 443) SSH (Port 22) Configure security group name and description Step 2: User Data Configuration Add the following script in the user data section during instance creation. This script will automatically install and configure Nginx when the instance launches. #!/bin/bash # Update package lists apt update apt upgrade -y # Install nginx apt install -y nginx # Display nginx version nginx -v # Start and enable nginx service systemctl start nginx systemctl enable nginx # Set permissions for web root directory chmod 2775 /var/www/html find /var/www/html -type d -exec chmod 2775 {} \; find /var/www/html -type f -exec chmod 0664 {} \; # Create custom index page echo " Welcome to DevOps Stage 0 - Ayomide/Mischief " > /var/www/html/index.html Step 3: Launch and Verify Launch the instance Review all configurations Click "Launch Instance" Verify Deployment Wait 2-3 minutes for instance to initialize Copy the public IP address from instance details Open a web browser and navigate to: http://54.236.249.255 You should see your custom welcome page Step 4: Maintenance and Clean Up Regular Maintenance Regularly update system packages: sudo apt update && sudo apt upgrade Monitor system logs: sudo tail -f /var/log/nginx/access.log Check Nginx status: sudo systemctl status nginx Clean Up Stop instance when not in use: AWS Console → EC2 → Select Instance → Instance State → Stop To permanently remove: AWS Console → EC2 → Select Instance → Instance State → Terminate Remember to delete associated resources (EBS volumes, Elastic IPs) to avoid charges Troubleshooting If the web page doesn't load: Verify instance status is "running" Check security group settings Verify Nginx is running: sudo systemctl status nginx Check Nginx error logs: sudo cat /var/log/nginx/error.log Remember: Always terminate resources you're not actively using to avoid unnecessary AWS charges. To be connected to top talents who make the entire deployment process feel like a breeze, visit HNG Tech.

Jan 30, 2025 - 03:34
 0
Setting up a basic Nginx webpage on AWS EC2 Instance

This guide walks through the process of deploying a simple web page using Nginx on an AWS EC2 instance, including proper setup, and best practices. It is your starter kit for future seamless deployments.

Prerequisites

  • AWS account with appropriate permissions
  • Basic understanding of Linux commands
  • Web browser access to AWS Management Console

Step 1: Creating an EC2 Instance

  1. Navigate to AWS Console
    • Go to aws.amazon.com and sign in
    • Search for "EC2" in the top search bar
    • Select "EC2" from the services list

AWS homepage

  1. Launch Instance Configuration

    • Click "Launch Instance" button
    • Enter a meaningful name for your instance
    • Select Ubuntu Server as the Amazon Machine Image (AMI)
      • Alternative: Amazon Linux 2 (uses yum instead of apt)
    • Choose instance type (t2.micro is eligible for free tier)
    • Create or select a key pair for SSH access
  2. Network Security Configuration

    • Create a new security group or select existing
    • Add inbound rules:
      • HTTP (Port 80)
      • HTTPS (Port 443)
      • SSH (Port 22)
    • Configure security group name and description

Creating EC2 instance

Step 2: User Data Configuration

Add the following script in the user data section during instance creation. This script will automatically install and configure Nginx when the instance launches.

#!/bin/bash

# Update package lists
apt update
apt upgrade -y

# Install nginx
apt install -y nginx

# Display nginx version
nginx -v

# Start and enable nginx service
systemctl start nginx
systemctl enable nginx

# Set permissions for web root directory
chmod 2775 /var/www/html
find /var/www/html -type d -exec chmod 2775 {} \;
find /var/www/html -type f -exec chmod 0664 {} \;

# Create custom index page
echo "

Welcome to DevOps Stage 0 - Ayomide/Mischief

"
> /var/www/html/index.html

Step 3: Launch and Verify

  1. Launch the instance

    • Review all configurations
    • Click "Launch Instance"
  2. Verify Deployment

    • Wait 2-3 minutes for instance to initialize
    • Copy the public IP address from instance details
    • Open a web browser and navigate to: http://54.236.249.255
    • You should see your custom welcome page

Running webpage

Step 4: Maintenance and Clean Up

  1. Regular Maintenance

    • Regularly update system packages: sudo apt update && sudo apt upgrade
    • Monitor system logs: sudo tail -f /var/log/nginx/access.log
    • Check Nginx status: sudo systemctl status nginx
  2. Clean Up

    • Stop instance when not in use: AWS Console → EC2 → Select Instance → Instance State → Stop
    • To permanently remove: AWS Console → EC2 → Select Instance → Instance State → Terminate
    • Remember to delete associated resources (EBS volumes, Elastic IPs) to avoid charges

Troubleshooting

If the web page doesn't load:

  1. Verify instance status is "running"
  2. Check security group settings
  3. Verify Nginx is running: sudo systemctl status nginx
  4. Check Nginx error logs: sudo cat /var/log/nginx/error.log

Remember: Always terminate resources you're not actively using to avoid unnecessary AWS charges.

To be connected to top talents who make the entire deployment process feel like a breeze, visit HNG Tech.