DEV Community

Cover image for Deploying A Linux Virtual Machine with Nginx on AWS
Jimi
Jimi

Posted on • Updated on

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)

  1. Log in to the AWS Management Console.
  2. In the search bar, type "VPC" and select the "Create VPC" option.

    Image description

    Image description

  3. Choose "VPC with a Single Public Subnet" under "VPC and More".

  4. Provide a name for your VPC (e.g., "My-Nginx-VPC").

  5. Keep the default number of Availability Zones (1).

  6. Leave the number of public subnets at 1 and private subnets at 0 (we won't need private subnets for this basic deployment).

    Image description

  7. Select "None" for NAT Gateways and VPC Endpoints.

  8. Review your configuration and click "Create VPC" to provision the VPC.

    Image description

Step 2: Launch an Amazon EC2 Instance

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

    Image description

  2. Click on "Launch Instance".

    Image description

  3. Choose a name for your instance (e.g., "My-Nginx-VM").

  4. Select "Amazon Linux" as the platform and choose an appropriate Amazon Machine Image (AMI), such as "Amazon Linux 2 AMI 2023.0".

  5. Under "Instance Type", select an appropriate instance type based on your expected traffic and workload.

  6. Scroll down to the "Storage" section and choose an appropriate volume size for your needs.

    Image description

  7. 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".

    Image description

  8. Under "Network", select the VPC you created earlier (e.g., "My-Nginx-VPC"). The subnet should default to the public subnet within the VPC.

  9. Ensure "Auto-Assign Public IP" is selected.

Image description

Step 3: Configure Security Group Rules

  1. Click on "Configure Security Group".
  2. Click "Edit" to modify the inbound security group rules.
  3. Click "Add Rule".
  4. Select "HTTP" for the "Type" and "0.0.0.0/0" for the "Source" (this allows HTTP traffic from anywhere).
  5. 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.

    Image description

  6. Review your security group rules and click "Review and Launch".

  7. On the review page, you can optionally assign a name tag to your instance for better identification. Click "Launch".

  8. 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

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

    Image description

  2. In the EC2 dashboard, select your instance and navigate to the "Connect" tab.

    Image description

  3. Choose "EC2 Instance Connect" and click "Connect". This method avoids manual configuration of SSH access.

    Image description

  4. Download the private key file you generated earlier (ensure it's kept secure). When prompted, provide the path to your private key file.

  5. Once connected, switch to the root user by running sudo su.

  6. Update the package list using yum update.

    Image description

  7. Install Nginx using yum install nginx.

  8. Type y and press Enter to confirm the installation.

  9. Start the Nginx service using sudo systemctl start nginx.

    Image description

Step 5: Verify Nginx Installation

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

    Image description

Step 6: Clean Up (Optional)

  1. It's important to clean up resources after you're finished to avoid incurring unnecessary charges.
  2. Stop your EC2 instance using the AWS Management Console or AWS CLI if you no longer need it.
  3. Optionally, you can terminate the EC2 instance to completely remove it from your AWS account.
  4. Delete the associated security group and key pair if they are no longer needed.
  5. If you created any additional resources during the setup process, such as IAM roles or S3 buckets, remember to delete them as well.
  6. 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.

Top comments (0)