How to Deploy an Apache Web Server on AWS EC2: Complete Beginner’s Guide (2026)

Table of Contents

  1. Introduction to AWS EC2 and Apache
  2. Prerequisites for Cloud Hosting
  3. Step 1: Launching an AWS EC2 Instance (Ubuntu Linux)
  4. Step 2: Configuring AWS Security Groups (HTTP/HTTPS Access)
  5. Step 3: Connecting to EC2 via SSH / Instance Connect
  6. Step 4: Installing Apache Web Server
  7. Step 5: Testing and Managing the Apache Service
  8. Step 6: Setting Up Virtual Hosts for Custom Domains
  9. Step 7: Attaching an AWS Elastic IP for Permanent Address
  10. Step 8: Securing Your Web Server with Free Let’s Encrypt SSL
  11. Step 9: Cloudflare DNS Integration and Performance Tweaks
  12. Step 10: Troubleshooting Common Errors (Connection Timeouts & 5xx)
  13. Conclusion and Best Practices

1. Introduction to AWS EC2 and Apache

Cloud infrastructure has revolutionized how websites and modern applications are hosted. Today, instead of buying expensive physical hardware or relying on traditional shared hosting platforms with strict resource limits, developers prefer cloud infrastructure like Amazon Web Services (AWS). Among AWS services, Amazon Elastic Compute Cloud (EC2) stands out as a highly scalable and flexible virtual computing platform.

If you want to host a fast, reliable, and production-ready website, learning how to deploy an Apache web server on AWS EC2 is one of the most fundamental skills you can acquire. Apache HTTP Server, maintained by the Apache Software Foundation, remains one of the world’s most trusted, modular, and reliable web server software platforms.

In this comprehensive 2026 guide, you will learn the exact step-by-step process to deploy an Apache web server on AWS EC2 from scratch. Whether you are launching a personal technology blog, a freelance portfolio, or an enterprise application, this tutorial provides all the practical commands and cloud architecture concepts you need.

2. Prerequisites for Cloud Hosting

Before you begin to deploy an Apache web server on AWS EC2, ensure you have the following basic prerequisites ready:

  • AWS Free Tier / Active Account: An active Amazon Web Services account. (You can create one at AWS Official Portal).
  • Custom Domain Name: A registered domain name (e.g., faiztech.online) from Namecheap, GoDaddy, or Cloudflare DNS.
  • Basic Command-Line Familiarity: Basic knowledge of Linux terminal commands (sudo, apt, nano, systemctl).
  • SSH Client or Web Terminal: Terminal access on Windows (PuTTY, PowerShell) or Linux/macOS, or AWS EC2 Instance Connect directly in the browser.

3. Step 1: Launching an AWS EC2 Instance (Ubuntu Linux)

The first core phase to deploy an Apache web server on AWS EC2 is creating and configuring your virtual machine (EC2 instance).

Step-by-Step EC2 Launch Process:

  1. Log in to your AWS Management Console.
  2. Navigate to the EC2 Dashboard by typing EC2 in the top search bar.
  3. Click the orange Launch Instance button.
  4. Name Your Instance: Give your server a clear tag, such as FaizTech-Apache-Server.
  5. Choose Amazon Machine Image (AMI): Select Ubuntu Server 24.04 LTS or Ubuntu 22.04 LTS (Free Tier eligible). Ubuntu provides a stable and beginner-friendly Linux environment.
  6. Choose Instance Type: Select t2.micro or t3.micro (1 vCPU, 1 GiB Memory). These instances fall under the AWS Free Tier usage limit.
  7. Key Pair (Login Credentials):
    • Select an existing key pair (.pem or .ppk).
    • Or click Create new key pair, name it faiztech-key, select RSA, and download the .pem file to a safe location on your computer.

4. Step 2: Configuring AWS Security Groups (HTTP/HTTPS Access)

A crucial security layer when you deploy an Apache web server on AWS EC2 is setting up the AWS Security Group. A Security Group acts as a virtual firewall controlling inbound and outbound network traffic.

Configuring Inbound Rules:

In the Network settings section during instance launch (or via EC2 > Security Groups):

  1. SSH Access (Port 22):
    • Type: SSH
    • Port Range: 22
    • Source: My IP (Recommended for security) or Anywhere (0.0.0.0/0) for testing.
  2. HTTP Access (Port 80):
    • Type: HTTP
    • Port Range: 80
    • Source: Anywhere (0.0.0.0/0) (Allows web visitors to view your website over unencrypted HTTP).
  3. HTTPS Access (Port 443):
    • Type: HTTPS
    • Port Range: 443
    • Source: Anywhere (0.0.0.0/0) (Allows secure encrypted traffic via SSL/TLS).

If you forget to open Port 80 or Port 443, visitors will encounter connection timeout errors when trying to reach your web server.

