Hamsa K
Editor
7 min read | 4 years ago

How to Install LAMP stack on Ubuntu 20 04 LTS

Install Php,Apache and Mysql on ubuntu 20.04

LAMP stands for Linux, Apache, MariaDB/MySQL and PHP, all of which are open source and free to use. LAMP stack is a popular, open source web development platform that can be used to run and deploy dynamic websites and web-based applications.

Before start of installation we are going to update repositories and packages once.

and also You must have root or sudo privileged user access to Ubuntu system.

sudo apt update && sudo apt -y upgrade

once update is done, then install mariadb with following steps

Step 1: Install Apache

sudo apt install apache2

once apache is installed start the service and enable it to start on boot.

sudo systemctl start apache2.service
sudo systemctl enable apache2.service

Step 2: Install Mariadb

MariaDB is a relational database management system forked from MySQL. It is free and Open source.

sudo apt install mariadb-server mariadb-client

check mariadb is running or not and then enable service to Auto start on boot

sudo systemctl status mariadb
sudo systemctl enable mariadb

Run the command below to secure your database server.

sudo mysql_secure_installation

when it promts to answer like below.

Enter current password for root (enter for none): press  Enter

Set root password? [Y/n]: Y or press Enter

New password: Enter password

Re-enter new password: Repeat  same password

Remove anonymous users? [Y/n]: Y or press Enter

Disallow root login remotely? [Y/n]: Y or press Enter

Remove test database and access to it? [Y/n]:  Y or press Enter

Reload privilege tables now? [Y/n]:  Y or press Enter

sample output:

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
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n]
 ... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n]
 ... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n]
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n]
 ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

To verify and validate that MariaDB is installed and working, login to the database console using below command.

sudo mysql -u root -p

sample output:

sudo mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 55
Server version: 10.3.22-MariaDB-1ubuntu1 Ubuntu 20.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>

type exit. Now install php and related modules.

Step 3: Install PHP

install latest php version 7.4 and few related modules as shown below.

apt install php7.4 libapache2-mod-php7.4 php7.4-bcmath php7.4-xml php7.4-gd php7.4-mysql php7.4-zip php7.4-fpm php7.4-soap

Restart Apache service to load php.

sudo systemctl restart apache2

Step 4: Adjust firwall rules

If firewall is running on your system, then you have to allow 80 and 443 ports or service name directly.

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Step 5: Test Apache and php

To verify Apache is running, navigate to your server's IP address with your web browser. You will see the default Apache2 page.

http://192.168.0.123

Replace your system ip in above url. Now follow below step to test php

echo '<?php phpinfo(); ?>' > /var/www/html/info.php

To verify PHP is working, navigate to your info page in your browser http://192.168.0.123/info.php

Thats'it. we have sucessfully installed LAMP on ubuntu 20.04 Lts server.

 



Warning! This site uses cookies
By continuing to browse the site, you are agreeing to our use of cookies. Read our terms and privacy policy