How to Set Up a LAMP Stack on AWS EC2 (Step-by-Step Guide)

A LAMP stack is one of the most widely used setups for hosting dynamic websites.

LAMP stands for:

  • Linux
  • Apache
  • MySQL
  • PHP

In this complete guide, you will learn how to set up a LAMP stack on AWS EC2 step-by-step, including optimization and security best practices.


Prerequisites

Before you begin, make sure you have:

  • AWS account
  • EC2 instance (Ubuntu recommended)
  • SSH access
  • Basic Linux knowledge

Step 1: Launch EC2 Instance

  • Choose Ubuntu AMI
  • Select instance type (t2.micro for beginners)
  • Configure security group (allow HTTP, HTTPS, SSH)

Step 2: Connect to EC2 via SSH

ssh -i key.pem ubuntu@your-public-ip

Step 3: Update System

sudo apt update
sudo apt upgrade -y

Step 4: Install Apache Web Server

sudo apt install apache2 -y

Test:
Open http://your-ip


Step 5: Install MySQL

sudo apt install mysql-server -y
sudo mysql_secure_installation

Step 6: Install PHP

sudo apt install php libapache2-mod-php php-mysql -y

Step 7: Test PHP Setup

sudo nano /var/www/html/info.php

Add:

<?php phpinfo(); ?>

Open in browser:
http://your-ip/info.php


Step 8: Configure Apache Virtual Host

Create config file:

sudo nano /etc/apache2/sites-available/yourdomain.conf

Step 9: Enable Site

sudo a2ensite yourdomain.conf
sudo systemctl reload apache2

Security Best Practices

  • Disable root login
  • Use firewall (ufw)
  • Keep system updated

Performance Optimization

  • Enable caching
  • Use CDN
  • Optimize Apache settings

Common Issues

  • Apache not starting
  • PHP not working
  • Permission errors

Pro Tips

  • Use Elastic IP
  • Set up SSL using Let’s Encrypt
  • Use monitoring tools

FAQs

Is LAMP still relevant?

Yes, widely used for WordPress and PHP apps.

Can I use Nginx instead?

Yes (LEMP stack)


Conclusion

You have successfully set up a LAMP stack on AWS EC2. With proper configuration, your server is ready for production use.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *