logo
post image

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

In this article step-by-step process is discussed to setup LAMP stack in AWS EC2. This will include :

  • Creating an EC2 instance
  • Connection to the instance from your system
  • Install Ubuntu 18.04
  • Install Apache 2.4
  • Install PHP 7.2
  • Install MySql 5.7
  • Install phpMyAdmin 4
  • Setting up FTP/SFTP access

AWS EC2 Pricing

Under the Amazon Free-Tier, you can use an EC2 instance for a year, free of cost. After an year, the instance is charged hourly depending upon the type of instance.

Working Environment for this Tutorial

This tutorial uses an Ubuntu system. The steps will work for all flavours of Linux, and also for Mac.

For Windows, the steps are almost the same, the only difference being you need to use PuTTY for SHH connection (rather than command line on Linux systems). See Step 6 below for more.

Step 1 - Launch an AWS EC2 Virtual Machine

Log into the AWS EC2 Management Console, and click on the button Launch Instance

Step 2 - Choosing the OS and Hardware

Clicking on the option from Step 1 takes to the below page. Here you get to choose the OS for the virtual machine and also the other hardware. Select Ubuntu Server 18.04 LTS (HVM), SSD Volume Type.

In the next screen choose the instance type. In this tutorial, t2.micro instance is selected as it is free-tier eligible. Single processor core and 1 GB memory is good enough for general purposes.

Step 3 - Editing Some Basic Configurations

Go for the default settings for Configure Instance, Add Storage and Add Tags screens.

However add 2 rules under Configure Security Group. They are SSH and HTTP.

SSH is required to login to the virtual machine using command line. This is already added to the security group.

HTTP is for the web server that will be installed shortly. To add HTTP rule, click on Add Rule & choose HTTP from the dropdown.

As per AWS documentation, security groups acts as a virtual firewall. In short you can set up the rules regarding IP addresses that can access the added protocols. For the tutorial, both the protocols are open to all IP addresses. But you can also restrict some IP addresses from accessing all protocols. This can be edited later also.

Clicking Review and Launch button in this page takes us to a page where you can review our instance settings. Click on the Launch button.

Step 4 - Creating a Key Pair

The next page will present with options for Key file. This key file is used to connect to the EC2 instance from your 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. And clicking that button is the last step to launch the instance in AWS.

With the successful launch of the instance, you will be redirected to the page which gives a success message that instance has been launched.

Click on the button View Instances and you will be redirected to the EC2 management console.

Step 5 - Locking the Key File

The permission for the downloaded key file needs to be changed so that it is not publicly viewable for SSH to work. It is achieved by :

sudo chmod 400 path/to/key/file

Step 6 - Connecting to Instance

The key file that was downloaded will now be used to connect to the EC2 instance from our system via SSH. In this tutorial a Linux system is used, so we are using the Linux terminal for SSH access.

(For SSH access with Windows see Connecting to Your Linux Instance from Windows Using PuTTY)

  1. SSH to the instance using the key file and hostname. The hostname here is generated by combining the EC2 username (which is ubuntu for Ubuntu 18.04) with the public DNS Name with a @ character.

    Example: ubuntu@ec2-54-91-219-85.compute-1.amazonaws.com

    The following command is used for SSH :

    sudo ssh -i "path/to/key/file" ec2-hostname

    Replace ec2-hostname with the hostname of the instance.

  2. Type yes if it gives a connection warning.
  3. Upon a successful connection, you will see the below screen :

Now you can start installing the necessary software on this EC2 instance.

Step 7 - Installing Apache

  1. Before installing Apache, you need to update the package list for upgrades and new packages.

    sudo apt-get update
  2. Now Apache can be installed.

    sudo apt-get install apache2
  3. Following the installation of Apache, if you navigate to the public IP of the instance (that can be found from the EC2 Console) from a browser, you will see the familiar Apache Welcome Page. This implies Apache is running perfectly.

Step 8 - Installing MySql

  1. Install MySql Server

    sudo apt-get install mysql-server
  2. After MySql has been installed, you will need to set the root password of the database and secure it using the following command :

    sudo mysql_secure_installation
  3. You will be presented a screen where MySQL asks whether you would like to activate the VALIDATE PASSWORD PLUGIN. For now, keeping things simple, type no.
  4. In the next type the root password of your choice. Confirm it again.
  5. In the next screen MySql will ask 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 the password has been set you can check the whether MySQL is working correctly by logging into the database with the command :

    sudo mysql -u root -p

    Password is the same that was set in the previous step.

    If everything went smoothly, you will be welcomed by the mysql prompt.

  10. Type exit to get out of MySql

    exit

Step 9 - Installing PHP with Common Extensions

  1. Now install PHP and commonly used PHP extensions by using the following command :

    sudo apt-get install php libapache2-mod-php php-mysql php-curl php-gd php-json php-zip php-mbstring
  2. After PHP has been installed, restart Apache.

    sudo service apache2 restart

Step 10 - Installing phpMyAdmin

To install phpMyAdmin execute the following command :

sudo apt install phpmyadmin

Following which you will be presented with options to configure phpmyadmin for the server :

  1. The first option is to select the proper web server. It is important to note here that although apache2 is highlighted it hasn't been selected.

    Select apache2 by pressing the space-bar. Then press Tab key that takes us to the Ok button. Now press enter.

  2. Next option is to configure the database for phpmyadmin with dbconfig-common. Select the Yes option here.

  3. Then password is asked for login to phpmyadmin. By default the username is phpmyadmin. You are now setting the password for this user (this is different from the roor user).

    Press Tab key to go the Ok button.

  4. Now phpMyAdmin can be accessed from the browser by navigating to

    http://[SERVER_PUBLIC_IP]/phpmyadmin

    Enter the login credentials with user phpmyadmin and the associated password.

Step 11 - Allowing Mysql Root Login through phpMyAdmin

This step is optional.

By default you cannot login as root user through phpMyAdmin. To do this perform the below steps :

  1. Login to MySql root.

    sudo mysql -u root -p
  2. Enter the query to change root login authentication from socket to password.

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

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

  3. Enter the query to put the changes into effect.

    FLUSH PRIVILEGES;

Now you can login as root from phpMyAdmin.

Step 12 - Connecting Instance with FTP Client

Very often you will need to use an FTP client to upload and edit codes to the instance. Filezilla is covered here, however the process will be almost similar for any other FTP/SFTP client.

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

    • Under Host, provide the Public IP Address of the instance.
    • For Protocol, select SFTP.
    • For Logon Type, select the option Key file.
    • The User is ubuntu.
    • For Key file, provide the path to the key file

    When the Connect button is clicked, Filezilla attempts to connect to the instance. An "Unknown Host Key" window pops up, click Ok here.

  2. You will be taken to default directory - /home/ubuntu. You can enter /var/www/html to go to the web server directory.
  3. Now the the ownership and access modes of the server directory /var/www/html needs to be changed from root to ubuntu. Otherwise you will be unable to write files through FTP.

    sudo chown -R ubuntu /var/www/html
    sudo chmod -R 755 /var/www/html

    Following these two commands you can create and upload files from FileZilla to our server directory in the instance.

  4. For testing you can create an index.php file in the server directory and write a simple PHP code to test the installation.

    <?php echo phpinfo(); ?>

    Now if you navigate to the public IP address, you should see the familiar page.

    This implies that the PHP installation 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.

For any issues, you can comment it below. We will try to solve it.