How to Install LAMP(Linux Apache Mysql PHP) on Ubuntu 20.04(AWS-EC2)
Tags: ubuntu, devops, cloud, aws
Published: 2022-07-14 09:02:00

## What is LAMP Stack?
The LAMP stack is a popular open-source solution stack used primarily in web development.
LAMP consists of four components necessary to establish a fully functional web development environment. The first letters of the components' names make up the LAMP acronym:
This term is actually an acronym which represents the **Linux** operating system, with the **Apache** web server. The site data is stored in a **MySQL** database, and dynamic content is processed by **PHP**.

The illustration below shows how the layers stack together:
![Lamp](https://phoenixnap.com/kb/wp-content/uploads/2022/01/visual-representation-of-the-lamp-stack-pnap.png)

### Step-1 Install Apache and Update the Firewall.
First we need to install apache on our Virtual machine but before that lets update the necessary softwares by following command. Just to remind, ubuntu used apt package manager and Red hat based distros like centos uses yum package manager.

```
$ sudo apt update
```

```
$sudo apt install apache2
```
To see Apache version on a Debian/Ubuntu Linux, run:

```
apache2 -v
```
For CentOS/RHEL/Fedora Linux server, type command:

```
httpd -v
```
You can locate apache2 or httpd path using the type command or command command. For instance:

```
type -a httpd
type -a apache2
whereis httpd
command -v httpd
```
Now you need to update the firewall so that your firewall allow HTTP and HTTPS traffic. To check the UFW (**uncomplicated firewall**) which is a firewall configuration tool that runs on top of iptables, included by default within Ubuntu distributions.

```
$ sudo ufw app list
```
```
Output
Available applications:
  Apache
  Apache Full
  Apache Secure
  OpenSSH
```
If you look at the Apache Full profile details, you’ll see that it enables traffic to ports 80 and 443:
```
sudo ufw app info "Apache Full"
```
```
Output
Profile: Apache Full
Title: Web Server (HTTP,HTTPS)
Description: Apache v2 is the next generation of the omnipresent Apache web
server.

Ports:
  80,443/tcp
```
Now to allow incoming HTTP and HTTPS traffic for this server, run:
```
sudo ufw allow "Apache Full"
```
```
OUTPUT

Rules updated
Rules updated (v6)
```
Then to check , go to your instance  public IP address 
```
http://your_server_ip
```
Note: To know your IP address,we can use following commands:
```
$ sudo apt install curl
$ curl http://icanhazip.com
$ curl ifconfig.me
```

It will show something like this with apache information:
![Apache](https://assets.digitalocean.com/articles/how-to-install-lamp-ubuntu-18/small_apache_default_1804.png)

### Step-2 Installing MYSQL.
As we have now our webserver up and running, its time to install Mysql.
```
$ sudo apt install mysql-server
$ sudo mysql --version
```  
This command will show you a list of the packages that will be installed, along with the amount of disk space they’ll take up. Enter Y to continue.
When installation is done, run a simple security script to remove some dangerous defaults and lock down access to your database system.
```
$ sudo mysql_secure_installation
```
Now read and complete the installtion steps like password set etc.
When it's done test if you are able to logged in to the mysql console

Answer Y for yes, or anything else to continue without enabling.
```
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No:
```
If you’ve enabled password validation, you’ll be shown the password strength for the root password you just entered and your server will ask if you want to change that password. If you are happy with your current password, enter N for “no” at the prompt:
```
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n
```

Finally type the following to enter into mysql console
```
$ sudo mysql
```
```
Output
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.34-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
```

and type ```
exit
```  to get out of the console.

### Step-3 Installing PHP
Why? to install it. Because PHP can run scripts, connect to your MySQL databases to get information, and hand the processed content over to your web server so that it can display the results to your visitors. 
Type the below command:
```
$ sudo apt install php libapache2-mod-php php-mysql
```
We've installed three packages in the above command:
- **php** package
- **libapache2-mod-php** to integrate PHP into Apache webserver.
- **php-mysql** package to allow PHP to connect to MySQL databases.<br>

To extend the functionality of PHP, you have the option to install some additional modules.
```
$ apt search php- | less
```
Use the arrow keys to scroll up and down, and press Q to quit.

The results are all optional components that you can install. It will give you a short description for each:

For example, to find out what the php-cli module does, you could type this:
```
$ apt show php-cli
```
If you decided that php-cli is something that you need, you could type:
```
sudo apt install php-cli
```
### Step-4 Test the PHP Processing on your Web Server
To test that your system is properly configured for PHP, create a PHP script called **info.php**. In order for Apache to find this file and serve it correctly, it must be saved to your web **root directory**.
```
$ sudo nano /var/www/**your_domain**/info.php
```
Insert the following command to show the php information.
```
<?php
phpinfo();
?>
```
Save and close it 
The address you will want to visit is:
```
http://your_domain/info.php
```
The page that you come to should look something like this:
![php](https://assets.digitalocean.com/articles/how-to-install-lamp-ubuntu-18/small_php_info_1804.png)

Now that you have a LAMP stack installed, you have many choices for what to do next. You’ve installed a platform that will allow you to install most kinds of websites and web software on your server.