How to Install LAMP Stack on Ubuntu 18.04

Introduction

A LAMP stack is an open source software stack for building dynamic websites and web applications. And this guide is about the LAMP Stack on Ubuntu system. Basically, LAMP is an acronym of its own components, Linux, Apache, MySQL, and PHP. Each component has it’s own functionally, Linux as the operating system, Apache as HTTP web server, MySQL to store all site data, and dynamic content handled by PHP.

A LAMP stack becomes one of the most popular software stacks for web applications technology, especially the LAMP stack on Ubuntu System. It has been used for the most PHP web application frameworks such as Laravel, YII Framework, CodeIgniter etc. And most content management system such as WordPress, Magento, Drupal, and Joomla uses it too.

In this guide, we’re going to show you install the LAMP Stack on Ubuntu 18.04 Bionic beaver.

Requirements

In order to complete this guide about LAMP Stack on Ubuntu system, ensure you have the latest version of Ubuntu Server 18.04 with basic setup and configuration of the ufw firewall and sudo root privileges. The basic initial setup ubuntu server 18.04 can be done on this link.

Step 1 – Installing Apache Web Server

Apache HTTP web server is one of the most popular web servers on the internet. Widely used by large websites such as Paypal.com, bbc.com, bbc.co.uk, steam store etc. In short, Apache web server is a great choice to host your dynamic websites and web applications.

First of all, ensure all repositories on the Ubuntu system is up to date. Or you can run the apt command below to update system repositories.

sudo apt update

Now install Apache with the following apt command.

sudo apt install apache2 -y

Once the Apache installation is complete, you will need to add Apache to the ufw firewall application list.

Check available applications on the ufw firewall.

sudo ufw app list

And you will be shown three different profile for Apache, add the ‘Apache Full’ to for opening both HTTP and HTTPS protocols.

sudo ufw allow in "Apache Full"
Add Apache Service to UFW Firewall
Add Apache Service to UFW Firewall

After that, open your web browser and type the server IP address on the address bar.

http://10.9.9.100/

And you woll get the Apache default index html.

Apache Index html
Apache Index html

Step 2 – Installing MySQL Database

After installing Apache web server, we’re moving forward to install one of the most popular RDBMS (Relational Database Management System) MySQL server.

Install MySQL server using the following apt command.

sudo apt install mysql-server mysql-client -y

When the installation is complete, we need to secure the MySQL deployment by configuring the root password of the MySQL server. Simple, run the following command.

sudo mysql_secure_installation

And you will be prompted about various configurations for securing the MySQL deployment.

Would you like to setup VALIDATE PASSWORD plugin? Y

Type ‘Y’ to yes.

Now you need to choose between three password policies, 0 for LOW, 1 for MEDIUM, and 3 for STRONG.

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2

We recommend using the ‘STRONG’ password policy ‘2’. Then type the strong password that you’re comfortable and type ‘y’ for yes to confirm.

Securing MySQL Deployment
Securing MySQL Deployment

After that, type ‘Y’ again for all the rest.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y

And the MySQL server configuration is complete. Test by logging in to the MySQL with the user root and your password, then create a new MySQL user for your applications.

mysql -u root -p
TYPE YOUR PASSWORD

Create a new MySQL user by running the following queries.

create user [email protected] identified by '[email protected]#';
flush privileges;

At this point, you have successfully logged in to the MySQL and created a new mysql user.

Create a New user MySQL
Create a New user MySQL

Step 3 – Installing PHP 7.x

PHP is one of the most server scripting languages that has been used on the internet. It can run PHP scripts, connect to the MySQL database, and generate contents of web applications for you through a web browser.

By default, the new version of ubuntu server has a default PHP 7.2 for installation. And for this case, we’re going to install PHP 7.2 with some basic modules for basic PHP web applications.

Install PHP 7.2 using the following commands.

sudo apt install php php-common php-mysql php-cli libapache2-mod-php -y

After installing PHP, we can test the PHP by running the following command.

Check the PHP version.

php -v

Check available PHP modules.

php -m

As a result, you will get the PHP 7.2 installed with basic PHP modules on the Ubuntu system.

Check PHP Version and Installed Modules
Check PHP Version and Installed Modules

Step 4 – Testing

Before going any further, let’s check the Apache and MySQL services using the following command.

sudo systemctl status apache2
sudo systemctl status mysql

Ensure both Apache and MySQL services is up and running.

Next, go to the ‘/var/www/html’ directory and create a new php file called ‘info.php’.

cd /var/www/html
vim info.php

Paste the following phpinfo script into it.

<?php
phpinfo();
?>

Save and close the configuration.

After that, open the web browser again and type the server IP address following with the script file name.

http://10.9.9.20/info.php

And you will be shown the PHP configuration on the server.

phpinfo Apache web server
phpinfo Apache web server

After that, we’re going to test the PHP and MySQL by creating the other PHP file called ‘connector.php’.

vim connector.php

Change the default username and password with your own user that you’ve created on top, then paste into it.

Save and close.

Now type the server IP address again following with the PHP filename ‘connector.php’.

http://10.9.9.100/connector.php

And you will get the result as the PHP connection to MySQL database successfully.

LAMP Stack on Ubuntu Server has been installed
Connect PHP to MySQL

Finally, you have installed the LAMP Stack on Ubuntu 18.04 Bionic Beaver.