Skip to main content

How to Install LAMP Stack on AWS EC2 Instance Easy Way

· 5 min read
Kaustubh Kulkarni

In this tutorial we will see, How to Install LAMP Stack on AWS EC2 Instance?


Create AWS Account

If you don't have AWS account previously, You can create your aws account here.

Creating Instance

We are going to create Ubuntu 20.04 EC2 instance . To create EC2 instance click here.

Accessing Instance from your PC

If you don't know how to access VPS ( Virtual Private Server). Then please follow this guide.

This is written for digital ocean, but it will work on ec2 too. Except method #1 [from console]

Installing LAMP Stack

Now we are going to install LAMP Stack on EC2 instance, First you will need to login your aws account & copy the instance IP address.

Accessing Server from Putty

To connect , first we will need to generate Private Key (.ppk) from PuttyGen. (PuttyGen Comes with Putty App). Open PuttyGen & Click on Load after clicking , from file select window , click All Files and select valid key file , we downloaded from aws console , while creating EC2 instance.

Now click on Save private key . save it as (.ppk) file. How to Install LAMP Stack on AWS EC2 Instance?

Now open Putty App, fill below fields as per instructions.

Host Name (IP Address)



keep port 22 as it is.

Now from left menu , select Connection -> SSH -> Auth

Check for option Private Key for file authentication & select the file we generated in last step & Click on Open Button. How to Install LAMP Stack on AWS EC2 Instance?

Once everything added , Go to session windows again & in saved session add this session, So next time, you will not require do this stuff again. Now Click on Open button.

SSH will go you warning click on Allow / OK. How to Install LAMP Stack on AWS EC2 Instance?

Now you have access to your SSH.

Initial Server Setup

First thing we have to do once logged in is, switching user to root from ubuntu. To do that paste below command in terminal.


$ sudo -i

(You can just copy paste commmands, Just change them according to your requirements. While exectuing commands below if asked click "y " and hit enter)

$ apt update
$ apt upgrade

Above 2 commands will update all the apps in our EC2 Ubuntu instance.

$ apt autoremove

It will remove all the non-required packages.

Setting Up Sudo - User

$ adduser enter_username_here

Now fill complete details of user and once asked for confirmation type y and hit enter. How to Install LAMP Stack on AWS EC2 Instance?

Now we're going to add sudo / root privileges to this user , So the user can do tasks by using word sudo. To do that

$ usermod -aG sudo enter_username_here

Configuring Firewall

We need to configure firewall to prevent unauthorised access to our instance. By default the firewall is turned off .

$ ufw status

This command will tell you status for firewall.

$ ufw app list

It will show apps needs access to firewall. for example OpenSSH.

$ ufw allow OpenSSH
$ ufw enable


Installing Apache

Apache is web server we are going to use for serving our sites. To install Apache paste below commands and enter y when asked.

$ apt install apache2
$ service apache2 restart

We need to update firewall rules again & need to add "Apache" to allowed list. To allow execute below commands.

$ ufw allow "Apache Full"

Once done visit your EC2 IP address, You will see success apache configuration success page. as shown below, How to Install LAMP Stack on AWS EC2 Instance?

If you are unable to view above page, then that's not issue, We can update some setting and you will be able to see this page. To do that,


Click on the services in top corner -> EC2, after click on Security Groups & select the security group associated with our instance. After selecting Click on Edit Inboud rules,

Now make all the settings as shown in this image. How to Install LAMP Stack on AWS EC2 Instance?

Once rules updated you can visit your IP address and you can see that confirmation page.

Installing MySQL

To install Mysql use below commands.

$ apt install mysql-server

You can also install phpMyAdmin , if you need , but it's not required. To install PhpMyAdmin use below commands,

$ apt install phpmyadmin

You can access PhpMyAdmin at your_instance_ip_address/phpmyadmin. View Video Version of this tutorial for more details.

Installing php

Installing php is the last step, Let's complete that too,

$ apt install libapache2-mod-php php-mysql

enter y when asked, That's it. You just completed installation of LAMP Stack on your aws EC2 Ubuntu instance.

Looking for Alternative of EC2?

Get the $100 free credits by Digital Ocean for free. You can get $100 by clicking below button.

Try DigitalOcean & Get $100 Credits for FREE