How to Install the LAMP Stack on Ubuntu 20.04 | 18.04

This brief tutorial shows students and new users how to install the LAMP stack on Ubuntu 20.04 | 18.04 LTS servers.

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 majority of the content management systems (CMS) in use today.

Many of the popular content management systems being used today are using some combinations of this opensource framework.

From WordPress to Drupal to Joomla and many others, they’re all using the LAMP stack to power their applications.

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.

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

Step 1: Prepare Ubuntu Linux

L in LAMP stands for Linux, in this case, Ubuntu. If you’re reading this post, then you probably know a thing or two about Ubuntu.

Your first task to get LAMP is a Linux machine. After installing, prepare the machine and run the commands below to update it.

sudo apt update
sudo apt upgrade
sudo apt autoremove

There are lots of other settings and configurations to you can apply to your Linux servers which are not covered here. But For LAMP, update the server and continue below.

Step 2: Install Apache2 HTTP Server

Apache2 represents the A in LAMP and is the most popular opensource web server and a key component of the LAMP stack.

To install Apache2 on Ubuntu, 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 whether Apache2 is installed and functioning, open your web browser and browse to the server’s IP address or hostname.

http://localhost

Apache2 test page on Ubuntu

If you see the above page in your browser, then Apache2 is working as expected.

Step 3: Install MariaDB Database Server

You’ll also need a database server as it’s another component of the LAMP stack. A database server is where content get stored and retrieved.

A true open source database server that you can run with any content management system is MariaDB database server. It is fast, secure and the default server for almost all Linux servers.

To install MariaDB, run the commands below:

sudo apt-get install mariadb-server mariadb-client

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

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

Next, run the commands below to secure the database server with a root password if you were not prompted to do so during the installation.

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

To verify and validate that MariaDB is installed and working, login to the database console using the commands below:

sudo mysql -u root -p

type the root password when prompted.

Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 49
Server version: 10.1.44-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

If you see a similar screen as shown above, then the server was successfully installed.

Step 4: Install PHP 7.4 and Related Modules

PHP is a general-purpose scripting language the glues all the other components of the stack.

Since some versions of Ubuntu don’t have the latest versions of PHP, you can add a third-party PPA repository to install PHP from there.

The command below will add a third-party PPA to Ubuntu.

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Then update and upgrade to PHP 7.4

sudo apt update

Next, run the commands below to install PHP 7.4 and related modules.

sudo apt install php7.4 libapache2-mod-php7.4 php7.4-common php7.4-mysql php7.4-gmp php7.4-curl php7.4-intl php7.4-mbstring php7.4-xmlrpc php7.4-gd php7.4-xml php7.4-cli php7.4-zip

After installing PHP 7.4, go and configure some basic settings that may be required for most CMS to function properly.

Run the commands below to open PHP

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

Below are good settings to configure for most CMS websites.

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

That should get PHP 7.4 installed with some basic settings to allow most CMS to function.

To validate that PHP is installed, run the commands below:

php -v

You should see an output like the one below:

PHP 7.4.5 (cli) (built: Apr 19 2020 07:36:30) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.5, Copyright (c), by Zend Technologies

You can also test with a test php script and displays installed version as well as related modules that are enabled or disabled.

To do that, run the commands below to create a php test file called phpinfo.php

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

Then type the content below and save the file.

<?php phpinfo( ); ?>

Save the file.

Open your browser and browse to your server hostname followed by phpinfo.php

http://localhost/phpinfo.php

You should see PHP default test page…

PHP Test Page on Ubuntu

That’s it!

Conclusion:

This post showed you how to install the LAMP stack on Ubuntu 20.04 | 18.04. If you find any error above, please use the comment form below to report it.

Enjoy~

You may also like the post below:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.