How to Install LAMP on Ubuntu Linux

This post shows students and new users steps to install and use the LAMP stack on Ubuntu Linux.

LAMP is an acronym for Linux (Ubuntu), Apache, MariaDB or MySQL and PHP Scripting Language. It is a group of open source software and building blocks for 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. Below, we’ll show you how to get the stack up and running on Ubuntu Linux.

If you’re a student or new user learning Linux, the easiest place to start learning is on Ubuntu Linux. Ubuntu is the modern, open source Linux operating system for desktop, servers and other devices.

To get started with installing the LAMP stack on Ubuntu Linux, follow the steps below:

How to install Ubuntu Linux

L in LAMP stands for Linux, in this case, Ubuntu. This post choses Ubuntu Linux because it’s easy to use and basically for beginners. If you’re a new student or new user, Ubuntu Linux is a great place to start with Linux.

Your first task to get LAMP configured is a Linux machine. If you haven’t installed Ubuntu before, then read this post to learn how to install it.

Once Ubuntu is installed, run the commands below to update it.

sudo apt update
sudo apt dist-upgrade
sudo apt autoremove

There are lots of other settings and configurations to apply that apply to Ubuntu, however, the post is only concern with installing LAMP.

How to install Apache on Ubuntu Linux

Apache represents the A in LAMP and is the most popular opensource web server and a key component of the LAMP stack. Apache is available in Ubuntu repositories. Update the packages index and install Apache with the following commands.

sudo apt update
sudo apt install apache2

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

sudo systemctl stop apache2
sudo systemctl start apache2
sudo systemctl enable apache2

To see if Apache is installed, open a web browser and browse to the server hostname or IP address.

If you see the above Apache welcome page, it means Apache is installed and functioning.

How to install MariaDB on Ubuntu Linux

The M in LAMP represents MariaDB or MySQL database server. Both MySQL and MariaDB is an open-source, multi-threaded relational database management systems and key component of the LAMP stack.

For this post, we’re going to be installing MariaDB instead of MySQL.

To install MariaDB, run the commands below.

sudo apt update
sudo apt install mariadb-server

After installing, you can run the commands below to view MariaDB service status.

sudo systemctl status mariadb

After running the command above, it should output similar lines as shown below.

mariadb.service - MariaDB 10.3.31 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2021-09-15 16:40:20 CDT; 22s ago
       Docs: man:mysqld(8)
             
   Main PID: 3007 (mysqld)
     Status: "Taking your SQL requests now..."
      Tasks: 31 (limit: 4651)
     Memory: 65.6M
     CGroup: /system.slice/mariadb.service
             └─3007 /usr/sbin/mysqld

Sep 15 16:40:20 ubuntu2004 /etc/mysql/debian-start[3045]: mysql
Sep 15 16:40:20 ubuntu2004 /etc/mysql/debian-start[3045]: performance_schema

Both MariaDB and MySQL come with a script that allows you to perform some security operations.

Run the commands below to invoke the script and perform some recommended tasks to secure the database.

sudo mysql_secure_installation

Both MariaDB and MySQL servers come with the root user is set to use the auth_socket authentication method by default.

The auth_socket plugin authenticates users that connect from the localhost through the Unix socket file. This means that you can’t authenticate as a root by providing a password.

To logon to MariaDB and MySQL servers as root, simply run the command below. You don’t need a password since it’s using auth_socket method.

To logon to MariaDB, run the commands below.

sudo mysql

The server console should come up.

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

How to install PHP on Ubuntu Linux

The P in LAMP stands for PHP. PHP supports many types of web servers, including Apache, Nginx and few others. If you’re using Apache web server, then the commands below is used to install PHP.

sudo apt update
sudo apt install php libapache2-mod-php

To install the latest versions of PHP that are not available in Ubuntu repository, run the commands below to install a third party PPA repository which includes multiple versions of PHP.

At the time of this writing, the latest version of PHP is 8.0.

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

After adding the repository above, you can then install other PHP version.

sudo apt install php8.0 php8.0-common php8.0-cli php8.0-gd php8.0-curl php8.0-mysql

That should do it!

For more on LAMP, read individual posts on the LAMP components

Conclusion:

This post showed you how to install the LAMP stack on Ubuntu Linux. If you find any error above or have something to add, please use the comment form below.