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



1. Connecting to Instance
       through putty
Step 1 - Installing Apache
Before installing Apache, you need to update the package list for upgrades and new packages.
[sudo apt-get update]
Now Apache can be installed.
[sudo apt-get install apache2] 
Step 2 - 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. 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 3 - Installing PHP with Common Extensions
1. Now install PHP and commonly used PHP extensions by using the following command :
Now install different supported versions of PHP as follows.
For Apache Web Server
[sudo apt install php5.6 sudo apt install php7.0 sudo apt install php7.1 sudo apt install php7.2 sudo apt install php7.3]
For Nginx Web Server 
[sudo apt install php5.6-fpm sudo apt install php7.0-fpm sudo apt install php7.1-fpm sudo apt install php7.2-fpm sudo apt install php7.3-fpm]
2. Set Default PHP Version in Ubuntu
Set Default PHP Version 5.6
[sudo update-alternatives --set php /usr/bin/php5.6]
Set Default PHP Version 7.0
[sudo update-alternatives --set php /usr/bin/php7.0]
Set Default PHP Version 7.1
[sudo update-alternatives --set php /usr/bin/php7.1]
3. After switching from one version to another, you can find your PHP configuration file, by running the command below.
[------------ For PHP 5.6 ------------ sudo update-alternatives --set php /usr/bin/php5.6 $ php -i | grep "Loaded Configuration File" ------------ For PHP 7.0 ------------ sudo update-alternatives --set php /usr/bin/php7.0 $ php -i | grep "Loaded Configuration File" ------------ For PHP 7.1 ------------ sudo update-alternatives --set php /usr/bin/php7.1 $ php -i | grep "Loaded Configuration File" ------------ For PHP 7.2 ------------ sudo update-alternatives --set php /usr/bin/php7.2 $ php -i | grep "Loaded Configuration File" ------------ For PHP 7.3 ------------ sudo update-alternatives --set php /usr/bin/php7.3 $ php -i | grep "Loaded Configuration File"]
4. After PHP has been installed, restart Apache.
[sudo service apache2 restart]
Step 4. 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 5. 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.

NEXT : Connecting Instance with FTP Client
Next Post Previous Post