Install apache2, PHP7.2, MySQL5.7, phpMyAdmin on ubuntu 18.04 hosted on AWS EC2

This article is dedicated for people who want to setup for the first time a host for their website or web application in AWS EC2.
I have added some bug fixes in the end related to the combination PHP7.2 and phpMyAdmin.
Let’s start !
Prerequisites
- PHP 7.2
- MySQL 5.7
- apache 2
- AWS EC2 instance used is t2.micro (but you can chose different instance depending on your needs and what your web application do.)
Launch AWS EC2 instance
Given that you already signup in AWS services, after a 24 hours you can access all the services including EC2. I chose a t2.micro (free tier eligible with 750 hours per month read more) just for tutorial purposes.
- Click on “Launch Instance”

- Choose an Amazon Machine Image (AMI)
To select an AMI free tier eligible category check “Free tier only” and then look for ubuntu instances, chose ubuntu 18.04 LTS and hit select.

- Choose Instance Type

- Configure Instance
For free tier eligible category, keep configuration as default, in case you want to launch a reserved instance please check more details here.
In case you want to execute some Linux commands to initiate a process or install packages or modules at the moment of starting the instance, you can use “User data” see the example below User data and console.
In this tutorial we are going to execute all commands using Shell scripting and with SSH access after completely launching our instance.

- Add Storage
EC2 limits the user to 30GB storage for Free tier category.

- Add tag
Give your instance a name that will identify it among others.

- Configure Security Group
We are going now to open some ports that will be used inside and outside our web application. We will need rules below: SSH with default port 22, HTTP with default port 80, HTTPS with default port 443 and MySQL/Aurora with port 3306.
NB. I set temporarily MySQL/Aurora source to 0.0.0.0. It’s too open, so you might need to limit it to only your mysql consumer host IP.

- Review and Launch
Create a new key pair, and download it (keep it safe, since we will need it for our SSH access)
To begin install our requirements, we need first to connect to our instance via SSH tunnel and to do so, please follow steps provided by AWS when click “Connect”
NB. Connect with Putty or other tool if possible, personally I prefer my beautiful terminal :)


Install apache2
Let’s first update ubuntu server by executing the command
sudo apt-get update
sudo apt-get upgrade
Now, we can install apache as easy as typing the command
sudo apt-get install apache2
NB. Check if apache is working fine by visiting the public IP address of your EC2 instance. In case everything is OK, you should see the famous apache index page.
Install MySQL
Now, let’s jump to MySQL installation by executing the commands
sudo apt-get install mysql-server
sudo mysql_secure_installation
You will be prompted to several screens let’s address them one by one
- Activate the VALIDATE PASSWORD PLUGIN: select No (no need for a validation now but you need of course to choose a strong memorizable password next step)
- Choose a root password and confirm it again.
- You might need to remove anonymous users, and temporarily allow root login remotely (after your web app database migration, you can reconfigure mysql to only allow root@’your host here’).
- Disable test databases access, and reload privilege tables.
Now after finishing installation you can check if MySQL works fine by logging to your database using the command. You will be prompted to MySQL command screen.
sudo mysql -u root -p
Install PHP
To install PHP and some other most commonly needed modules, we will use again apt-get and executing the command below
sudo apt-get install php libapache2-mod-php# necessary modules for PHP server
sudo apt-get install php-mysql php-curl php-gd php-json php-zip php-mbstring php-mcrypt
We need to restart apache to get last system changes
sudo service apache2 restart
To check if PHP is installed successfully, go to /var/www/html folder and create your first php script there by creating a PHP file (index.php already created by apache. Feel free to remove it or modify it) and copy past the code below in it
<?php
phpinfo();
?>
After visiting the link [x.x.x.x/index.php] (x.x.x.x public IP address of your EC2 instance), you will see this page

Install phpMyAdmin
phpMyAdmin facilitate accessing and managing your MySQL database via a web page. To install it you need to execute the command below
sudo apt-get install phpmyadmin
Then follow steps below after each prompt respectively
- Press “space” to select apache2 (*) then press “tab key” and “enter” to confirm.
- Press Yes to configure phpmyadmin
- Enter password for phpmyadmin
By default you cannot login as root user through phpMyAdmin. To do this perform the below steps :
- Login to MySql root.
sudo mysql -u root -p
- Change root login to password type by executing the command
ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘root password created the moment of installing mysql’;
- Apply changes by executing the command
FLUSH PRIVILEGES;
Bugs you could encounter while accessing phpMyAdmin (PHP7.2 + phpMyAdmin)
- See first bug here (Resolution provided by Chaloemphon Thipkasorn)
- See second bug here (Resolution provided by Chaloemphon Thipkasorn)
Thank you guys, Hope my article helps you.