How to Install LAMP Stack on Ubuntu 20.04 Server or Desktop

How to Install LAMP Stack on Ubuntu 20.04 Server or Desktop easily and correctly

From this tutorial you are going to learn how to install LAMP stack on Ubuntu 20.04 LTS Server/Desktop. First of all What is Stack? Basically a software stack is a bundle of different software kept together providing a complete functionality. Now lets talk about LAMP stack LAMP stands for Linux, Apache, MariaDB/MySQL and PHP, all of these are open source and free to use.

LAMP is one of the most common software stack that helps in empowering many Web applications and dynamic Websites. In Lamp stack We have Linux as the operating system, Apache as the web server, MariaDB/ or MySQL as the database server and PHP as the server-side scripting language which helps in generating dynamics web pages and web applications.

LAMP helps to host many of the famous frameworks like WordPress, Drupal, Joomla etc. In LAMP by default, MariaDB or MySQL databases are managed using CLI (command-line interface), via the MySQL shell. If you want to use a GUI (Graphical User Interface) to perform operations and manage your databases, you can do this by installing PhpMyAdmin, Which is very popular PHP-based web application.

Prerequisites for Install LAMP Stack on Ubuntu 20.04 LTS Server/Desktop

To Install LAMP stack on Ubuntu 20.04 LTS Server or Desktop, we should have a system which is running on Ubuntu 20.04 or Ubuntu based operating system like Lubuntu, Linux lite, etc. on local or remote system.

Steps to Install LAMP Stack on Ubuntu 20.04 LTS Server/Desktop

Below you will find tutorial in detail to Install LAMP Stack on Ubuntu 20.04 LTS, Let begin.

Step 1: Update all software packages

Its the time to update all the repository and packages of software. Its a good practice to update software packages and repositories before installing any application in Linux. We will run these codes at first in terminal.

sudo apt update
sudo apt upgrade

Update and upgrade apt for Installing LAMP Stack on Ubuntu 20.04

Step 2: Apache Web Server installation

Apache is an open-source very popular Web/HTTP server application and is used by numerous hosting providers and websites on the internet. It is very powerful, reliable as well as highly extensible. The name of package for Apache Web Server is Apache2.

Installing Apache

Now lets Install Apache on Ubuntu 20.04 for that enter the below commands

sudo apt install -y apache2 apache2-utils

In the above command apache2 is Apache Web server and apache2-utils installs some useful utilities like Apache benchmark tool(ab), fcgistarter, etc for Apache web server. Once you have completed the installation of Apache Web Server, it should automatically get started as on Ubuntu systemd services are automatically started and enabled to start at system boot, when a package intended to run as a service.

Some extra details on Apache

For other Linux distribution, its a good idea to enable Apache to automatically start at system boot time.

sudo systemctl enable apache2

The configuration files for Apache are located in /etc/apache2 directory and the main configuration file is /etc/apache2/apache2.conf. And the default document root for storing your web files is /var/www/html/.

You can easily check status of Apache with systemctl.

sudo systemctl status apache2
sudo systemctl is-enabled apache2

How to Install LAMP Stack on Ubuntu 20.04 Server or Desktop easily and correctly systemctl 1
Install LAMP Stack on Ubuntu 20.04 – sudo systemctl status apache2
Install LAMP Stack on Ubuntu 20.04 - sudo systemctl is-enabled apache2
Install LAMP Stack on Ubuntu 20.04 – sudo systemctl is-enabled apache2

If Apache is not running, use systemctl to start it.

sudo systemctl start apache2

Testing Apache

To check installed Apache version use the below command:

apache2 -v

Install LAMP Stack on Ubuntu 20.04 - Check Apache version
Install LAMP Stack on Ubuntu 20.04 – Check Apache version

Once the Apache installation is done, we need to test if Apache is installed correctly. To do so open a web browser and use ‘localhost‘ or ‘127.0.0.1‘ or check your server ip.

Install LAMP Stack on Ubuntu 20.04 - Apache default successful
Install LAMP Stack on Ubuntu 20.04 – Apache default successful

Step 3: MariaDB Database Server installation

