How to Install Linux, Apache, MySQL, PHP (LAMP) Stack on Ubuntu 18.04

The LAMP stack is a group of four open-source software used in building dynamic websites and web applications. The acronym stands for the Linux operating system, the Apache web server, the MySQL database, and the PHP programming language. In this tutorial, we will install LAMP on an Ubuntu 18.04 computer.

1. Update and upgrade your Ubuntu system

$ sudo apt update
$ sudo apt upgrade

2. Install Apache and confirm that it is installed on your system

$ sudo apt install apache2
$ systemctl status apache2

To further verify that the Apache installation is complete, go to your browser and enter the keyword localhost.

Apache 2

3. Install MySQL, and start and enable the MySQL service

$ sudo apt-get install mysql-client mysql-server
$ sudo systemctl start mysql.service
$ sudo systemctl enable mysql.service

4. Secure the MySQL database server

$ sudo mysql_secure_installation

You will be asked a series of questions, some of which have introductory sentences. Here are the answers:
Would you like to setup VALIDATE PASSWORD plugin? y
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
New password: (Enter password)
Re-enter new password: (Repeat password)
Do you wish to continue with the password provided? : y
Remove anonymous users? : y
Disallow root login remotely? : y
Remove test database and access to it? : y
Reload privilege tables now? : y

5. Restart the MysqlSQL server service

$ sudo systemctl restart mysql.service

6. Install PHP and the needed PHP extensions

$ sudo apt install php libapache2-mod-php php-mysql
$ sudo apt install php-curl php-gd php-xml php-mbstring php-xmlrpc php-zip php-soap php-intl

7. Make index.php and not index.html your default page

$ sudo gedit /etc/apache2/mods-enabled/dir.conf

This will open a text editor with text that looks like the lines of code below:

<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Swap the positions of index.html and index.php and save the file.

8. Restart the Apache server

$ sudo systemctl restart apache2

9. Check the directory /var/www/html

The webpage that you see when you type in localhost in your browser has the file name index.html and is located in the directory /var/www/html.

10. Grant user permission to /var/www/html and and restart Apache

$ sudo chown -R user /var/www/html
$ sudo service apache2 restart

11. Confirm that PHP is installed on your Ubuntu computer

Create a sample.php file in /var/www/html. Type the lines of code below as the content of sample.php:

<?php
phpinfo();
?>

Visit localhost/sample.php in your browser to check the result.

PHP7