In this guide, we will show you how to install the LAMP stack on Ubuntu 20.04. A software stack is a collection of software tools bundled together. LAMP stands for Linux, Apache, MariaDB/MySQL and PHP, all of which are open source and free to use. It is the most common software stack that supports dynamic websites and web applications. Linux is an operating system; Apache is a web server; MariaDB/MySQL is a database server and PHP is a server-side scripting language responsible for creating dynamic web pages.
Updating software packages
Before we install the LAMP stack, it is recommended to update the repository and software packages. Run the following command:
apt update && apt upgrade

We did not have an update and we continue immediately on.
Apache web server installation
Enter the following command to install the Apache web server. The apache2-utils package will install some useful utilities such as the Apache HTTP Server Test Tool (ab).
apt install -y apache2 apache2-utils

Once installed, Apache should automatically start. Check its status with systemctl.
systemctl status apache2

If it’s not running, use systemctl to start it.
systemctl start apache2
It is also recommended to enable Apache to start automatically at system boot time with the command below:
systemctl enable apache2
Check your Apache version:
apache2 -v

Now enter the IP address of your server in the address bar of your browser. You should see the inscription “It works!” Web page, which means that the Apache web server is working correctly.

Now we need to set www-data (Apache user) as the owner of the folder root (otherwise known as web root). By default, it is owned by the root user.
chown www-data:www-data /var/www/html/ -R
By default, Apache uses the system hostname as its global ServerName file. If the system hostname cannot be resolved in DNS, you will probably see the following error after running the apache2ctl -t command.
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
To solve this problem, we can set the global ServerName file in Apache. Use the “nano” text editor to create a new configuration file.
nano /etc/apache2/conf-available/servername.conf
Add the following line to this file:
ServerName localhost
Save and close the file. Press Ctrl+X then press Enter to confirm. Then enable the config file with the command:
a2enconf servername.conf
Restart Apache for the changes to take effect:

Now, if you run apache2ctl -t again, you won’t see the above error message.
Installing the MariaDB Database Server
MariaDB is a replacement for MySQL. It is developed by former members of the MySQL team who are concerned that Oracle might turn MySQL into a closed source product. Enter the following command to install MariaDB:
apt install mariadb-server mariadb-client
After installation, the MariaDB server should automatically start. Use systemctl to check its status:
systemctl status mariadb

If it is not running, start it with this command:
systemctl start mariadb
To allow MariaDB to start automatically during system boot, run the command:
systemctl enable mariadb
Now run the security (configuration) script after installing MariaDB with the command:
mysql_secure_installation
When it asks you to enter the MariaDB root password, press the Enter key as the root password has not been set yet. Then type y to set the root password for the MariaDB server.
You can then press Enter to answer any remaining questions, which will remove the anonymous user, disable remote root login, and delete the test database. This step is the main requirement to secure the MariaDB database.

By default, the MariaDB package on Ubuntu uses a unix_socket to authenticate the user on login, which basically means you can use the OS username and password to login to the MariaDB console. So you can run the following command to login without providing the MariaDB root password.
mariadb -u root
To exit the MariaDB console, run the command:
exit;
Check the MariaDB server version information:
mariadb --version

As you can see, we have installed MariaDB, version 10.3.38.
Installing PHP
PHP 7.4 is the latest stable version of PHP for Ubuntu 20.04 and has a slight performance advantage over PHP 7.3. Enter the following command to install PHP 7.4 and some common PHP modules:
apt install php7.4 libapache2-mod-php7.4 php7.4-mysql php-common php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline
Enable the Apache PHP 7.4 module, then restart the Apache web server.
a2enmod php7.4 systemctl restart apache2

Check the PHP version information:

We see that we have installed correctly, the version of PHP 7.4.
To test PHP scripts with the Apache server, we need to create an info.php file in the root folder of the web server where visitors are looking at it from.
nano /var/www/html/info.php
Paste the following PHP code into the file:
<?php phpinfo(); ?>
Now go to the info page using the link:
http://IP-ADDRESS/info.php

You should see your server’s PHP information. This means that PHP scripts can work properly with the Apache web server.
How to run PHP-FPM with Apache
There are two main ways to run PHP code on the Apache web server:
- Apache PHP Module
- PHP-FPM.
The steps above use the Apache PHP 7.4 module to process PHP code, which is usually fine. But in some cases, you need to run PHP code using PHP-FPM instead.
Disable the Apache PHP 7.4 module with the command below:
a2dismod php7.4
Install PHP-FPM with the command:
apt install php7.4-fpm
Enable proxy_fcgi and setenvif module with command:
a2enmod proxy_fcgi setenvif
Include /etc/apache2/conf-available/php7.4-fpm.conf config file:
a2enconf php7.4-fpm
Restart Apache for the changes to take effect, use the command below:
systemctl restart apache2
Now, if you refresh the info.php page in your browser, you will find that the server API has changed from Apache 2.0 Handler to FPM/FastCGI, which means Apache web server will pass PHP requests to PHP-FPM.

Important!!! For the security of your server, you should remove the info.php file now to prevent a possible attack on your server’s weaknesses from PHP.
rm /var/www/html/info.php
Conclusion
We hope that it was very useful for you to teach how you can install LAMP on your server running Ubuntu 20.04.