Click Launch Instance and wait 1–2 minutes until the instance state changes to Running.

5. Step 3: Connecting to EC2 via SSH / Instance Connect

Once the virtual machine is online, connect to its terminal environment before attempting to deploy an Apache web server on AWS EC2.

Method 1: AWS EC2 Instance Connect (Easiest)

  1. Go to EC2 Dashboard > Instances.
  2. Select your running instance (FaizTech-Apache-Server).
  3. Click the Connect button at the top right.
  4. Choose EC2 Instance Connect and click Connect. A terminal window opens directly in your web browser.

Method 2: Connection via SSH Terminal (PowerShell / macOS Terminal)

Open your computer terminal, navigate to the folder where your .pem key was downloaded, and run:

Bash

chmod 400 faiztech-key.pem
ssh -i "faiztech-key.pem" ubuntu@<YOUR-PUBLIC-IP-ADDRESS>

6. Step 4: Installing Apache Web Server

With server terminal access ready, it is time to perform the core configuration to deploy an Apache web server on AWS EC2.

Step 1: Update Linux Package Lists

First, update the local APT package index to fetch the latest software versions and security patches:

Bash

sudo apt update && sudo apt upgrade -y

Step 2: Install Apache HTTP Server

Run the standard package installation command for Apache:

Bash

sudo apt install apache2 -y

This command automatically downloads Apache and creates system service files (apache2.service).

7. Step 5: Testing and Managing the Apache Service

Once installation completes, verify that the web server process is active.

Check Service Status:

Bash

sudo systemctl status apache2

You should see an output containing active (running) in green text.

Useful Apache Control Commands:

  • Stop Apache: sudo systemctl stop apache2
  • Start Apache: sudo systemctl start apache2
  • Restart Apache: sudo systemctl restart apache2
  • Reload Configuration: sudo systemctl reload apache2
  • Enable Auto-start on Boot: sudo systemctl enable apache2

Testing in Browser:

Find your EC2 instance’s Public IPv4 address from the AWS Console (e.g., 16.171.238.132). Open a web browser tab and visit:

http://<YOUR-PUBLIC-IP-ADDRESS>

You will see the default Ubuntu Apache2 Web Page. This confirms that you have successfully managed to deploy an Apache web server on AWS EC2!

8. Step 6: Setting Up Virtual Hosts for Custom Domains

Running a default landing page is fine for testing, but production websites require custom domain mapping (e.g., faiztech.online). Apache uses Virtual Hosts to serve multiple domain names from a single web server instance.

Step 1: Create Web Directory Structure

Create a dedicated root directory for your domain:

Bash

sudo mkdir -p /var/www/faiztech.online/html

Set file ownership permissions so your user account can edit files:

Bash

sudo chown -R $USER:$USER /var/www/faiztech.online/html
sudo chmod -R 755 /var/www

Step 2: Create a Sample Index Page

Create a basic HTML file using nano:

Bash

nano /var/www/faiztech.online/html/index.html

Paste the following HTML code:

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Welcome to FaizTech Cloud Server</title>
</head>
<body>
    <h1>Success! Apache Web Server is Running on AWS EC2!</h1>
    <p>Hosted smoothly on Amazon Web Services with custom virtual host setup.</p>
</body>
</html>

Save and exit (Ctrl + O, Enter, then Ctrl + X).

Step 3: Create Apache Virtual Host File

Instead of modifying default files, create a new site configuration file:

Bash

sudo nano /etc/apache2/sites-available/faiztech.online.conf

Paste this Virtual Host configuration:

Apache

<VirtualHost *:80>
    ServerAdmin admin@faiztech.online
    ServerName faiztech.online
    ServerAlias www.faiztech.online
    DocumentRoot /var/www/faiztech.online/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 4: Enable the Virtual Host and Rewrite Engine

Enable your new Virtual Host configuration file and disable the default 000-default page:

Bash

sudo a2ensite faiztech.online.conf
sudo a2dissite 000-default.conf
sudo a2enmod rewrite

Test Apache configuration for syntax errors:

Bash

sudo apache2ctl configtest

If the terminal responds with Syntax OK, reload Apache to apply changes:

Bash

sudo systemctl reload apache2

This Virtual Host configuration is essential when you deploy an Apache web server on AWS EC2 for multi-site hosting environments.

9. Step 7: Attaching an AWS Elastic IP for Permanent Address

By default, standard AWS EC2 instances receive dynamic Public IPv4 addresses. Whenever you stop and start your instance, AWS reassigns a new IP address. If your DNS records point to the old IP, your website will break instantly.

To fix this, attach a static Elastic IP address (EIP).

