How to Host a Simple HTML Page Using Nginx on an EC2 Instance

If you’re looking to host a simple HTML page using an Nginx web server on AWS EC2, this step-by-step guide will walk you through the entire process. Nginx is a popular web server that can serve static files efficiently, and by deploying it on an EC2 instance, you can host your web pages with ease.

Prerequisites

  • An AWS account.
  • A running EC2 instance with a security group that allows HTTP (port 80) traffic.
  • Basic knowledge of Linux and AWS EC2.

Let’s get started!

Step 1: Log in to your EC2 Instance

Connect to your EC2 instance using EC2 Instance Connect.

Step 2: Switch to the Root User

Elevate your privileges to the root user to perform administrative tasks.

sudo su -

Step 3: Update the System

Before installing any software, make sure your system packages are up to date.

sudo apt-get update -y

Step 4: Install Nginx

Now, install Nginx, which will act as your web server.

sudo apt install nginx -y

Step 5: Navigate to the Web Directory

By default, Nginx serves web content from the /var/www/html/ directory. Let’s move into that directory.

cd /var/www/html/

Step 6: Manage Nginx Service

Before making changes, ensure the Nginx service is running. You can check the status and stop or start it as needed.

service nginx status   # Check Nginx status
service nginx stop     # Stop Nginx
service nginx start    # Start Nginx

Step 7: Modify the Nginx Configuration

If you need to adjust the Nginx configuration, you can modify the default configuration file located in /etc/nginx/sites-enabled/.

cd /etc/nginx/sites-enabled/
nano default

Here, you can configure your server block to match the content you’re serving.

Step 8: Download the HTML Files

Let’s create a temporary directory to download the HTML files for our website.

mkdir temp
cd temp/

Now, use wget to download the HTML files from GitHub (or any other source).

wget https://github.com/ranjithkumarmadhavan/test/archive/refs/heads/main.zip

Step 9: Extract the Files

Check if the file is downloaded correctly and install the unzip utility if it’s not installed yet.

ls -lrt         # List the downloaded files
apt install unzip
unzip main.zip  # Unzip the downloaded file

Step 10: Move the Files to the Web Directory

Once the files are extracted, move the contents into the /var/www/html/ directory, which is the root directory for Nginx to serve your HTML files.

cd test-main/
mv * /var/www/html/

Step 11: Restart Nginx

Now that the HTML files are in place, restart the Nginx service to load your new website.

cd /var/www/html/
service nginx stop   # Stop Nginx
service nginx start  # Start Nginx

Step 12: Access Your Website

With Nginx up and running, you can now access your website by navigating to the public IP address of your EC2 instance in a web browser.

http://<your-ec2-public-ip>

You should see your HTML page displayed, hosted by the Nginx server running on your EC2 instance.


Conclusion

Congratulations! You’ve successfully hosted a simple HTML page using Nginx on an AWS EC2 instance. This setup provides a foundation for hosting more complex websites and applications in the future. Stay tuned for more tutorials on managing and scaling web applications in the cloud!

Leave a Comment