How to Install Apache 2.4, MariaDB 10.3, and PHP 7.2 on Ubuntu 18.04

Published on: Fri, Nov 16, 2018 at 12:45 pm EST

In this article, you will learn how to setup an up-to-date LAMP stack by installing the latest stable releases of Apache 2.4 and MariaDB 10.3 on Ubuntu 18.04.

Note: Ubuntu 18.04 ships with PHP 7.2 already installed, so we will only need to install some necessary packages.


  • An up-to-date Ubuntu 18.04 x64 server instance
  • A sudo user.

Create a sudo user

First, update your system:

apt-get update -y
apt-get upgrade -y

Next, create a new user:

adduser <username>

Type and re-type a new secure password for this user, then either set the user information or leave the fields blank and press ENTER to use the defaults.

Add the user to the sudo group:

usermod -aG sudo <username>

Install Apache 2.4

Install the latest stable release of Apache 2.4:

sudo apt-get install apache2 -y

Use the following command to confirm the installation and check the Apache version:

apache2 -v

The output will resemble the following:

Server version: Apache/2.4.29 (Ubuntu)
Server built:   2018-10-03T14:41:08

In a production environment, you will want to remove the default Ubuntu Apache welcome page:

sudo mv /var/www/html/index.html /var/www/html/index.html.bak

For security purposes, you should prevent Apache from exposing files and directories within the web root directory /var/www/html to visitors:

sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.bak
sudo sed -i "s/Options Indexes FollowSymLinks/Options FollowSymLinks/" /etc/apache2/apache2.conf

Note: In accordance with your specific requirements, you can customize more settings in that file later.

Start the Apache service and make it start on system boot:

sudo systemctl start apache2.service
sudo systemctl enable apache2.service

Install MariaDB 10.3

First, install software-properties-common, if necessary:

sudo apt-get install software-properties-common

Next, import the gpg key:

sudo apt-key adv --recv-keys --keyserver hkp:// 0xF1656F24C74CD1D8

Add the system apt repo:

sudo add-apt-repository 'deb [arch=amd64] bionic main'

Update apt:

sudo apt update -y

Now you can install MariaDB:

sudo apt install -y mariadb-server mariadb-client

During the installation process, the MariaDB package configuration wizard will automatically pop up and ask you to setup a new password for the MariaDB root user. Choose a secure password and repeat it to confirm it.

Having MariaDB installed, you can confirm the installation:

mysql -V

The output will be similar to the following:

mysql  Ver 15.1 Distrib 10.3.10-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

Start and enable the MariaDB service:

sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

Secure the installation of MariaDB:

sudo /usr/bin/mysql_secure_installation

The first prompt will be to enter the root password you just set. Next it will ask if you would like to change the password. You can enter n and press ENTER, unless you would like to change the password.

During the interactive process, just press ENTER for the default options, as they are the safest.

MariaDB 10.3 has now been securely installed onto your system. In the future, you can setup designated users and databases for your web apps as well. Log into the MySQL shell as root:

mysql -u root -p

Type the MariaDB root password you set earlier when prompted.

Create a MariaDB database webapp, a database user webappuser, and the database user's password yourpassword:

CREATE USER 'webappuser'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALL PRIVILEGES ON webapp.* TO 'webappuser'@'localhost' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;

If necessary, you can customize MariaDB by reviewing and editing the main MariaDB config file which is /etc/mysql/my.cnf:

sudo cp /etc/mysql/my.cnf /etc/mysql/my.cnf.bak
sudo vi /etc/mysql/my.cnf

Remember to restart the MariaDB service if you make any modifications to that file:

sudo systemctl restart mariadb.service

Install PHP 7.2 packages

Be sure to install these packages after installing Apache:

sudo apt-get install -y php libapache2-mod-php7.2 php7.2-cli php7.2-common php7.2-mbstring php7.2-gd php7.2-intl php7.2-xml php7.2-mysql php7.2-zip

Setup the UFW firewall

By default, the UFW firewall on Ubuntu 18.04 is inactive. You should enable the UFW firewall in order to enhance security. First check the app list:

sudo ufw app list

Next set your rules:

sudo ufw allow OpenSSH
sudo ufw allow in "Apache Full"

Finally, start ufw:

sudo ufw enable

Congratulations, you now have a LAMP stack up and running on your Ubuntu 18.04 system. You can now deploy your own web app on the basis of the LAMP stack.