How to Allocate and Attach Elastic IP:

  1. Open the AWS EC2 Console.
  2. Under Network & Security in the left sidebar, click Elastic IPs.
  3. Click Allocate Elastic IP address.
  4. Keep default settings (Amazon’s pool of IPv4 addresses) and click Allocate.
  5. Select the newly generated Elastic IP address, click Actions, and choose Associate Elastic IP address.
  6. Select your Instance (FaizTech-Apache-Server) and your instance’s Private IP.
  7. Click Associate.

Now your EC2 server holds a static IP address that never changes, ensuring long-term domain mapping reliability when you deploy an Apache web server on AWS EC2.

10. Step 8: Securing Your Web Server with Free Let’s Encrypt SSL

Modern web applications require encryption (HTTPS). Without an SSL/TLS certificate, web browsers label your site as “Not Secure,” leading to lost user trust and lower Google Search rankings.

You can issue a free, auto-renewing SSL certificate using Certbot from Let’s Encrypt.

Step 1: Install Certbot and Python Apache Plugin

Run the following installation command in your Ubuntu terminal:

Bash

sudo apt update
sudo apt install certbot python3-certbot-apache -y

Step 2: Request and Install SSL Certificate

Execute Certbot for your domain:

Bash

sudo certbot --apache -d faiztech.online -d www.faiztech.online

Follow the interactive prompts:

  • Enter an email address for renewal and security notifications.
  • Agree to the terms of service.
  • Choose whether to automatically redirect HTTP traffic to HTTPS (Select option 2 for automatic HTTP to HTTPS redirect).

Step 3: Verify Auto-Renewal

Let’s Encrypt certificates are valid for 90 days. Certbot installs a system background timer to renew certificates automatically. Test renewal with a dry run:

Bash

sudo certbot renew --dry-run

Installing SSL is one of the most critical security milestones when you deploy an Apache web server on AWS EC2.

11. Step 9: Cloudflare DNS Integration and Performance Tweaks

To enhance security, speed up global content delivery, and prevent Distributed Denial of Service (DDoS) attacks, integrate your server with Cloudflare DNS.

Cloudflare Setup Steps:

  1. Create a free Cloudflare account and add your domain (faiztech.online).
  2. Update your domain registrar’s Nameservers to point to Cloudflare’s assigned nameservers.
  3. In Cloudflare > DNS > Records, add an A Record:
    • Name: @ (or faiztech.online)
    • IPv4 Address: Your AWS Elastic IP address.
    • Proxy Status: Select DNS Only (Gray Cloud) during initial setup, or Proxied (Orange Cloud) if SSL is fully configured.

Important Note on Cloudflare Proxy vs. Server SSL:

If you enable Cloudflare Proxy (Orange Cloud) before installing SSL on your EC2 origin server, set Cloudflare SSL/TLS mode to Flexible. Once Certbot is fully installed on Apache, upgrade Cloudflare SSL mode to Full (Strict) to eliminate connection timeout loops (ERR_CONNECTION_TIMED_OUT).

12. Step 10: Troubleshooting Common Errors (Connection Timeouts & 5xx)

Even after learning how to deploy an Apache web server on AWS EC2, cloud administrators occasionally encounter connection errors. Here is how to diagnose and resolve them quickly:

1. Issue: ERR_CONNECTION_TIMED_OUT

  • Root Cause 1: AWS Security Group is blocking Port 80 or Port 443.
    • Fix: Check EC2 Security Group inbound rules and ensure 0.0.0.0/0 is permitted for HTTP/HTTPS.
  • Root Cause 2: Apache service has stopped running.
    • Fix: Run sudo systemctl restart apache2.
  • Root Cause 3: Cloudflare Proxy/SSL configuration mismatch.
    • Fix: Temporarily switch Cloudflare Proxy Status to DNS Only (Gray Cloud) to test raw server connectivity.

2. Issue: 502 Bad Gateway or 504 Gateway Timeout

  • Root Cause: Apache server taking too long to respond or PHP service (if installed) crashing.
  • Fix: Check Apache error logs using the command:Bashsudo tail -f /var/log/apache2/error.log

13. Conclusion and Best Practices

Congratulations! You have mastered the full workflow required to deploy an Apache web server on AWS EC2.

From launching an Ubuntu instance and configuring security groups to creating virtual hosts, attaching static Elastic IPs, and securing the connection with Let’s Encrypt SSL, your web infrastructure is now ready for production workloads.

Final Checklist for Maintainers:

  1. Always Use Elastic IP: Keep your public IP address static.
  2. Automate System Updates: Regularly execute sudo apt update && sudo apt upgrade -y to patch kernel vulnerabilities.
  3. Backup Server Configurations: Regularly create AWS EBS Snapshots or AMIs of your EC2 instance so you can restore your setup instantly in case of failure.
  4. Monitor Performance: Keep an eye on AWS CloudWatch CPU utilization graphs to scale instance sizes when web traffic increases.

By following this workflow, you can confidently deploy an Apache web server on AWS EC2 and manage professional, high-performance web applications with ease!

Leave a Comment