How To Install Apache, MariaDB, and PHP (LAMP stack) in Ubuntu 18.04

How To Install Apache, MariaDB, and PHP (LAMP stack) in Ubuntu 18.04


The LAMP stack is an open-source web development stack used to deploy and run web-based applications and dynamic websites. Typically, the stack is made up of an Operating system, web server, database server, and a programming language; a blend that powers a commendable portion of the modern internet world. LAMP stands for Linux (Operating System),  Apache (web server), MySQL/MariaDB (databases), PHP/Perl/Python (Programming language).

Traditionally, MySQL has been used as the database server in the LAMP stack. However, things have changed, and MariaDB has become more and more common. MariaDB is an excellent drop-in replacement for open-source purists looking to avoid Oracle’s MySQL. This tutorial will help you install Apache, MariaDB, and PHP on Ubuntu 18.04 LTS server. .

Assuming you have a good VPS hosting or cloud VPS hosting, let’s get started!


Before you proceed, the following requirements must be in place.

Step 1:
Installing the Apache2 Web Server

The first step when installing the LAMP stack is to install a web server. In our case, we’ll install the Apache2 web server Using the Ubuntu apt package manager.

First, execute the command below to update your package index:

$ sudo apt update

Next, run the command below to install Apache2:

$ sudo apt install apache2

Adjusting the Firewall Setting

Once you install Apache web server, the next thing is adjusting the firewall settings to permit HTTP and HTTPS web traffic.

Run the command below to check the ufw application profiles:

$ sudo ufw app list

This will give you the output below:

Available applications:
  Apache Full
  Apache Secure

Now, if you inspect Apache Full profile, you will notice that it enables web traffic to both port 80 and port 443. To examine this profile, run the command below:

$ sudo ufw app info "Apache Full"

The output will be:

Profile: Apache Full
Title: Web Server (HTTP,HTTPS)
Description: Apache v2 is the next generation of the omnipresent Apache web


To permit inbound HTTP and HTTPS web traffic on both port 80 and port 443, execute the command below:

$ sudo ufw allow in"Apache Full"

To check if all went as planned, visit the public IP address of server through the web browser.


This should give you a default Apache web page for Ubuntu 18.04. The page is available for testing and informational purposes, and it should be something similar to this:

How To Install Apache, MariaDB, and PHP (LAMP stack) in Ubuntu 18.04?

This page shows that the Apache web server is successfully installed and easily accessible via the firewall.

Step 2
Installing MariaDB

By now, the Apache web server is correctly configured, its time we install the database management system. As aforementioned we’ll deviate from the norm and install MariaDB database instead of Oracle’s MySQL.

Run the command below to install MariaDB:

$ sudo apt install mariadb-server

Next, execute the command below to verify the installation:

$ sudo systemctl status mariadb

This will give you the output below:

Loaded: loaded(/lib/systemd/system/mariadb.service;enabled;vendorpreset
Active: active(running)sinceSat2018-08-2503:26:34UTC;1weeksodaysago
  Docs: man:mysqld(8)
Main PID: 12761(mysqld)
Status: "Taking your SQL requests now..."
    Tasks: 27(limit:1152)
CGroup: /system.slice/mariadb.service

Next, run the command below to verify the version of MariaDB:

$ mysql -V

This will give you an output similar to the one below:

mysql  Ver 15.1Distrib 10.1.34-MariaDB, fordebian-linux-gnu (x86_64) usingreadline 5.2

Securing MariaDB

Now, your database management system (MariaDB) is up and running but its not secured. The next step is ensuring MariaDB is secure. Execute the commands below to secure MariaDB shell:

$ sudo mysql_secure_installation

Note: You will be requested to enter your current root user password. However, since you have just installed MariaDB, and have not set any password yet, the password is blank. Just Press ENTER to continue.

You will also be prompted to answer some question. Every step is detailed and should not be hard to follow. It recommended you pick Yes [Y] for all the answers. Here are the questions:

Set root password? [Y/n] type Y and press enter
New Password: Key in your preferred password and enter to continue.
Re-enter new password: Re-enter the password and press enter.
Remove anonymous users? [Y/n] type Y and press enter.
Disallow root login remotely [Y/n] type Y and press enter
Remove test database and access it? [Y/n] type Y and press enter
Reload privilege tables now? [Y/n] type Y and press enter

This will give you the output below:

All Done! If you've completed all of the above steps, your MariaDB installation should now be secure.
Thank you for using MariaDB!

Step 3
Installing PHP

PHP is another layer of the LAMP stack that processes code to present dynamic content. It’s a vital component that can connect to the MariaDB database, run scripts, and relay processed content for the web server to display.

Run the command below to install PHP:

$ sudo apt install php libapache2-mod-php php-mysql

Modifying Apache settings

Once you install PHP, you should change the way Apache presents files whenever a directory is asked for. By default, Apache first looks for index.html file whenever a user asks for a directory. However, we want Apache web server to look for a PHP file called index.php first whenever a directory is requested.

To make this modification, we’ll have to edit the dir.conf file. Run the command below to open this file:

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

This is the file content:

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

To complete the changes, move the index.php, just before index.html, to have the following:

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

Save the file and press CTRL +X to close. Type Y and press ENTER to confirm the changes.

Next, Run the command below to restart your Apache web server.

$ sudo systemctl restart apache2

Now, we’ll use systemct1 to examine Apache2 status. To achieve this, run the command below:

$ sudo systemctl status apache2

This will give you an output similar to the one below:

 apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
   Active: active (running) since Tue 2018-04-2314:28:43 EDT; 45s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 13581 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS)
  Process: 13605 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)
    Tasks: 6 (limit: 512)
   CGroup: /system.slice/apache2.service
           ├─13623 /usr/sbin/apache2 -k start
           ├─13626 /usr/sbin/apache2 -k start
           ├─13627 /usr/sbin/apache2 -k start
           ├─13628 /usr/sbin/apache2 -k start
           ├─13629 /usr/sbin/apache2 -k start
           └─13630 /usr/sbin/apache2 -k start

