DEV Community

Cover image for 🛠️ Deploying a LAMP Stack on AWS EC2 (Ubuntu 24.04 LTS) — A DevOps Implementation Guide
Maxwell Ugochukwu
Maxwell Ugochukwu

Posted on • Edited on

🛠️ Deploying a LAMP Stack on AWS EC2 (Ubuntu 24.04 LTS) — A DevOps Implementation Guide

Introduction:

In today’s digital-first world, building robust and scalable web applications requires a reliable and efficient backend infrastructure — and that’s where the LAMP stack shines.

The LAMP stack is a powerful combination of four open-source technologies: Linux (operating system), Apache (web server), MySQL (database), and PHP (scripting language). Together, they form a foundational framework for developing high-performance web applications from simple websites to complex enterprise solutions. Thanks to their open-source nature, these technologies are not only cost-effective but also backed by a vast global developer community.

But why is LAMP still a popular choice in an age of modern frameworks and cloud-native stacks?

Because it’s flexible, proven, and efficient. It offers a tried-and-tested development environment that significantly reduces setup time, supports dynamic content, and is easily adaptable to cloud platforms like Amazon Web Services (AWS).

In this blog, we’ll break down how the LAMP stack works, explore its layered architecture, and walk you through the process of deploying a fully functional LAMP stack on AWS. Whether you're a developer looking to migrate your application to the cloud or a beginner seeking hands-on experience in web server configuration, this guide will help you master the fundamentals — and go beyond.

Let’s dive in and get your web stack cloud-ready! ☁️💡

🔧 Before You Begin: Essential Setup Checklist

If you’re just getting started with deploying your own LAMP stack on AWS, it’s important to have the right tools in place. Here’s a step-by-step preparation guide to ensure your local development environment is ready for action.

✅ 1. Install Windows Terminal(If you are using a Windows Operating System)
Windows Terminal provides a modern, fast, and powerful terminal experience that supports multiple shells (e.g., PowerShell, Command Prompt, WSL, etc.).
➡️ Download Windows Terminal from Microsoft Store

✅ 2. Install Visual Studio Code (VS Code)
VS Code is a lightweight yet powerful code editor with tons of extensions for developers.
➡️ Download VS Code

✅ 3. Set Up a Workspace Folder
Create a dedicated folder on your machine where you’ll manage all your project files. You can name it something like lamp-stack-aws.
Open this folder with VS Code and save it as a workspace for quick access.

✅ 4. Install Git
Git is essential for version control and integrating with GitHub.
➡️ Download Git for Windows

Once installed, you can verify by running the following in your terminal:

git --version
Enter fullscreen mode Exit fullscreen mode

✅ 5. Install OpenSSH Server
OpenSSH enables secure communication with your cloud instances via the SSH protocol. Windows 10/11 often includes it, but if it’s not installed:

  1. Open Settings > Apps > Optional Features
  2. Click Add a feature, search for OpenSSH Server, and install it.
  3. Then enable and start it using PowerShell
Get-Service -Name sshd
Start-Service -Name sshd
Enter fullscreen mode Exit fullscreen mode

✅ 6. Create Your AWS and GitHub Accounts
You’ll need both accounts to deploy and manage your LAMP stack in the cloud and store your project code.

  • 🔐 AWS Account (includes 12 months of free tier): 👉 Sign up for AWS
  • 🧑‍💻 GitHub Account (for version control and DevOps practices): 👉 Sign up for GitHub

🔍 With these tools in place, you’re now fully equipped to begin building your LAMP stack in the AWS cloud!

🚀 Launch EC2 and Deploy the LAMP Stack on AWS

Now that your local environment is set up, let’s move to the cloud. The LAMP stack (Linux, Apache, MySQL, PHP) will be deployed on an Amazon EC2 instance — a virtual server in AWS.


🖥️ Step 1: Launch Your EC2 Instance

- Log in to the AWS Console
👉 https://console.aws.amazon.com
- Navigate to EC2 Dashboard
- Click Services > EC2 or use the search bar to search for EC2.
- Click Launch Instance.

- Give your instance a name like lamp-server and choose an Amazon Machine Image (AMI): Select Ubuntu Server 24.04 LTS (HVM) – Free tier eligible.

- Choose Instance Type: Select t2.micro (Free tier eligible).

- Configure Instance Details. Leave default settings for beginners.

- Select Create a new key pair, download the .pem file securely.

- Configure Security Group: Create a new security group.

Add these inbound rules:

  • SSH – Port 22 – Anywhere (0.0.0.0/0)
  • HTTP – Port 80 – Anywhere (0.0.0.0/0)
  • (Optional) Add HTTPS if you plan to use SSL later.

