How to Install LAMP Stack on AWS EC2 Ubuntu 22.04|20.04

To Install LAMP Stack on Amazon AWS EC2 with Linux Ubuntu 22.04|20.04, Now, open your ssh terminal window, and connect to AWS ec2 instance with the help of putty or ssh, then install Linux, apache, php, and mysql with the help of sudo apt install apache2, sudo apt-get install mysql-server, and sudo apt-get install php libapache2-mod-php commands and also configure them with instance.

How to Install LAMP Stack (Linux, Apache, MySql & PHP) on Linux Ubuntu 22.04|20.04 Amazon AWS EC2

Here are steps to install lamp stack Stack (Linux, Apache, MySql & PHP) on amazon aws ec2 ubuntu 22.04|20.04:

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

  1. 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.

With the key file downloaded, the button for launching the instance became active. Clicking that button is the last step to launch the AWS instance.

With the successful launch of the instance, we will be redirected to the page which gives a success message that instance is being launched.

Click on the button View Instances redirects us to the EC2 management console. This will contain the public DNS & public IP address of instance which will be required in the later steps.

(From the next step on-wards we need to connect to the virtual machine from our system and execute commands on it through a terminal / shell)

Step 3 – Connect Your AWS EC2 Instance from SSH

To connect your AWS EC2 instance from the SSH terminal, Open your putty or ssh and connect to from server.

Note:- If you are new to connecting AWS EC2 instance with windows, ubuntu and Mac systems. So you can read this guide How to Connect to ec2 Instance From Putty and SSH Terminal.

Step 4 – Install Apache on AWS EC2 Ubuntu

First, update system packages, type the sudo apt update command on ssh and press enter:

sudo apt update

To install apache 2 web server, Simply run sudo apt install apache2 command on ssh terminal:

sudo apt install apache2

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 completed. 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:

apache2 -version

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,:

sudo ufw app list

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:

sudo ufw status

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:

install apache 2 web server on amazon ec2
install apache 2 web server on amazon ec2

Step 5 – Install MySQL on AWS EC2 Ubuntu

Type sudo apt-get install mysql-server on ssh terminal and press enter, it will install MySql Server on aws ec2 ubuntu server:

sudo apt-get install mysql-server

After that, type sudo mysql_secure_installation command on ssh terminal to set the root password of the database and to secure the MySql server:

sudo mysql_secure_installation

Running mysql secure command, on the tab ssh terminal some prompts will appear, to which you have to answer by press y or n, these are:

  • You will be presented with a screen where MySQL asks whether you would like to activate the VALIDATE PASSWORD PLUGIN. For now, keeping things simple, type no.
  • In the next type the root password of your choice. Confirm it again.
  • In the next screen MySql will ask whether to remove anonymous users. Type yes
  • Disallow root login remotely? Type No
  • Remove test database and access to it? Type Yes
  • Reload privilege tables now? Type Yes

To type sudo mysql -u root -p command on ssh terminal to log in into the database and verify mysql server pa

sudo mysql -u root -p

The above command will ask for the password. Enter the password that you enter while securing the MySQL and hit enter will login into MySQL:

Now, Type exit on ssh terminal to get out of MySql:

exit

Step 6 – Install PHP Amazon EC2 Ubuntu

To install php and it’s related packages or extension, simply type the following command on ssh and press enter:

sudo apt-get install php libapache2-mod-php php-mysql php-curl php-gd php-json php-zip php-mbstring

After successfully installing PHP on Ubuntu Amazon ec2 instance. To open the dir.conf file and configure php in it, run the command sudo nano /etc/apache2/mods-enabled/dir.conf; You can do it something like this:

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

Now, you have installed and configured the LAMP stack (Linux, Apache, MySql & PHP) in Amazon AWS ec 2 instance, and have done some configuration, To restart the Apache web server for the configuration changes to be applied. By typing sudo service apache2 restart command command on the ssh terminal window, press Enter, it will restart apache server:

sudo service apache2 restart

Conclusion

That’s it; you have learned how to install lamp stack (Linux, Apache, MySql & PHP) on amazon aws ec2 ubuntu 22.04|20.04.

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *