In this tutorial, we will guide you through the step-by-step process of installing LAMP (Linux, Apache, MySQL, PHP) on an Ubuntu 20.04 AWS EC2 instance.
How to Install LAMP (Linux, Apache, MySql & PHP) on Linux Ubuntu 20.04 AWS EC2
- Step 1 – Launching a Virtual Machine
- Step 2 – Selecting Virtual Machine Specs
- Step 3 – Connect Your AWS EC2 Instance from SSH
- Step 4 – Install Apache
- Step 5 – Install MySQL on AWS EC2 Ubuntu
- Step 6 – Install PHP Amazon EC2 Ubuntu
- Step 7 – Restart Apache Web Server
Step 1 – Launching a Virtual Machine
Log into the AWS EC2 Management Console, and click on the button Launch Instance.
Step 2 – Selecting Virtual Machine Specs
- Clicking Launch Instance will take us to a page where the specs for virtual machine can be chosen.Select Ubuntu Server 20.04 LTS(HVM), SSD Volume Type.
2. In the next screen we are going with t2.micro instance which is Free Tier Eligible.
3. For the section Configure Instance — go for default settings.
4. For the section Add Storage — go for default settings.
5. For the section Add Tags — go for default settings.
6. For the section Configure Security Group — add HTTP rule. HTTP is required for the web server that will be installed shortly.
7. We can also setup rules regarding IP addresses that can access the added protocols. For the tutorial, both the protocols are open to all IP addresses. If required, this can be edited later as well.
8. Clicking Review and Launch button in this page takes us to a page where we can review our instance settings. Click on the Launch button.
Creating and Downloading a Key Pair
After clicking on the Launch button, the next page will present with options for Key file. This key file is used to connect to the EC2 instance from our system.
Choose the option Create a new key pair and download the key file.
After downloading the key file, the launch button for the AWS instance becomes active. Clicking this button is the final step to launch the instance.
Once the instance is successfully launched, a success message will be displayed, and we will be redirected to a page indicating that the instance is being launched.
Clicking on the “View Instances” button will redirect us to the EC2 management console. Here, we can find the public DNS and public IP address of the instance, which will be required in the subsequent steps.
From the next step onwards, we will need to connect to the virtual machine from our system and execute commands on it through a terminal or shell.
Step 3 – Connect Your AWS EC2 Instance from SSH
To get started, you’ll need to connect to your AWS EC2 instance using SSH. If you’re new to connecting to AWS EC2 instances from Windows, Ubuntu, or Mac, you can follow this tutorial on “How to Connect to an EC2 Instance from Putty and SSH Terminal” to connect to your EC2 instance via SSH.
Step 4 – Install Apache
In this step, you need to download the latest version of the software by first updating the local package index of Ubuntu repositories. So, start your ssh terminal and type the below given command:
Use the below given command to install apache 2 into your ubuntu web server. So, open ssh terminal again and run this sudo command to install apache web server on aws ec2 instance:
Note that, on running installation of apache 2 web server on aws ec2 instance. The prompt box will be open with a “Y/N” option to continue the installation.
Please enter Y, after which the installation process will begin.
Verify the Apache installation
Installing Apache2 web server on your Ubuntu 20.04 EC2 instance is co
To proceed with this step, you’ll need to update the local package index of Ubuntu repositories to download the latest version of the software. Begin by opening your SSH terminal and running the following command:
```
sudo apt-get update
```
Next, you can install Apache 2 on your Ubuntu web server by executing the following command in your SSH terminal:
```
sudo apt-get install apache2
```
During the installation process, a prompt box will appear with a “Y/N” option to continue the installation. Please enter “Y” to proceed with the installation process.
mpleted. So, open your ssh terminal and type the below command to check the Apache 2 is installed or not on our Ubuntu 20.04 aws EC2 instance:
Congratulation!!, You have successfully installed the Apache 2 web server on AWS ec2 instance.
If you want to access it from outside. So, you need to update firewall settings on your apache 2 web server on your Ubuntu 20.04 EC2 instance.
List the UFW application profiles
In this step, open your terminal ssh and type the following command to list such available firewall ufw applications,:
Then you will see a list of the application profiles:
OutputAvailable applications: Apache Apache Full Apache Secure OpenSSH
As indicated by the output, there are three profiles available for Apache:
- Apache: This profile opens only port 80 (normal, unencrypted web traffic)
- Apache Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
- Apache Secure: This profile opens only port 443 (TLS/SSL encrypted traffic)
Allow Apache Full on UFW
In this step, you need to allow Apache Full on UFW will open port 80 and 443 for network traffic, while providing maximum security to the server. So, use the below given UFW command to allow Apache Full through the following command:
sudo ufw allow 'Apache Full'
You can verify the change by typing:
The output will provide a list of allowed HTTP traffic:
OutputStatus: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Apache ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Apache (v6) ALLOW Anywhere (v6)
Verify that the Apache service is running
sudo systemctl status apache2
Finally, open your browser and type your AWS ec2 instance IP address. And, it will be looks like the Apache 2 page in the following picture:
Step 5 – Install MySQL on AWS EC2 Ubuntu
In this step, open your ssh terminal and type the following command to install MySql Server:
sudo apt-get install mysql-server
Then, secure MySql server by using the following command. So, you will need to set the root password of the database and secure it using the following command :
sudo mysql_secure_installation
Once you run the command to install MySQL, the following questions will be presented in the prompt:
1. You will be asked whether you would like to activate the VALIDATE PASSWORD PLUGIN for MySQL. For simplicity, type “no” for now.
2. Next, you’ll be prompted to enter a root password of your choice. Confirm it again.
3. MySQL will then ask whether to remove anonymous users. Type “yes.”
4. You’ll be prompted whether to disallow root login remotely. Type “no.”
5. MySQL will then ask whether to remove the test database and access to it. Type “yes.”
6. Finally, you’ll be asked whether to reload privilege tables now. Type “yes.”
After setting the password, you can check whether MySQL is working correctly by logging into the database with the following command:
sudo mysql -u root -p
The above command will ask for the password. Enter the password what you enter while the securing the MySQL and hit enter will login into MySQL:
Now, Type exit on ssh terminal to get out of MySql:
exit
Step 4 – Install PHP Amazon EC2 Ubuntu
In this step, install PHP with it’s common extensions. So open your ssh terminal and type the following command:
sudo apt-get install php libapache2-mod-php php-mysql php-curl php-gd php-json php-zip php-mbstring
After successfully installed PHP on ubuntu amazon ec2 instance. Now, you need to configure dir.conf
file by using the following sudo nano command:
sudo nano /etc/apache2/mods-enabled/dir.conf
It will look like this:
<IfModule mod_dir.c> DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm </IfModule>
Move the PHP index file to the first position after the DirectoryIndex
specification, like this:
<IfModule mod_dir.c> DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm </IfModule>
Step 7 – Restart Apache Web Server
After successfully installed PHP MySQL and configure these on ubuntu amazon ec2 instance.
Finally, You need to restart Apache 2 server by using the following command:
sudo service apache2 restart