Want to run your own website, blog, or project on the cloud? The LAMP stack (Linux, Apache, MySQL, PHP) is one of the most popular ways to host dynamic websites.In this guide, weโll walk through setting up a LAMP server using AWS EC2 which will give you your own cloud-based server that works the same as a home server but with the scalability and reliability of AWS.
๐ ๏ธ Prerequisites
An AWS account (sign up at aws.amazon.com).
Basic knowledge of the terminal and SSH.
A free-tier EC2 instance (weโll use Ubuntu).
SSH key pair (to log into your instance).
โ๏ธ Step 1: Launch an EC2 Instance
- Go to the AWS Management Console โ EC2.
Click Launch Instance.
Choose Ubuntu Server (latest LTS) as the AMI.
Select t2.micro (free-tier eligible).
Add storage (default 8GB is fine).
- Configure Security Group:
- Allow SSH (22) from your IP.
- Allow HTTP (80) from anywhere.
- Allow HTTPS (443) from anywhere.
- Launch and download the key pair (
.pem
file).
You should always save this .pem file for future use.
๐ Step 2: Connect to Your Instance
Use SSH to log in:
ssh -i your-key.pem ubuntu@(EC2-Public-IP)
๐ Step 3: Update Your System
sudo apt update && sudo apt upgrade -y
๐๏ธ Step 4: Install Apache
sudo apt install apache2 -y
Enable and start Apache:
sudo systemctl enable apache2
sudo systemctl start apache2
Now, check in a browser:
http://(EC2-Public-IP)
You should be able see the default Apache page.
๐๏ธ Step 5: Install MySQL (I used mariadb)
sudo apt install mysql-server -y
Secure installation:
sudo mysql_secure_installation
Set a root password and remove insecure defaults.
Enable MySQL service:
sudo systemctl start mariadb
sudo systemctl enable mariadb
๐ป Step 6: Install PHP
sudo apt install php libapache2-mod-php php-mysql -y
To check the version:
php -v
๐งช Step 7: Test PHP
Create a PHP test file:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
To test your database page, follow the syntax of the below screenshot and replace it with your credentials.
Open in browser to see the PHP_MySQL Database page:
http://(EC2-Public-IP)/dbtest.php
Open in browser to see the PHP configuration page:
http://(EC2-Public-IP)/info.php
๐ Step 8: (Optional but recommended) Secure with Firewall
Enable UFW and allow required ports:
sudo ufw allow OpenSSH
sudo ufw allow 'Apache Full'
sudo ufw enable
Now youโve successfully completed setting up a LAMP server on AWS EC2 instance! ๐ you can now:
Host your own projects.
Deploy WordPress or other CMS.
Experiment with databases and web hosting.
๐ Next Steps
Attach a domain name to your server.
Secure your site with Letโs Encrypt SSL.
Automate deployment using Ansible.
Hope you like my guide and all the best for your journey ahead. Thankyou.
Top comments (0)