How To Install LAMP Stack on Ubuntu 18.04 LTS (Apache , MySQL and PHP)

What is LAMP Stack?

LAMP stands for Linux, Apache, MySQL and PHP. LAMP is the most popular environment for the PHP based applications.

Apache is the leading web server and is developed by Apache Foundation, MySQL is database management server used to keep data, and PHP is the most used programming language up to data.

 

Within this article you will learn how to install Apache 2.4, PHP 7.2 and MySQL 5.7 on Ubuntu 18.04 LTS in the easy way.

 

1. Preparation

 

You need to have root and or sudo permission user account to your Ubuntu 18.04 server.

 

Once logged as root execute the following :

 

-  sudo apt update

- sudo apt ugrade

 

2. Install PHP 7.2

 

Ubuntu 18.04 haas by default PHP 7.2 available for installation. But we strongly recommend adding and using PPA for PHP installation which includes different versions as well. To do so, execute the below commands, which will update apt cache and install PHP packages for you.

 

sudo apt-get install python-software-properties

sudo add-apt-repository ppa:ondrej/php

sudo apt update

sudo apt install -y php7.2

 

You may want to install additional PHP modules like  Curl , GD, json, mbstring and xml

 

To do so execute :

 

sudo apt install php7.2-curl php7.2-gd php7.2-json php7.2-mbstring php7.2-xml

 

3. Install MySQL

 

Finally it is time to install mysql-server for your MySQL database and the php-mysql module which will support the communication between both.

 

Execute:

 

sudo apt install mysql-server php7.2-mysql

 

You will be required to set root password. It is important to remember your MySQL password as you will need it to connect to your MySQL server with root permissions.

Once the installation is done, execute the following command which will ask you several questions on which you can safely answer with  N for root password change and Y for everything else.

 

sudo mysql_secure_installation

 

You may want to install phpMyAdmin for easier web based MySQL administration.

 

sudo apt install phpmyadmin

 

4. Restart Apache and MySQL

 

Execute:

 

sudo systemctl restart apache2.service

sudo systemctl restart mysql.service

 

5. Firewall configuration

 

If you have not done it yet, then you will have to execute the below command in order to open port 80 of your VPS.

 

For IPtables Users:

 

sudo iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT

 

For UFW users:

 

sudo ufw allow 80/tcp

 

 

5. Test it up

 

The LAMP installation is done, now is time to test your webserver.

First of all, you will have to create a info.php file which should be located in your poblic folder (/var/www/html/)

 

Inside the file add the following:

 

<?php

    phpinfo();

?>

 

And finally , open your browser and go to your domain.com/test.php and you should see the below with all the PHP details.