How to Install LAMP (Linux, Apache, MySql & PHP) on AWS EC2 with Ubuntu 20.04

cloud
Published on June 3, 2020

Ubuntu 20.04 was released in April 2020, and it is now available on major cloud platforms as well.

This tutorial discusses installation of LAMP stack in an AWS EC2 virtual machine. The following topics are included :

  • Launching an EC2 instance
  • Selecting virtual machine specs
  • Creating Key Pair required to connect to virtual machine
  • Connecting to EC2 virtual machine from a Windows, Linux or Mac OS system
  • Updating package list on virtual machine
  • Installing Apache 2.4
  • Installing MySql 8.0
  • Installing PHP 7.4 & commonly used PHP extensions
  • Installing phpMyAdmin 4.9
  • Allowing MySql root login through phpMyAdmin (if required)
  • Connecting to EC2 virtual machine with an FTP client (FileZilla)

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.

    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.

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

Step 3 - 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 4.1 - Connecting to Instance - From Windows

We can use the key file to connect to the instance using PuTTY application (this is included in Windows by default). However the key file that was downloaded is a .PEM file. For PuTTY to work, it needs a .PPK file.

.PEM can be converted to .PPK using the PuTTYgen application (included by default in Windows).

  1. Open PuTTYgen (Start > All Programs > PuTTY > PuTTYgen)

  2. For Type of key to generate field choose RSA

  3. Choose Load and choose the downloaded .PEM file (choose option to display files of all types)

  4. Choose Open. PuTTYgen will display a message that the .PEM file was successfully imported. Choose OK.

  5. Choose Save private key. For the warning, choose Yes.

  6. Specify the same name as that of the downloaded .PEM file and choose Save. PuTTYgen will now generate the .PPK file

Now that we have a suitable file extension for PuTTY to work, we can now connect to the instance.

  1. Open PuTTY (Start > All Programs > PuTTY > PuTTY)

  2. In the Category panel on the left, choose Session.

  3. The Host Name field is formed by combining the username, here ubuntu, with the Public DNS of the EC2 instance separated by the @ character.

    ubuntu@ec2-public-dns

    In our case it was something like :

    ubuntu@ec2-3-22-172-83.us-east-2.compute.amazonaws.com
  4. Port field is 22

  5. For Connection type choose SSH

  6. In the Category panel on the left, expand Connection, expand SSH, and then choose Auth.

  7. Browse and select the .PPK file that was created earlier with PuTTYgen. Now click on the main Open button.

  8. If a warning message is given, choose Yes

  9. We are now connected to the instance and can enter commands on it.

Step 4.2 - Connecting to Instance - From Linux / Mac

The key file that was downloaded will now be used to connect to the EC2 instance via SSH.

  1. The permission for the downloaded key file needs to be changed so that it is not publicly viewable. This is required for the SSH connection to work.

    Open the terminal command line on our system and type the following command to lock the key file.

    sudo chmod 400 /path/to/key/file.pem

    /path/to/key/file.pem needs to be replaced by the path of the key file in our system.

  2. Now we can SSH to the instance using path of the key file and the hostname. In this scenario, the hostname is formed by combining the username, here ubuntu, with the Public DNS of the EC2 instance separated by the @ character.

    The following command is used to connect to the instance:

    sudo ssh -i "/path/to/key/file.pem" ubuntu@hostname

    In our case it was something like :

    ssh -i "UA.pem" ubuntu@ec2-3-22-172-83.us-east-2.compute.amazonaws.com
  3. Type yes if it gives a connection warning.

  4. On successful connection with the instance the terminal will look as shown below:

Step 5 - Updating the Package List

Update the package list on the instance so that we can have the latest available versions of Apache, PHP, MySql and phpMyAdmin.

sudo apt-get update

Step 6 - Installing Apache

  1. Install Apache through the following command :

    sudo apt-get install apache2

    Note that it will install the latest available version of Apache (2.4.43 as of May 2020).

  2. A successful installation of Apache can be checked by typing in the Public IP address / DNS of the instance in the browser. This can be found from the AWS EC2 management page.

    Successfully installed Apache will look like :

