How to install LAMP on AWS EC2 with Ubuntu 18.04
In this article we are going to discuss how to install LAMP on AWS EC2 with Ubuntu 18.04 step-by-step.
Step 1 – Launch an AWS EC2 Virtual Machine
Log in into the AWS EC2 Management Console and click on the Launch Instance button.
Step 2 – Choose the OS and Hardware
After clicking on Launch Instance the below page will open. Here you have to choose OS for the virtual machine and hardware.
Select Ubuntu Server 18.04 LTS (HVM), SSD Volume Type.
After choosing OS and hardware we have to choose an instance type. We are going to choose t2.micro instance as it is free-tier eligible.
Step 3 – Configure Instance Details
When you will come on this page you will see the following screen.
In this step the most important thing is that the shutdown behavior we need to keep always Stop not Terminate, as we can see in the below image.
Now another important thing is Tenancy that we have to keep “Shared – Run a shared hardware instance”, if we choose other two options then additional charges may apply, so choose it carefully. But note that it is completely depends on our requirement, so based on our requirement we need to select the hardware we are going to use shared or dedicated .
Now next we will move onto User data, let consider we need to install some applications or install some packages during the launch process, we can also puts commands here so during launch packages also will get installed and system is ready to go.
Step 4 – Editing Some Basic Configurations
Go for the default settings for Configure Instance, Add Storage and Add Tags screens.
Initially AWS provide storage capacity of hard disk for storing applications is 8 GB but we can increase it up to 30 GB.
Step 5 – Adding Tags
Tags are very important thing, if there are more than one instances then to know which server is going to use for which purpose, for such conditions tags are very useful.
Step 6 – Configure Security Group
This is very important step. Below is the screenshot of this step is given
First of all we have to add security group name. We can write whatever we want to add, then we have to add description.
Now, Add 3 rules under Configure Security Group .
1. SSH : SSH is required to login to the virtual machine using command line.
2. HTTP : HTTP is for the web server.
If you want to add HTTP rule, you can add by clicking Add Rule button and choosing HTTP from the dropdown. Same for HTTPS.
Security groups acts as a virtual firewall according to AWS documentation. In other words we can set rules regarding to IP addresses that can access the added protocols. We need both the protocols are open to all IP addresses, we can also restrict accessing protocols from some IP addresses.
Clicking Review and Launch button we will redirect to a page where we can review our instance settings.
Step 7 – Creating a Key Pair
In this step page will represent with options for key file, which is uses for connect to the EC2 instance from your system.
Choose option Create a new key pair from the dropdown menu and download the key file.
After key file downloaded the Launch Instance button became active. This will be the last step to clicking button and launch the instance in AWS.
After successfully launch of the instance will redirect to the new page where success message will be displayed.
For redirecting EC2 management console click on View Instances Button.
Step 8 – Locking the Key File
We need to change the permission for the downloaded key file, so that it is not publicly viewable for SSH to work.
We can achieved by following command:
sudo chmod 400 path/to/key/file
Step 9 – Connecting to Instance
Via SSH the downloaded key file can be used to connect EC2 instance from our system.
Here we are using the Linux terminal for SSH access.
1.SSH to the instance using the key file and hostname. By combining EC2 username with public DNS name with a @ character the hostname is generated.
Command shown below is used for SSH:
sudo ssh -i "path/to/key/file" ec2-hostname
We need to Replace ec2-hostname with the hostname of the instance.
2. If it gives connection warning type yes.
3. After successful connection we will get below screen.
Now necessary software we can install on this EC2 instance.
Step 10 – Installing Apache
First we need to update the package list for upgrades and new packages using below command.
sudo apt-get update
2. Now we can install apache. To install apache use below command
sudo apt-get install apache2
3.If we navigate to the public IP of the instance from a browser by following the installation of apache, we will see the welcome page, it means that apache is running perfectly.
Step 11 – Installing MySql
1.To install mysql server use below command:
sudo apt-get install mysql-server
2. Set the root password of the database, after mysql has been installed.
secure the password using following command:
3. As shown in the above image Mysql will asks you to activate the VALIDATE PASSWORD PLUGIN. For now we have to type no.
4. Next type the root password of your choice and confirm it.
5. Next Mysql will ask for whether to remove anonymous users. Type yes.
6. Disallow root login remotely? Type No
7. Remove test database and access to it? Type Yes
8. Reload privilege tables now? Type Yes
9. After password set successfully we can check Mysql is working correctly or not by logging into the database by using below command :
sudo mysql -u root -p
After successful login below screen will be displayed.
10. To get out of the Mysql use below command :
Step 12 – Installing PHP with Common Extensions
Now we have to install php and commonly used php extensions by using below command :
sudo apt-get install php libapache2-mod-php php-mysql php-curl php-gd php-json php-zip php-mbstring
2. Restart apache after php installed by using below command.
sudo service apache2 restart
Step 13 – Installing phpMyAdmin
To install phpMyAdmin execte below command.
sudo apt install phpmyadmin
To configure phpmyadmin for the server following image will show options .
First option is to select proper web server.
By pressing the space bar select apache 2, then press tab key it will go to ok button then press enter.
2. Next configure the database for phpmyadmin with dbconfig-common. Select yes option.
3. Now for log in to phpMyAdmin it will ask password. By default the username is phpmyadmin. Now we have to set password for this user.
Press tab key for go to ok button and press enter.
4. Now we can access phpMyadmin from the browser.
Enter the login credentials with user phpmyadmin and associated password.
Step 14 – Allowing Mysql Root Login through phpMyAdmin
We can not login as root user through phpMyadmin. If want to do this perform the following steps :
Login to Mysql root using following command :
sudo mysql -u root -p
2. Enter the below query to change root login authentication from socket to password.
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; Replace password by root password we entered while installing Mysql.
3. Enter the below command to put the changes into effect.
Now we can login as root form phpMyAdmin.
NOTE : THIS STEP IS OPTIONAL
Step 15 – Connecting Instance with FTP Client
To upload and edit codes to the instance we will need to use FTP client. We are going to use Filezilla. Process will be almost similar for any other FTP/SFTP client.
2.Go to File > Site Manager enter all credentials need to upload files.
3. Under host tab enter public IP address for the instance.
4. For protocol select SFTP.
5. For Logon Type, select the option Key file.
6. The User is ubuntu.
7. For Key file, provide the path to the key file
After clicking on connect button, filezilla attempts to connect to the instance.
An “Unknown Host Key” window pops up will be display then click Ok.
8. After connecting to the instance it will taken to the default directory –
If we want to go to the web server directory we have to enter /var/www/html
9. Now we have to change ownership and access modes of the server directory
from root to ubuntu. Otherwise we will be unable to write files through FTP.
sudo chown -R ubuntu /var/www/html sudo chmod -R 755 /var/www/html
Using these two commands we can create and upload files from filezilla to our server directory in the instance.
10. If we want to test, we can create a index.php file in the server directory. Php file contains the following code.
If we navigate the public IP address, we will see the following page.
The above output shows Php installation is working perfectly.
Setup is finished.