Lets install MariaDB now which is a major part in Installing LAMP Stack on Ubuntu 20.04. MariaDB is a fork of the popular MySQL database. Its available by default in many of the Linux distributions including Ubuntu and is very popular. MariaDB was developed by former members of MySQL team, Who thought MySQL might become a closed-source product soon due to acquisition by Oracle.

Installing MariaDB

Now to install MariaDB on Ubuntu 20.04, We have to run following commands.

sudo apt install mariadb-server mariadb-client

You can find the MariaDB configuration files under the directory /etc/mysql/. Refer to the MariaDB documentation for details on configuration file and configuration, There are so many configuration files in the directory.

Some extra details on MariaDB

Once installation is done, Its the time to confirm if MariaDB service is running. Also check if the service is enabled to automatically start when your system boots using commands below.

sudo systemctl status mariadb
sudo systemctl is-enabled mariadb

Install LAMP Stack on Ubuntu 20.04 - sudo systemctl status mariadb
Install LAMP Stack on Ubuntu 20.04 – sudo systemctl status mariadb
Install LAMP Stack on Ubuntu 20.04 - sudo systemctl is-enabled mariadb
Install LAMP Stack on Ubuntu 20.04 – sudo systemctl is-enabled mariadb

If MariaDB is not running, You can start it with the command below.

sudo systemctl start mariadb

Incase MariaDB service is not enable automatically to start at boot time, Execute below code

sudo systemctl enable mariadb

Adding Security to MariaDB

Now its the time to add some security measures on our system or server for our MariaDB installation which we just performed, For that we will be using mysql_secure_installation script, which is bundled with MariaDB.

sudo mysql_secure_installation

Once we have initiated the script, We will be going through a series of questions where we can answer Yes[Y] or no[n] for enabling and disabling one or the other security options. By default in a new installation there is no password for root user for MariaDB. password for Because the database system has just been installed, there is no database root (or administrator) user password.

When it asks you to enter MariaDB root password, press Enter key, as the root password not set yet. Now you have to press enter to set the root password for MariaDB server. Further you can set the data as below.

  • Enter current password for root (enter for none): Press Enter
  • Set a root password? [Y/n] Y
  • 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
How to Install LAMP Stack on Ubuntu 20.04 Server or Desktop easily and correctly maria setting configuration

Testing MariaDB

To access the MariaDB shell, run the below code with -u attribute and sudo keyword. If sudo is not used we will get error.

sudo mariadb -u root

If you want to exit from MariaDB shell just type exit.

exit

Step 4: Installing PHP in Ubuntu 20.04

PHP is an acronym for “PHP: Hypertext Preprocessor” and it is a widely-used, open source scripting language. PHP scripts are executed on the server and PHP is free to download and use. It is one of the most popular programming language for web. It is used to build some of the most popular websites and web applications like Facebook, Wikipedia etc.

Installing PHP

Now lets install PHP by executing the following command.

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

Some extra details on PHP

The configuration files for PHP will be located in /etc/php/7.2/. While coding or creating web applications you might have to install some PHP extensions required by your application. You can search a PHP extension as shown.

sudo apt-cache search php | grep php-

#show all php packages

Once you have found the extension, you can install it. Let take an example, say i need a Zip compression module, I can install it using the command below. You can add multiple extension modules separated by spaces like e-module-1 e-module-2.

sudo apt install php-zip

After the installation of PHP extension module, we need to restart apache to be able to use the extension installed recently. That again can be done using systemctl,below is the code.

sudo systemctl restart apache2

Testing PHP

Now it is a good time to test if PHP and Apache are working together as we expect. For that create a PHP file in your favorite text editor or code editor. Name it anything, I am naming it as info.php, Now place this document under web document root /var/www/html/ directory and add the phpinfo(); in the file. I am using vi for this and below are the code and file content.

sudo vi /var/www/html/info.php

<?php
        phpinfo();
?>

Open you web browser and type localhost/testVersion.php or 127.0.0.1/testVersion.php, you should see complete PHP information like configuration settings and available predefined variables, installed modules, and other information.

Install LAMP Stack on Ubuntu 20.04 - Php information
Install LAMP Stack on Ubuntu 20.04 – Php information

You can Check verion in terminal as well using below command

php –version

