Deploying A Linux Virtual Machine with Nginx on AWS

Deploying A Linux Virtual Machine with Nginx on AWS

This guide will walk you through provisioning a Linux virtual machine (VM) with Nginx web server installed and running on Amazon Web Services (AWS).

Prerequisites

An AWS account with appropriate permissions.
Familiarity with basic Linux terminal commands.

Step 1: Create a Virtual Private Cloud (VPC)

Log in to the AWS Management Console.
In the search bar, type “VPC” and select the “Create VPC” option.

Choose “VPC with a Single Public Subnet” under “VPC and More”.
Provide a name for your VPC (e.g., “My-Nginx-VPC”).
Keep the default number of Availability Zones (1).
Leave the number of public subnets at 1 and private subnets at 0 (we won’t need private subnets for this basic deployment).

Select “None” for NAT Gateways and VPC Endpoints.
Review your configuration and click “Create VPC” to provision the VPC.

Step 2: Launch an Amazon EC2 Instance

Once the VPC is created, navigate to the EC2 service by searching for “EC2” in the search bar.

Click on “Launch Instance”.

Choose a name for your instance (e.g., “My-Nginx-VM”).
Select “Amazon Linux” as the platform and choose an appropriate Amazon Machine Image (AMI), such as “Amazon Linux 2 AMI 2023.0”.
Under “Instance Type”, select an appropriate instance type based on your expected traffic and workload.
Scroll down to the “Storage” section and choose an appropriate volume size for your needs.

In the “Key Pair” section, select “Create a new key pair”. Provide a name for the key pair (e.g., “My-Nginx-Key”) and download the private key file securely. You’ll need this file to connect to your instance later. Click “Create key pair”.

Under “Network”, select the VPC you created earlier (e.g., “My-Nginx-VPC”). The subnet should default to the public subnet within the VPC.
Ensure “Auto-Assign Public IP” is selected.

Step 3: Configure Security Group Rules

Click on “Configure Security Group”.
Click “Edit” to modify the inbound security group rules.
Click “Add Rule”.
Select “HTTP” for the “Type” and “0.0.0.0/0” for the “Source” (this allows HTTP traffic from anywhere).
Click “Add Rule” again and select “SSH” for the “Type” and your desired source IP range for “Source” (e.g., your local IP address or CIDR block). This allows SSH access to your instance.

Review your security group rules and click “Review and Launch”.
On the review page, you can optionally assign a name tag to your instance for better identification. Click “Launch”.
Select the existing key pair you created earlier (e.g., “My-Nginx-Key”) and click “Launch Instances”.

Step 4: Connect to your EC2 Instance and Install Nginx

Wait for your instance to launch and reach a running state.

In the EC2 dashboard, select your instance and navigate to the “Connect” tab.

Choose “EC2 Instance Connect” and click “Connect”. This method avoids manual configuration of SSH access.

Download the private key file you generated earlier (ensure it’s kept secure). When prompted, provide the path to your private key file.
Once connected, switch to the root user by running sudo su.
Update the package list using yum update.

Install Nginx using yum install nginx.
Type y and press Enter to confirm the installation.
Start the Nginx service using sudo systemctl start nginx.

Step 5: Verify Nginx Installation

To find your instance’s public IP address, navigate back to the EC2 dashboard and locate the Public IPv4 Address under your instance details.
Open a web browser on a different computer and paste the public IP address of your instance into the address bar.
If Nginx is running correctly, you should see the default Nginx welcome page displayed in the browser.

Step 6: Clean Up (Optional)

It’s important to clean up resources after you’re finished to avoid incurring unnecessary charges.
Stop your EC2 instance using the AWS Management Console or AWS CLI if you no longer need it.
Optionally, you can terminate the EC2 instance to completely remove it from your AWS account.
Delete the associated security group and key pair if they are no longer needed.
If you created any additional resources during the setup process, such as IAM roles or S3 buckets, remember to delete them as well.
Review your AWS billing dashboard to ensure that all resources have been properly terminated and that there are no unexpected charges.

By following these cleanup steps, you can ensure that you’re only paying for the AWS resources you actually use, saving you money in the long run.