DEV Community

Cover image for How to Install LAMP(Linux Apache Mysql PHP) on Ubuntu 20.04(AWS-EC2)
Tanmay Shukla
Tanmay Shukla

Posted on • Updated on

How to Install LAMP(Linux Apache Mysql PHP) on Ubuntu 20.04(AWS-EC2)

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

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
Enter fullscreen mode Exit fullscreen mode
$sudo apt install apache2
Enter fullscreen mode Exit fullscreen mode

To see Apache version on a Debian/Ubuntu Linux, run:

apache2 -v
Enter fullscreen mode Exit fullscreen mode

For CentOS/RHEL/Fedora Linux server, type command:

httpd -v
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
Output
Available applications:
  Apache
  Apache Full
  Apache Secure
  OpenSSH
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode

Now to allow incoming HTTP and HTTPS traffic for this server, run:

sudo ufw allow "Apache Full"
Enter fullscreen mode Exit fullscreen mode
OUTPUT

Rules updated
Rules updated (v6)
Enter fullscreen mode Exit fullscreen mode

Then to check , go to your instance public IP address

http://your_server_ip
Enter fullscreen mode Exit fullscreen mode

Note: To know your IP address,we can use following commands:

$ sudo apt install curl
$ curl http://icanhazip.com
$ curl ifconfig.me
Enter fullscreen mode Exit fullscreen mode

It will show something like this with apache information:
Apache

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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:
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Finally type the following to enter into mysql console

$ sudo mysql
Enter fullscreen mode Exit fullscreen mode
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> 
Enter fullscreen mode Exit fullscreen mode

and type

exit
Enter fullscreen mode Exit fullscreen mode


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
Enter fullscreen mode Exit fullscreen mode

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.

To extend the functionality of PHP, you have the option to install some additional modules.

$ apt search php- | less
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

If you decided that php-cli is something that you need, you could type:

sudo apt install php-cli
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Insert the following command to show the php information.

<?php
phpinfo();
?>
Enter fullscreen mode Exit fullscreen mode

Save and close it
The address you will want to visit is:

http://your_domain/info.php
Enter fullscreen mode Exit fullscreen mode

The page that you come to should look something like this:
php

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.

Top comments (5)

 
tanmaygi profile image
Tanmay Shukla

Thank u very much !

 
tanmaygi profile image
Tanmay Shukla

Thanks, Actually I have launch the ec2 then installed apache, mysql and php on it as per some blog. But not set up any dns. but it sounds necessary can you help me with that.

Collapse
 
tanmaygi profile image
Tanmay Shukla

Yes, we can use it too thanks for the information

Collapse
 
tanmaygi profile image
Tanmay Shukla

but here we need public Ip address not private

 
tanmaygi profile image
Tanmay Shukla

actually I am still learning all these DNS and all. Still in early phase. hope u dont mind