- Launch Your Instance: Click Launch Instance.

- Click the instance ID to enter the ec2

🌍 Step 2: Assign an Elastic IP (Optional but Recommended)

Elastic IPs ensure your EC2 instance keeps a static public IP.

- Go to EC2 > Elastic IPs.

- Allocate a new Elastic IP and associate it with your instance.

🔐 Step 3: Connect to EC2 via SSH

Once your EC2 instance is up and running, the next crucial step is to connect to it securely from your local machine. This connection is established via SSH (Secure Shell), a cryptographic network protocol that allows you to control and manage your server remotely through the command line.

Before you connect, ensure you have the following:

  • Your EC2 instance's public IP address (or Public DNS)
  • The .pem key pair file you downloaded when launching your EC2 instance
  • A terminal (Windows users can use Windows Terminal, Windows Subsystem for Linux or Git Bash; Linux/macOS users can use their default terminal)

🛠️ Make the PEM file secure (important!)
Before connecting, change the permission of your PEM file to ensure it's not publicly viewable. Run the following command in your terminal:

chmod 400 path/to/your-key.pem
Enter fullscreen mode Exit fullscreen mode

Replace path/to/your-key.pem with the actual path to your PEM file.

🖥️ Connect using SSH
Run the following command in your terminal to connect to the EC2 instance:

ssh -i "your-key.pem" ubuntu@your-ec2-public-ip
Enter fullscreen mode Exit fullscreen mode
  • your-key.pem: the name of your private key file
  • ubuntu: the default username for Ubuntu-based EC2 instances
  • your-ec2-public-ip: the public IPv4 address of your EC2 instance You can as well get this command from your aws management console when you click on connect and chose SSH from your EC2 instance.

If everything is set up correctly, you’ll see a welcome message from Ubuntu, and your terminal prompt will change to something like:

ubuntu@ip-172-31-32-224:~$
Enter fullscreen mode Exit fullscreen mode

This means you’re now inside your EC2 server and can begin installing your web stack or managing files as needed.

✅ For WSL (Windows Subsystem for Linux) Users

Your .pem key can be accessed via:

cd /mnt/c/Users/<YourWindowsUsername>/Downloads
Enter fullscreen mode Exit fullscreen mode

Copy to home and set permissions:

cp /mnt/c/Users/<YourWindowsUsername>/Downloads/your-key.pem ~/
chmod 400 ~/your-key.pem
Enter fullscreen mode Exit fullscreen mode


Installing Apache2 on Your Ubuntu EC2 Instance

Now that your EC2 instance is up and running, the next step is to install Apache2, the web server component of the LAMP stack. Apache2 is responsible for serving your website’s files to users via their web browsers.

📌 Step-by-Step Guide

1. Update your package list
Before installing any new software, it’s good practice to update the local package index to make sure you're getting the latest version available:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

2. Install Apache2
This command installs the Apache2 web server:

sudo apt install apache2 -y
Enter fullscreen mode Exit fullscreen mode

3. Check if Apache2 is running
After installation, verify that Apache is active and running:

sudo systemctl status apache2
Enter fullscreen mode Exit fullscreen mode

You should see output indicating that the service is active (running) like above. If not, you can start it manually using:

sudo systemctl start apache2
Enter fullscreen mode Exit fullscreen mode

4. Test your server locally
Confirm that Apache is serving pages by using curl:

curl http://localhost:80
Enter fullscreen mode Exit fullscreen mode

You should see the default Apache2 Ubuntu welcome page content.

5. Access the web server in your browser.

Finally, open your browser and visit your EC2 public IP address. For example: http://13.134.19.83/

Confirm that your EC2 instance is running and the Apache2 service is active.

If the page still doesn’t load, try restarting Apache:

sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

Install and Configure MySQL on Your LAMP Stack

With Apache up and running, the next critical component of the LAMP stack is MySQL. It serves as the relational database management system (RDBMS) that stores and manages the backend data for your web applications.

⚙️ Install MySQL

sudo apt install mysql-server
Enter fullscreen mode Exit fullscreen mode

After installation, you can access the MySQL interface using:

sudo mysql
Enter fullscreen mode Exit fullscreen mode

🔐 Secure Your MySQL Installation

Before proceeding further, it's essential to secure your database with a strong root password:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'PassWord.1';
Enter fullscreen mode Exit fullscreen mode

Exit, then run the MySQL secure installation script:

sudo mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

This will prompt you with a series of questions to remove test databases, disable remote root login, and reload privilege tables to which you should reply yes for all questions.

You can now log into MySQL using:

sudo mysql -p
Enter fullscreen mode Exit fullscreen mode