Installing PHP Modules

Next, we need to install additional modules to enhance PHP modules. First, lets check the available PHP modules; we’ll use apt to search the modules

$ sudo apt-cachesearch php7.2

This will give you a list of the available PHP7.2 extensions. Each extension comes with a short description. If you find a module that you need to install, use the apt command to install it. For instance, if you want to install php7.2-cli module, execute the command below:

$ sudo apt install php7.2-cli

To install multiple modules all at once, use the apt command and type the modules and separate them using space.

 $ sudo apt install package1 package2 ...

By, now the LAMP stack is successfully installed and configured on your Ubuntu 18.04. But, before you make any change or deploy any application, it’s advisable you test your PHP processing.

Testing PHP Processing

To test whether you web server is properly configured for PHP, we create a PHP script; info.php. For Apache to find the file and process it correctly, it should be saved in the web root directory. The web root directory is /var/www/htm/. Now, execute the command below to create a file in this locations:

$ sudo nano /var/www/html/info.php

Once the blank file is opened, add the text below:


Save and exit the file

Next, visit the address:


This should open a page similar to the one below:

How To Install Apache, MariaDB, and PHP (LAMP stack) in Ubuntu 18.04?

How To Install Apache, MariaDB, and PHP (LAMP stack) in Ubuntu 18.04?

How To Install Apache, MariaDB, and PHP (LAMP stack) in Ubuntu 18.04?

The page above shows that PHP is functioning as expected. Now, run the command below to remove the above file, as it could give unauthorized users access to your server information.

$ sudo rm /var/www/html/info.php

The page can be recreated when needed.


That’s it! You have successfully installed the LAMP stack. You should be able to install your desired web software and websites on your Ubuntu 18.04 server without a struggle.

Check out these top 3 Linux hosting services

$1.79 /mo
Starting price
Visit FastComet
Rating based on expert review
  • User Friendly
  • Support
  • Features
  • Reliability
  • Pricing
$4.00 /mo
Starting price
Visit Kamatera
Rating based on expert review
  • User Friendly
  • Support
  • Features
  • Reliability
  • Pricing
$2.49 /mo
Starting price
Visit HostArmada
Rating based on expert review
  • User Friendly
  • Support
  • Features
  • Reliability
  • Pricing
  • Do you need the best VPS? Read about our different offers.

How to Install OpenCart on an Ubuntu 18.04 Server or VPS with Apache, MariaDB and PHP 7

In this how-to, you'll learn how to install OpenCart, an open source shopping ca
3 min read
Angela Olaru
Angela Olaru
Senior Writer & Hosting Expert

How to Install AMPPS on Ubuntu 18.04

AMPPS is a solution stack for Apache, MYSQL, MongoDB, PHP, Perl and Python that
2 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How To Install Apache, MySQL & PHP on an Ubuntu 18.04 VPS or Dedicated Server

This article shows users how to setup PHP, Apache web server and MySQL database
4 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Install PHP7.2 on Ubuntu 18.04

This tutorial will help you install PHP 7.2 on your Ubuntu 18.04 system.
3 min read
David Malcom
David Malcom
Author provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top