Step 5: Installing phpMyAdmin in Ubuntu 20.04

Most of the things are done now, But managing the Databases is not easy using console, for that we will install phpMyAdmin which is freely available. It is a Web based graphical user interface for managing databases. It also supports wide range of operations on MySQL and MariaDB databases.

Installing phpMyadmin

Lets install it using below command

sudo apt install phpmyadmin

While the installation is running we will be prompted to choose many items, among these first we need to choose Web server which will be configured to run phpMyadmin. There will be two options Apache and lighttpd you need to select Apache here and press enter to proceed.

Install LAMP Stack on Ubuntu 20.04 - Select Web server for phpMyAdmin
Install LAMP Stack on Ubuntu 20.04 – Select Web server for phpMyAdmin

After Web server selection, we will be prompted with window saying phpMyAdmin must have a database installed and configured before it can be used. Basically its asking to create a database for phpMyAdmin as a part of phpMyAdmin installation. Now on this window select Yes and press enter.

Install LAMP Stack on Ubuntu 20.04 - All to create database for phpMyAdmin
Install LAMP Stack on Ubuntu 20.04 – All to create database for phpMyAdmin

Now we will be prompted to create a password for phpMyAdmin to register with the MariaDB database server. Enter a password, then you will see a a password confirmation prompt, enter the same password again.

Install LAMP Stack on Ubuntu 20.04 - create a password for phpMyAdmin to register with the MariaDB database server
Install LAMP Stack on Ubuntu 20.04 – create a password for phpMyAdmin to register with the MariaDB database server
Install LAMP Stack on Ubuntu 20.04 - password  confirmation for phpMyAdmin to register with the MariaDB database server
Install LAMP Stack on Ubuntu 20.04 – password confirmation for phpMyAdmin to register with the MariaDB database server

Configuring phpMyAdmin

We have completed the installation part, Now we have do some configurations changes. The configuration files for phpMyAdmin are located in the directory /etc/phpmyadmin and main configuration file for phpMyAdmin is /etc/phpmyadmin/config.inc.php.

To make phpMyAdmin work with Apache, We will use the configuration file present at /etc/phpmyadmin/apache.conf. Now we will configure Apache so that it will server phpMyAdmin page. We will use symbolic link or symlink command to link /etc/phpmyadmin/apache.conf to /etc/apache2/conf-available/phpmyadmin.conf and we will enable the phpmyadmin.conf configuration files for Apache2.
At the end we will restart the Apache service for recent changes to work.

sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf

Install LAMP Stack on Ubuntu 20.04 – Creating a /etc/phpmyadmin/apache.conf to /etc/apache2/conf-available/phpmyadmin.conf

sudo a2enconf phpmyadmin.conf

Install LAMP Stack on Ubuntu 20.04 – Enabling configurating in Apache

sudo systemctl reload apache2.service

Install LAMP Stack on Ubuntu 20.04 – Reload Apache
Install LAMP Stack on Ubuntu 20.04 - Enabling phpMyAdmin for Apache
Install LAMP Stack on Ubuntu 20.04 – Enabling phpMyAdmin for Apache

Testing phpMyAdmin

Now we can test phpMyAdmin by opening url localhost/phpmyadmin or 127.0.0.1/phpmyadmin, you will land to phpMyAdmin login page. Here enter root for the username and its password, or another MariaDB user and password if you have any. In case remote login is disabled root user, we can login using phpmyadmin user and password which we created while installing to login to phpMyAdmin.

Install LAMP Stack on Ubuntu 20.04 - phpMyAdmin login page
Install LAMP Stack on Ubuntu 20.04 – phpMyAdmin login page

Once you are logged in successfully, you will see the PhpMyAdmin dashboard. You will be able to see all the databases and options present there to manage and manipulate database, tables, columns, permissions, relations, users, indexes, etc.

Install LAMP Stack on Ubuntu 20.04 - phpMyAdmin after login
Install LAMP Stack on Ubuntu 20.04 – phpMyAdmin after login

This is all about How to Install LAMP Stack on Ubuntu 20.04 LTS. Please comment if your are facing any issues in implementation of the above guide.

One Response

  1. qntdtprwky May 28, 2021