Type your password when prompted.

To exit MySQL:

exit
Enter fullscreen mode Exit fullscreen mode


Install and Verify PHP

PHP is the scripting language that integrates the web server (Apache) with the database (MySQL).

🚀 Install PHP and Required Modules

sudo apt install php libapache2-mod-php php-mysql
Enter fullscreen mode Exit fullscreen mode

📃 Verify PHP Installation

php -v
Enter fullscreen mode Exit fullscreen mode

This should return the PHP version information, confirming that PHP is installed.


Host Your Web Project

Next, create a directory where your web project will live:

sudo mkdir /var/www/projectlamp
sudo chown -R $USER:$USER /var/www/projectlamp/
Enter fullscreen mode Exit fullscreen mode

This ensures you have permission to manage files in that directory.


Configure Apache for Your Project

Create a custom virtual host configuration for your projectlamp site:

sudo nano /etc/apache2/sites-available/projectlamp.conf
Enter fullscreen mode Exit fullscreen mode

Paste the following configuration:

<VirtualHost *:80>
        ServerName projectlamp
        ServerAlias www.projectlamp
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/projectlamp

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Save and exit the editor.

Enable your new site and disable the default one:

sudo a2ensite projectlamp
sudo a2dissite 000-default
Enter fullscreen mode Exit fullscreen mode

Test the configuration:

sudo apache2ctl configtest
Enter fullscreen mode Exit fullscreen mode

Then reload Apache:

sudo systemctl reload apache2
Enter fullscreen mode Exit fullscreen mode


Create and Test Web Pages

🌐 Add a Simple HTML Page

Navigate to the project directory:

cd /var/www/projectlamp
Enter fullscreen mode Exit fullscreen mode

Create a basic HTML file:

nano index.html
Enter fullscreen mode Exit fullscreen mode

Paste:

<!DOCTYPE html>
<html>
  <head>
    <title>Hello LAMP</title>
  </head>
  <body>
    <h1>Hello LAMP from hostname</h1>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Save and exit. You can now view this page using your EC2 public IP:

http://<your-ec2-public-ip>
Enter fullscreen mode Exit fullscreen mode


Enable PHP as the Default Page

Apache usually serves index.html by default. To give priority to index.php, modify the configuration file:

sudo nano /etc/apache2/mods-enabled/dir.conf
Enter fullscreen mode Exit fullscreen mode

Find this line:

DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
Enter fullscreen mode Exit fullscreen mode

Change it to:

DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
Enter fullscreen mode Exit fullscreen mode

Reload Apache:

sudo systemctl reload apache2
Enter fullscreen mode Exit fullscreen mode

Create a PHP Info Page

This will help confirm that PHP is correctly set up.

nano /var/www/projectlamp/index.php
Enter fullscreen mode Exit fullscreen mode

Paste:

<?php
phpinfo();
?>
Enter fullscreen mode Exit fullscreen mode

Save and exit. Load your EC2 IP again and you should see a PHP configuration page.

To remove the PHP info page for security reasons after verification:

sudo rm /var/www/projectlamp/index.php
Enter fullscreen mode Exit fullscreen mode


With this, you've successfully completed the setup of a LAMP Stack on AWS EC2! You now have a fully functional web server environment ready to host dynamic web applications.


🔧 LAMP Stack Troubleshooting Guide (Real Scenarios)

Based on actual issues I encountered during deployment, here are common errors and their fixes:

❌ Permission Denied (publickey)

  • Ensure your .pem file path is correct and has chmod 400 permissions.
  • Example fix:
chmod 400 ~/your-key.pem
Enter fullscreen mode Exit fullscreen mode

❌ Host Key Verification Failed

Occurs if you’ve previously connected to a server with the same IP.

  • Fix:
ssh-keygen -R <your-ec2-public-ip>
Enter fullscreen mode Exit fullscreen mode

❌ Apache Service Not Found

  • You may have tried using:
sudo systemctl start httpd
Enter fullscreen mode Exit fullscreen mode

But on Ubuntu, use:

sudo systemctl start apache2
Enter fullscreen mode Exit fullscreen mode

❌ MySQL Access Denied (Password Yes/No)

  • Make sure you've correctly run:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'PassWord.1';
Enter fullscreen mode Exit fullscreen mode
  • If MySQL socket is missing:
sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld
Enter fullscreen mode Exit fullscreen mode

❌ DirectoryIndex Still Loads HTML First

Even after modifying dir.conf, Apache might still serve index.html. Ensure index.php exists in the directory and reload Apache.

sudo systemctl reload apache2
Enter fullscreen mode Exit fullscreen mode

Top comments (0)