DEV Community

Cover image for How to Setup a LAMP Server at Home Using AWS (Step-by-Step Guide)
Alan Varghese
Alan Varghese

Posted on

How to Setup a LAMP Server at Home Using AWS (Step-by-Step Guide)

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

  1. Go to the AWS Management Console โ†’ EC2.

  1. Click Launch Instance.

  2. Choose Ubuntu Server (latest LTS) as the AMI.

  3. Select t2.micro (free-tier eligible).

  4. Add storage (default 8GB is fine).

  1. Configure Security Group:
- Allow SSH (22) from your IP.

- Allow HTTP (80) from anywhere.

- Allow HTTPS (443) from anywhere.
Enter fullscreen mode Exit fullscreen mode
  1. 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)