How to Install LAMP Stack in Ubuntu 20.04?


lamp in ubuntu

This post may contain affiliate links/ads and I may earn a small commission when you click on the links/ads at no additional cost to you. As an Amazon Affiliate, I earn from qualifying purchases. You can read my full disclaimer here.

LAMP stack is a web service stack that is used to create dynamic web sites. Most hosting companies use this stack to provide a hosting environment for their user’s website.

LAMP gained its name by the acronyms of the names of 4 original open-source components:

Linux based OS (Ubuntu in our case)

Apache – Web server.

MySQL – A Relational Database Management System.

PHP – A server side scripting language mainly used for web development.

Although, you can replace PHP with Python or Perl and MySQL with MariaDB.

In this post, I am going to show you how to install LAMP stack (Apache, MariaDB, PHP) on Ubuntu 20.04.

Pre-Requisites to install LAMP in Ubuntu

Ubuntu 20.04 running on local computer or remote server.

– Active Internet Connection to download and install packages.

– Basic Linux Knowledge

Steps to install LAMP in Ubuntu

1. Update Software Packages

Before installing lamp in Ubuntu 20.04, let’s update our software repositories and software packages using the following command:

sudo apt update && sudo apt upgrade

2. Install and Configure Apache Web Server

Let’s start by installing Apache web-server.  

Use the following command to install Apache and its utilities on your Ubuntu system.

sudo apt install -y apache2 apache2-utils

Once, installation is finished, the Apache web-server will start automatically.

To check whether Apache is running or not, use the “systemctl” command:

sudo systemctl status apache2
apache running status

(Press “q” to regain terminal if the above command doesn’t exit immediately.)

If it’s not running, then start its service:

sudo systemctl start apache2

Also enable the service, so that it will automatically start at boot time.

sudo systemctl enable apache2

To avoid interruptions due to firewall, let’s allow incoming requests to TCP port 80.

If you are using Iptables firewall, run the following command:

sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT

If you are using UFW firewall, run the following command:

sudo ufw allow http

Now, let’s set www-data (Apache user) as the owner of the document root (also known as web-root), so that apache has full power on the /var/www/html directory.

sudo chown -R www-data:www-data /var/www/html/

Enter your system’s IP address in your web browser (http://your-ip) and you’ll see apache default page stating “It Works!”.

You can also check apache version using:

apache2 -v
apache version

3. Install MariaDB Database

Now, let’s install MariaDB on Ubuntu 20.04.

The following command will install both MariaDB server and client package. 

sudo apt install mariadb-server mariadb-client

After installation, check the status of MariaDB service.

systemctl status mariadb
mariadb running status

If it’s not running, then start the service.

sudo systemctl start mariadb

Also, enable the service, so it will automatically run at boot time.

sudo systemctl enable mariadb

Now, let’s run post-installation security script.

sudo mysql_secure_installation

Press enter, when it asks current MariaDB password.

Then hit “Y” to setup MariaDB root password.

mariadb post installation for install lamp in ubuntu

For all other questions, just hit “y” or Enter key. (Y is capitalised so the default answer for every question is “Yes”)

mariadb post installation for install lamp in ubuntu

MariaDB is successfully installed on your Ubuntu system. To check its version, you can use:

mariadb --version

You can also login to MariaDB database using terminal command:

sudo mariadb -u root

(You don’t need any login password while you are accessing MariaDB from your local OS)

To exit MariaDB run:

exit;

4. Install and Configure PHP

Finally, let’s install the last piece of stack.

Now, before starting let me tell you something.

There are 2 ways to run PHP code with the Apache webserver. Normally the Apache PHP module works fine but in some cases, you’ll need PHP-FPM (FastCGI Process Manager) to run your PHP code.

After installing PHP, you can switch between the two modules.

As of writing this post PHP 7.4 is the latest and stable version of PHP.

To install PHP with its necessary libraries and PHP-FPM run the following command:

sudo 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 php7.4-fpm

Now, restart the Apache service.

sudo systemctl restart apache2

Create a file to test PHP with Apache.

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

Add the following line to the info.php file.

<?php phpinfo(); ?>

Save and exit the file. (In Nano editor use Ctrl + x to exit the file and press ‘y’ when it asks to save the file.)

Now, open web browser and navigate to:

http://your-ip/info.php

(Replace “your-ip” with IP address of your server)

You will see information about PHP on your web browser.

Also note, the “Server API” is set to Apache 2.0 Handler

Now, to switch between different modules:

1. Using Apache PHP module

By default, the Apache PHP module is enabled. If it’s not, then you can use the following command to enable it.

sudo a2enmod php7.4

After that restart Apache service for the changes to take effect.

sudo systemctl restart apache2

2. Using PHP-FPM (FastCGI Process Manager)

If you want to use PHP-FPM, then first disable the Apache PHP module.

sudo a2dismod php7.4

Enable proxy_fcgi and setenvif module.

sudo a2enmod proxy_fcgi setenvif

Enable the PHP-FPM configuration file (/etc/apache2/conf-available/php7.4-fpm.conf) using the following command:

sudo a2enconf php7.4-fpm

Restart apache service.

sudo systemctl restart apache2

Now if you navigate to http://your-ip/info.php, you’ll see FPM/FastCGI instead of Apache 2.0 Handler.

Don’t forget to delete the info.php for the sake of server’s security.

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

Conclusion

Congrats! You have successfully installed the LAMP stack (Apache, MariaDB and PHP 7.4) on Ubuntu 20.04.

If you like this post, then follow Techsphinx on Facebook and Twitter for more reviews, tricks, tips and tutorials.


Like it? Share with your friends!

Rahul R Nair

Rahul is obsessed with technology and programming. He is also the founder of TechSphinx. Being a technophile, he is always busy doing some techy stuff or learning about the latest technologies. When not busy with his usual routine (staring at the computer screen) he likes to write and share his knowledge with the world.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x