How to Install the LAMP Stack on Ubuntu 16.04 / 18.04 / 18.10

LAMP is an acronym for Linux (Ubuntu), Apache2 HTTP Server, MariaDB or MySQL Database Server and PHP Scripting Language… It is a group of open source software and building block of many of the web applications and majoriy of the content management systems (CMS) in use today…

Content Management Systems like WordPress, Joomla, Drupal and others, they all use the LAMP Stack…

If you’re going to be developing any PHP based applications or websites, then you’re probably going to be using the LAMP Stack as well…

This brief tutorial is going to show students and new users how to install Apache2, MariaDB and PHP on Ubuntu Linux 16.04 / 18.04 and 18.10 servers…

To get started with installing the LAMP Stack, follow the steps below:

Step 1: Prepare Ubuntu Linux

The LAMP stack includes the Linux machine… in this case, Ubuntu… To get LAMP you must first install a Linux machine… this post assumes you’ve already install Ubuntu server..

After installing Ubuntu server, run the commands below to update the server…

sudo apt update && sudo apt dist-upgrade && sudo apt autoremove

Step 2: Install Apache2 HTTP Server

Apache2 HTTP Server represents the A in the LAMP stack… It’s the most popular web server installed today… way ahead of other popular web servers…

To install Apache2 HTTP on Ubuntu server, run the commands below…

sudo apt update
sudo apt install apache2

After installing Apache2, the commands below can be used to stop, start and enable Apache2 service to always start up with the server boots.

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

To test Apache2 setup, open your browser and browse to the server hostname or IP address and you should see Apache2 default test page as shown below.. When you see that, then Apache2 is working as expected..

ex.. http://localhost

apache2 ubuntu install

Step 3: Install MariaDB Database Server

MariaDB stands for M in LAMP and it’s a great place to start when looking at open source database server… Although MySQL was originally the default database server among Linux systems, MariaDB has taken over.. To install it run the commands below.

sudo apt-get install mariadb-server mariadb-client

After installing MariaDB database server, the commands below can be used to stop, start and enable MariaDB service to always start up when the server boots..

On Ubuntu 16.04 LTS

sudo systemctl stop mysql.service
sudo systemctl start mysql.service
sudo systemctl enable mysql.service

On Ubuntu 18.04 LTS and 18.10 

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

After that, run the commands below to secure MariaDB server by creating a root password and disallowing remote root access.

sudo mysql_secure_installation

When prompted, answer the questions below by following the guide.

  • Enter current password for root (enter for none): Just press the Enter
  • Set root password? [Y/n]: Y
  • New password: Enter password
  • Re-enter new password: Repeat password
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove test database and access to it? [Y/n]:  Y
  • Reload privilege tables now? [Y/n]:  Y

Restart MariaDB server

To test if MariaDB is installed, type the commands below to logon to MariaDB server

sudo mysql -u root -p

Then type the password you created above to sign on… if successful, you should see MariaDB welcome message

mariadb welcome

Step 4: Install PHP and Related Modules

The last component of the LAMP stack is PHP… It’s the P in the LAMP stack… To install PHP and related PHP modules, run the commands below…

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

After installing PHP, run the commands below to find the version installed on the server…

php -v

You should see an output like the one below:

PHP 7.2.10-0ubuntu0.18.04.1 (cli) (built: Sep 13 2018 13:45:02) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.10-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies

The version number determines the location of PHP default configuration file… For PHP 7.2, the location is as shown below:

sudo nano /etc/php/7.2/apache2/php.ini

Replace the version number above with the version of PHP installed..,..

When the file opens, make the changes on the following lines below in the file and save. The value below are great settings to apply in your environments.

file_uploads = On
allow_url_fopen = On
memory_limit = 256M
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = America/Chicago

After making the change above, save the file and close out.

Step 4: Restart Apache2

When you’re done making PHP changes above, run the commands below to restart Apache2 HTTP server for PHP settings to apply…

sudo systemctl restart apache2.service

To test PHP settings with Apache2, create a phpinfo.php file in Apache2 root directory by running the commands below

sudo nano /var/www/html/phpinfo.php

Then type the content below and save the file.

<?php phpinfo( ); ?>

Save the file.. then browse to your server hostname followed by phpinfo.php

http://localhost/phpinfo.php

You should see PHP default test page…

PHP 7.2 ubuntu nginx

Congratulations! You’re successfully installed the LAMP Stack on Ubuntu 16.04 / 18.04 / 18.10…

Enjoy~

You may also like the post below:

Install Vanilla Forums on Ubuntu 18.04 / 16.04 / 18.10 with Apache2, MariaDB and PHP 7.2 Support