How to install LAMP stack on Ubuntu 21.04

Share this article

LAMP is a software stack of open-source packages. The goal of the LAMP stack is to provide high-performance web applications development.

Let’s first see what LAMP stands for:

  • L: Linux (Operating system)
  • A: Apache (Web server)
  • M: MySQL (Database server)
  • P: PHP (Programming language)

Each component adds crucial capabilities to the stack. In the following sections, we will look at how to install the LAMP stack.

Step 1: Linux Operating System installation

Here we will use ubuntu 21.04 as Linux operating system. For installation look at this complete guide for ubuntu 21.04 installation.

Preparing ubuntu server

Now for All packages, we will use our terminal and install it on top of ubuntu 21.04 from the first step.

For accessing the terminal you can use the shortcut ctrl+alt+t in the desktop version.
Also, To install the latest versions of the software. Do the system update, run the following commands

sudo apt update
sudo apt upgrade

Open important ports which we will be using to access the webserver. Generally, HTTP web servers run on port 80, and HTTPS web servers use port 443. If you want to use a different port number you can open it on your system’s firewall. By running the following commands.

sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable

Step2: Apache Webserver installation

Install Apache server by using the below command.

sudo apt install apache2 -y

Verifying apache webserver installation

In the below-mentioned command, you can see that while checking out the status of the command it says web server is enabled and PID is 7943

foofunc@ubuntu21:~$ sudo service apache2 status
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-08-10 08:41:05 UTC; 1min 7s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 7943 (apache2)
      Tasks: 55 (limit: 11821)
     Memory: 5.2M
     CGroup: /system.slice/apache2.service
             ├─7943 /usr/sbin/apache2 -k start
             ├─7944 /usr/sbin/apache2 -k start
             └─7945 /usr/sbin/apache2 -k start

Aug 10 08:41:05 ubuntu21 systemd[1]: Starting The Apache HTTP Server...
Aug 10 08:41:05 ubuntu21 apachectl[7942]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress th>
Aug 10 08:41:05 ubuntu21 systemd[1]: Started The Apache HTTP Server.
foofunc@ubuntu21:~$ 

You can also test it from GUI, In my ubuntu server, IP address is 192.168.122.205. I entered URL: http://192.168.122.205:80 in my browser. I received the below-mentioned output.

Step3: MariaDB Database server installation

The founder of both MySQL as well as MariaDB is the same and they serve the same purpose. However, MariaDB has several optimizations that tend to improve the performance as compared to MySQL.  Therefore, MariaDB is a popular database server. We will install Mariadb and the installation is simple and requires just a few steps.

Install the MariaDB server and client using the below-mentioned command.

sudo apt install mariadb-server mariadb-client -y

Verify MariaDB installation

foofunc@ubuntu21:~$ service mariadb status
● mariadb.service - MariaDB 10.5.10 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-08-10 08:57:08 UTC; 14s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
    Process: 9168 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SUCCESS)
    Process: 9177 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 9179 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $? -eq 0 ]   && systemctl set-environment _WSREP_START_POSITION>
    Process: 9245 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 9247 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
   Main PID: 9226 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 18 (limit: 11821)
     Memory: 69.6M
     CGroup: /system.slice/mariadb.service
             └─9226 /usr/sbin/mariadbd

Aug 10 08:57:10 ubuntu21 /etc/mysql/debian-start[9252]: Processing databases
Aug 10 08:57:10 ubuntu21 /etc/mysql/debian-start[9252]: information_schema
Aug 10 08:57:10 ubuntu21 /etc/mysql/debian-start[9252]: mysql
Aug 10 08:57:10 ubuntu21 /etc/mysql/debian-start[9252]: performance_schema
Aug 10 08:57:10 ubuntu21 /etc/mysql/debian-start[9252]: Phase 6/7: Checking and upgrading tables
Aug 10 08:57:10 ubuntu21 /etc/mysql/debian-start[9252]: Processing databases
Aug 10 08:57:10 ubuntu21 /etc/mysql/debian-start[9252]: information_schema
Aug 10 08:57:10 ubuntu21 /etc/mysql/debian-start[9252]: performance_schema
Aug 10 08:57:10 ubuntu21 /etc/mysql/debian-start[9252]: Phase 7/7: Running 'FLUSH PRIVILEGES'
Aug 10 08:57:10 ubuntu21 /etc/mysql/debian-start[9252]: OK
lines 1-27/27 (END)

Secure MariaDB installation

Finally, you will want to secure your MariaDB installation by issuing the following command. It will ask some configurational questions, answer them according to your need.

foofunc@ubuntu21:~$ sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Step4: PHP Programming language installation

PHP 7.4 is included by default in Ubuntu 21.04’s repositories. You can also install the latest PHP 8.0 version which was released in Nov. 2020. For that you’ll need to use a third-party repository.

For the sake of simplicity and stability. We are installing PHP 7.4.

sudo apt  install php -y

Verify PHP installation

foofunc@ubuntu21:~$ php -v
PHP 7.4.16 (cli) (built: Jul  5 2021 13:04:38) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies
foofunc@ubuntu21:~$ 

Install PHP 7.4 modules (extensions)

First, check out what are the modules present for PHP in ubuntu 21.04. Use the following command and you will get a long list of the modules, here showing a small part of it.

foofunc@ubuntu21:~$ apt-cache search php
exuberant-ctags - build tag file indexes of source code definitions
libapache2-mod-php - server-side, HTML-embedded scripting language (Apache 2 module) (default)
libapache2-mod-php7.4 - server-side, HTML-embedded scripting language (Apache 2 module)
libct4 - libraries for connecting to MS SQL and Sybase SQL servers
libcups2-dev - Common UNIX Printing System(tm) - Development files CUPS library
libgtksourceview-4-common - common files for the GTK+ syntax highlighting widget
libnet-libidn-perl - Perl bindings for GNU Libidn
php - server-side, HTML-embedded scripting language (default)
php-all-dev - package depending on all supported PHP development packages
php-cgi - server-side, HTML-embedded scripting language (CGI binary) (default)
php-cli - command-line interpreter for the PHP scripting language (default)
php-common - Common files for PHP packages
php-curl - CURL module for PHP [default]
php-dev - Files for PHP module development (default)
php-gd - GD module for PHP [default]
php-gmp - GMP module for PHP [default]

You can install these modules by using the apt command see the following example.

foofunc@ubuntu21:~$ sudo apt install php-cli -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  php-cli

Summary

LAMP is L(Linux) A(Apache) M(Mysql) P(PHP) is a combined package intended for fast and reliable web application development. In this article, we have seen how to install Apache2, MySQL, and PHP on Ubuntu 21.04. Commands were simple and straightforward.

In case you have any questions regarding the topic don’t hesitate to comment.

2 thoughts on “How to install LAMP stack on Ubuntu 21.04”

Leave a Comment

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