Step 7 - Installing MySQL

  1. Install MySQL using the following command :

    sudo apt-get install mysql-server
  2. After installation is complete, the mysql database is strengthened using the following command:

    sudo mysql_secure_installation
  3. In the next screen we will be asked whether we need to setup VALIDATE PASSWORD plugin. We selected no.

  4. Set the password for the root MySql account.

  5. Remove anonymous users? We selected yes.

  6. Disallow root login remotely? We selected no.

  7. Remove test database and access to it? We chose yes.

  8. Reload privilege tables now? We again chose yes.

  9. Following installation we can login into MySQL by executing the following command:

    sudo mysql -u root -p

    Entering the password in the prompt, we can login into MySQL and will be greeted by the mysql prompt. Type exit to get out of MySQL.

Step 8 - Installing PHP with Common Extensions

  1. Install PHP and commonly used extensions through the following command.

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

    The latest available version of PHP (7.4.3 as of May, 2020) will be installed.

  2. After PHP has been installed, restart Apache using the following command:

    sudo service apache2 restart

Step 9 - Installing phpMyAdmin

  1. Install phpMyAdmin through the command :

    sudo apt-get install phpmyadmin
  2. In the next screen select the apache2 webserver.

    It is important to note here, space-bar is pressed to select the option, tab is pressed to move to Ok button. Correctly selected apache2 will look like :

  3. In the next screen for dbconfig-common configuration, choose Yes

  4. By default, phpMyAdmin sets a MySQL user named phpmyadmin. In the next terminal prompt, the password for this MySQL username is set (this is different from the root MySQL user for which we had set a password in an earlier step).

  5. phpMyAdmin is now installed. This can be tested in the browser by navigating to the /phpmyadmin path of the public IP address / DNS of the instance (found in the EC2 management page).

    Enter the username as phpmyadmin and the password that was setup in the earlier step.

Step 10 - Allowing MySQL Root Login Through phpMyAdmin

This is an optional step and will allow the root MySQL user to login through phpMyAdmin.

  1. Login to MySql root.

    sudo mysql -u root -p
  2. Change authentication method for the root account to mysql_native_password by executing the following query.

    ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' BY 'password';

    Replace password by the root password you entered while installing MySQL.

  3. To put the changes into effect, execute the following query :

    FLUSH PRIVILEGES;
  4. Following this, we can now do a root login through phpMyAdmin. The username is root and password is the one that was setup earlier while installing MySQL.

  5. Return back to the command line.

    exit

Step 11 - Connecting Instance with FTP Client

Uploading files and other things becomes very simple using a FTP client. We are going to set up FileZilla to connect to our EC2 instance.

  1. Open Filezilla. Under File > Site Manager setup the credentials needed to upload files.

    • Protocol is SFTP.

    • Host is Public IP Address of EC2 Instance.

    • Logon Type is Keyfile.

    • User is ubuntu.

    • Navigate to the downloaded key file in Key file.

  2. When FileZilla prompts about the server's host key in unknown, click Yes.

  3. FileZilla will then connect to EC2.

  4. We will be taken to default directory - /home/ubuntu. We can enter /var/www/html to go to the web server directory.

  5. To be able to upload files using FileZilla we need to change the ownership and access modes of the server directory.

    This is done by SSHing into EC2 and typing the following commands:

    sudo chown -R ubuntu /var/www/html
    sudo chmod -R 755 /var/www/html
  6. The installation can be tested by creating a file using FileZilla and entering the following simple code:

    <?php echo phpinfo(); ?>

    Navigate to the created file from your browser to get output like the one shown below.

    This implies that the setup is working perfectly.

Setup is Finished

Congratulations! You have a working EC2 instance with LAMP stack.

In case of any problems during installation, you can always terminate that instance and start a new one.

In this Tutorial