How to install Apache, PHP 7.2, and MySQL on Ubuntu 18.04 (LTS)

December 3, 2020

1. Installing MySQL or MariaDB Database ( The following example will be an installation of MariaDB 10 )

1.1 Install MariaDB 10

Run the following command to install MariaDB-server and client:

apt-get -y install mariadb-server mariadb-client

Now we set a root password for MariaDB.


You will be asked these questions:

Enter current password for root (enter for none): <-- press enter
Set root password? [Y/n] <-- y
New password: <-- Enter the new MariaDB root password here
Re-enter new password: <-- Repeat the password
Remove anonymous users? [Y/n] <-- y
Disallow root login remotely? [Y/n] <-- y
Reload privilege tables now? [Y/n] <-- y

1.2 Test  the MySQL/MariaDB root login
Test the login to MariaDB with the “mysql command”

mysql -u root -p

and enter the MariaDB root password that you’ve set above. The result should be similar to the screenshot below:

To leave the MySQL/MariaDB shell, enter the command “quit” and press enter.

2. Install Apache Web Server

Apache 2 is available as an Ubuntu package, therefore we can install it like this:

apt-get -y install apache2

Now direct your browser to, and you should see the Apache2 default page (It works!):

The document root of the apache default vhost is //www/html on Ubuntu and the main configuration file is /etc/apache2/apache2.conf. The configuration system is fully documented in /usr/share/doc/apache2/README.Debian.gz.

3. Install PHP 7.2

We can install PHP 7.2 and the Apache PHP module as follows:

apt-get -y install php7.2 libapache2-mod-php7.2

Then restart Apache:

systemctl restart apache2

4. Test PHP and get details about your PHP installation

The document root of the default web site is //www/html. We will now create a small PHP file (info.php) in that directory and call it in a browser. The file will display lots of useful details about our PHP installation, such as the installed PHP version.

nano //www/html/info.php

Then change the owner of the info.php file to the www-data user and group.

chown www-data:www-data //www/html/info.php

Now we call that file in a browser (e.g.

As you see, PHP 7.2 is working, and it’s working through the Apache 2.0 Handler, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP. MySQL is not listed there which means we don’t have MySQL / MariaDB support in PHP yet.

5. Get MySQL / MariaDB support in PHP

To get MySQL support in PHP, we can install the php7.2-mysql package. It’s a good idea to install some other PHP modules as well as you might need them for your applications. You can search for available PHP modules like this:

apt-cache search php7.2


apt-cache search php-

as not all PHP packages have the version number 7.2 in their name.
Pick the ones you need and install them like this:

apt-get -y install php7.2-mysql php7.2-curl php7.2-gd php7.2-intl php-pear php-imagick php7.2-imap php-memcache  php7.2-pspell php7.2-recode php7.2-sqlite3 php7.2-tidy php7.2-xmlrpc php7.2-xsl php7.2-mbstring php-gettext

Now restart Apache2:

systemctl restart apache2


Article Tags:
· · · · ·
Article Categories:

Leave a Comment