Deploying a web server manually can be time-consuming and prone to human error. Whether it’s installing Apache, configuring MySQL, or setting up PHP dependencies — doing it repeatedly for every environment is inefficient.
That’s why I decided to automate the entire LAMP (Linux, Apache, MySQL, PHP) setup using Ansible. This project demonstrates how to use Ansible to provision, configure, and verify a LAMP stack on an AWS EC2 Ubuntu instance, all with a single command.
By the end of this tutorial, you’ll have a fully automated, production-ready LAMP stack — ideal for developers, DevOps beginners, and anyone looking to streamline server deployments.
What Is a LAMP Stack?
The LAMP stack is one of the most common and reliable web server architectures used across the internet.
L – Linux: The operating system (Ubuntu in our case)
A – Apache: Web server that serves your website content
M – MySQL: Database to store your web application data
P – PHP: Server-side scripting language to process dynamic content
Together, they create a stable, powerful foundation for hosting web applications — especially WordPress, Joomla, and PHP-based sites.
Tools & Technologies Used
- AWS EC2 (Ubuntu 22.04) ---> Cloud instance hosting
- Ansible ---> Automation and configuration management
- Apache ---> Web server
- MySQL ---> Database server
- PHP ---> Scripting language
- Git & GitHub ---> Version control and project documentation
- macOS Terminal ---> Used as the Ansible control node
Step-by-Step Workflow
1️⃣ Launch an AWS EC2 Instance
Use Ubuntu 22.04 AMI.
Create a new key pair (e.g.,
lamp-key.pem).Open inbound rules for ports 22 (SSH), 80 (HTTP), and 443 (HTTPS).
2️⃣ Configure SSH Access
chmod 400 lamp-key.pem
ssh -i lamp-key.pem ubuntu@<EC2-Public-IP>
3️⃣ Install Ansible on Control Node
brew install ansible ansible --version
4️⃣ Create an Inventory File
[lamp]<EC2-Public-IP> ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/lamp-key.pem
5️⃣ Write the LAMP Ansible Playbook
-
name: Deploy LAMP Stack
hosts: lamp
become: true
tasks:- name: Update packages apt: update_cache: yes
- name: Install Apache apt: name: apache2 state: present
- name: Install PHP and modules
apt:
name:
- php
- libapache2-mod-php
- php-mysql state: present
- name: Install MySQL apt: name: mysql-server state: present
- name: Ensure Apache is running service: name: apache2 state: started enabled: true
6️⃣ Run the Playbook
ansible-playbook -i inventory lamp.yaml
7️⃣ Verify the Setup
- Visit your EC2 public IP in a browser → You should see the Apache Welcome Page.
- Create a PHP test file:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
- Open
http://<EC2-IP>/info.phpto confirm PHP is working.
Troubleshooting & Solutions
EC2 unreachable ---> Ansible
UNREACHABLEerror ---> Check key permissions and open SSH port 22MySQL Error 2002 ---> Service not running ---> Restart MySQL:
sudo systemctl restart mysqlAnsible skipped tasks ---> Wrong inventory path ---> Correct path and verify hostnames
YAML syntax error ---> Incorrect spacing ---> Validate using
yamllintor online validator
What I Learned
How to automate web server setup using Ansible playbooks
How to manage EC2 instances with SSH and Ansible
Real-world debugging experience with YAML, MySQL, and Apache
How to document and version control DevOps projects in GitHub
About the Author
Alan Varghese
Learning topics covering RHCSA, MCSA, CCNA, CompTIA A+/Network+/Security+, AWS Solutions Architect
💼 Aiming for roles in DevOps, Cloud, and Cybersecurity
🔗 Portfolio:
GitHub: alanvarghese-dev
LinkedIn: linkedin.com/in/alanvarghese-dev
✉️ Email: thealanvarghese@gmail.com
Conclusion
Automating your LAMP stack deployment using Ansible on AWS not only saves time but also ensures consistency, scalability, and reliability.
With just one command, you can build a fully functional web server from scratch — ready for hosting websites, web apps, or WordPress installations.
“Automation isn’t about replacing human effort — it’s about amplifying it.”









Top comments (0)