How to install and configure PHP Apache2 Mysql on Ubuntu 16.04 and 18.04

To run any website we need a group of open-source applications to execute the code and a database to store the information. In this tutorial, we will learn how to install and configure the Lamp stack (PHP, Apache2, and Mysql) on ubuntu 18.04 LTS and 16.04 LTS.

Let's get started

By default LAMP stack comes with ubuntu central repository package. All you need to do is to update the Ubuntu APT Repository. It can be done with the following command

sudo apt update

Step 1. Install apache2 server

Apache2 is a powerful, flexible web server that implements the latest protocols, including HTTP/1.1  (RFC2616). To install the apache2 in your machine / VPS enter the following command. 

sudo apt install apache2

Apache2 Install Prompt

Press to install the apache2. Once you are done with the installation. Open your browser and enter your VPS IP address to test the installation. If you are using your local machine you can use 127.0.0.1 or localhost to test the installation.

An alternate way to test the installation is to check the status of apache2 if it is up and running.

sudo service apache2 status

The following screen is an example of the successful installation of apache2.

Apache2 sucess page

Step 2. Install and configure Mysql Server

Mysql is an open-source relational database management system based on a structured query language and used by many companies.

In this section, we will cover the installation of MySQL on ubuntu 18.04 LTS and 16.04 LTS from the terminal. To install enter the following command

sudo apt install mysql-server

This installs the package for the MySQL server, as well as the packages for the client and for the database common files.

During the installation process, you may be prompted to set a password for MySQL root user as shown below:

mysql server password set

Note: If the MySQL installation does not prompt to set the password. Don't worry we'll be hardening the MySQL in the next step. Once the installation and configuration of MySQL are done. Now we will be set up the password for the MySQL database root user.

Secure Mysql 

To harden the MySQL server enter the following command in your terminal

sudo mysql_secure_installation

you will be prompted to set the MySQL root account. also, you will be asked to configure VALIDATE PASSWORD PLUGIN as shown below

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

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:

Press Y to install the password plugin. After that, you will be asked to choose the password policy.

There are three levels of password validation policy:

LOW  Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2

Press 2 and hit enter. Now, You'll be asked to set a new password 

Please set the password for root here.

New password:

Re-enter new password:

Enter the password and hit Enter. Once done with setting the password you will be then prompted to remove the anonymous user and test database and disable remote login as shown below. 

By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) :

Press Y to remove anonymous users and hit enter

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) :

Press Y to disallow remote login

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) :

Press Y to remove the test database

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) :

Press y to flush privileges.

Once you are done with the installation and configuration of MySQL. Now it's time to test your MySql login with your username and password. To do so enter the following command.

mysql -u root -p
Enter password: 

Now enter your root password.

Step 3. Install PHP

PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. At the writing of this article, the current version of PHP is 7.3.

To install type the following command.

sudo apt install php libapache2-mod-php

This should install the latest version of PHP on your machine. To verify the version of PHP type the following command

php -v

To install the specific version of PHP. You can use PPA. 

Step 4. Check the PHP installation version

To verify the installation you can PHP info the following script:

# /var/www/html/index.php
<?php

phpinfo();

?>

This code should output the below response.

php install ubuntu success message