Introduction to AWS EC2 and CentOS Setup

AWS EC2 (Elastic Compute Cloud) is a popular choice for hosting websites or applications on the cloud due to its scalability, flexibility, and ease of use. This guide will walk you through setting up an Apache Web Server on a CentOS instance using AWS EC2. CentOS, a free and open-source Linux distribution, is widely preferred for its stability and security, making it an excellent choice for web servers.

Launching an EC2 Instance and Selecting CentOS

The first step to setting up your Apache web server is launching an EC2 instance. Here’s how you do it:

  1. Log into AWS Management Console: Navigate to the EC2 dashboard.
  2. Launch an Instance: Click the “Launch Instance” button to create a new EC2 instance.
  3. Choose an Amazon Machine Image (AMI): Search for and select a CentOS AMI. CentOS is not a default option, but you can find it in the AWS Marketplace.
  4. Choose Instance Type: Select the appropriate instance type based on your needs. A t2.micro instance is ideal for small-scale use cases and eligible for AWS Free Tier.
  5. Configure Instance Details: In this step, you can customize network settings and other instance-related configurations.
  6. Add Storage: Add additional storage if needed.
  7. Add Tags: You can tag your instance to keep track of its purpose and resources.
  8. Configure Security Groups: This step is crucial and will be detailed in the next section.

Configuring Network Settings and Security Groups

Security groups act as virtual firewalls that control traffic to your instance. To enable HTTP (for Apache) and SSH access, configure the following:

  1. SSH Access: Add a rule that allows inbound traffic over port 22 (SSH). Set the source to your IP address or anywhere (0.0.0.0/0), depending on your preference, but limit access to trusted IPs for security purposes.
  2. HTTP Access: Add another HTTP rule by allowing inbound traffic over port 80. This will ensure that users can access your web server once Apache is running.

Connecting to the EC2 Instance via SSH

Once your EC2 instance is running, the next step is to connect to it:

  1. Locate your key pair: During the instance launch process, you’ll either create a new key pair or use an existing one. Make sure you have access to the .pem file.
  2. Connect via SSH: Use the following command to SSH into your instance from your terminal:
    ssh -i /path/to/keypair.pem centos@<your-ec2-public-ip>

Replace /path/to/keypair.pem with the location of your key pair and <your-ec2-public-ip> with the public IP address of your EC2 instance.

Updating the Server and Installing Apache

Now that you’re connected to your instance, it’s essential to update the system and install Apache:

  1. Update the CentOS Packages:
    sudo yum update -y

This ensures that all system packages are up-to-date.

  1. Install Apache (httpd):
    sudo yum install httpd -y

This command installs the Apache HTTP server on your CentOS instance.

Starting and Enabling the Apache Server

Once Apache is installed, you’ll need to start the service and enable it to run automatically at startup:

  1. Start Apache:
    sudo systemctl start httpd
  2. Enable Apache to Start on Boot:
    sudo systemctl enable httpd

With these commands, Apache will start serving your web pages as soon as the server boots up.

Verifying Apache Installation and Accessibility

To verify that Apache is installed correctly and accessible:

  1. Check Apache Status:
    sudo systemctl status httpd

This command will show whether Apache is running.

  1. Test Accessibility: Open a web browser and enter your EC2 instance’s public IP address. If Apache is running successfully, you should see the default Apache welcome page.
  2. Firewall Configuration (if necessary): If you still can’t access the server, ensure that the firewall allows HTTP traffic:
    sudo firewall-cmd –permanent –add-service=http

sudo firewall-cmd –reload

Once Apache is running, you can start uploading your website files to the /var/www/html/ directory and begin serving content to your users.

References

Install a web server on your EC2 instance

Apache Web Server + MySQL Server + phpMyadmin on CentOS Server 9