How To Install Linux, Apache, MariaDB, PHP (LAMP Stack) on Ubuntu 20.04

THIS DOCUMENT IS ALSO AVAILABLE FOR

LAMP stack ( Linux, Apache, MariaDB / MySQL, and PHP) is the widely used tech stack to host websites and blogs. This stack is mainly used to host content management software such as WordPress, Drupal, Joomla, etc.

Here, we will see how to install LAMP stack on Ubuntu 20.04.

Install LAMP Stack

Install Linux

Here is the guide about the Step by Step installation of Ubuntu 20.04 and Upgrading Ubuntu 18.04 LTS to Ubuntu 20.04 LTS.

Install AMP Stack

In other Linux environments, you need to install the required packages manually. But, in Ubuntu, you can install the whole stack with a single command.

  1. Install LAMP stack on Ubuntu 18.04 – Single Command
  2. Install LAMP stack on Ubuntu 18.04 – Manual (One by one)
You would get MySQL 8.0 as the database server when you install the LAMP stack using the single command. But, at the same time, if you choose to install the LAMP stack manually, you will get MariaDB 10.3 as the database.

Method 1: Install LAMP Stack Manually (One by one)

Now, we will proceed to install AMP (Apache v2.4, PHP v7.4, and MariaDB v10.3) on Ubuntu 20.04.

Install Apache

Update the package repository index.

sudo apt update

Install the apache2 package using the apt command.

sudo apt -y install apache2 apache2-utils
Apache’s default document root is /var/www/html and the main configuration file is /etc/apache2/apache2.conf. Additional configurations like Virtual hosts and modules can be found in /etc/apache2/*-enabled directory.
Install MariaDB

MariaDB has replaced MySQL as a default database server in Ubuntu. To install MariaDB v10.3, run the following command.

READ: How To Install MariaDB On Ubuntu 20.04

sudo apt install -y mariadb-server mariadb-client

Run the mysql_secure_installation command to secure the MySQL installation.

 sudo mysql_secure_installation

Output:


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):  << Just Press Enter
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  << Set MariaDB root password
New password: xxx << Enter New MariaDB root password
Re-enter new password: xxx << Re-Enter MariaDB root 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] Y  << Remove anonymous users
 ... 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] Y  << Disable root login remotely
 ... 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] Y  << Remove test database
 - 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] Y  << Reload tables
 ... 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!
MariaDB uses the Unix Socket authentication mechanism for MariaDB root user, which allows MariaDB root user to log in only from the Unix socket (# prompt or sudo). To disable Unix Socket authentication and enable native password, follow the below steps.

Login as MariaDB root user.

sudo mysql -u root -p

No password required

use mysql;

update user set plugin='mysql_native_password' where user='root';

flush privileges;

quit;
Install PHP

By default Apache server supports serving of static HTML files only, not PHP. To have a PHP support, you would need to install PHP and the PHP extension package for MariaDB/MySQL.

Here, we will install PHP 7.4 on Ubuntu 20.04.

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

Restart the Apache webserver.

sudo systemctl restart apache2

Now, skip to Testing LAMP Stack.

Method 2: Install LAMP Stack In Single Command

Now, we will proceed to install AMP (Apache v2.4, PHP v7.4, and MySQL v8.0) on Ubuntu 20.04.

Update the repository cache using the apt command.

sudo apt update

Paste below the line in the terminal and hit enter.

sudo apt install -y lamp-server^

The above command will search and install the required packages for the LAMP server. Wait for few minutes to let the installation of the LAMP stack complete.

Apache’s default document root is /var/www/html and the main configuration file is /etc/apache2/apache2.conf. Additional configurations like Virtual hosts and modules can be found in /etc/apache2/*-enabled directory.

Run the mysql_secure_installation command to secure the MySQL installation.

sudo mysql_secure_installation

Output:

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: Y  << Enable validate password component to validate the strength of user passwords

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2  << Level of password validation policy
Please set the password for root here.

New password: xxx << Set MySQL root password

Re-enter new password: xxx << Re-Enter MariaDB root password

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y  << Continue
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : Y  << Remove anonymous users
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? (Press y|Y for Yes, any other key for No) : Y  << Disable root login remotely
Success.

By default, MySQL 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? (Press y|Y for Yes, any other key for No) : Y  << Remove test database
 - 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? (Press y|Y for Yes, any other key for No) : Y  << Reload tables
Success.

All done!
MySQL uses the Unix Socket authentication mechanism for MySQL root user when installed from the Ubuntu repository. This allows MySQL root user to log in only from the Unix socket (# prompt or sudo). To disable Unix Socket authentication and enable native password, follow the below steps.

Login as MariaDB root user.

sudo mysql -u root -p

No password required

use mysql;

update user set plugin='mysql_native_password' where user='root';

flush privileges;

quit;

Test LAMP Stack

For testing the PHP, we will place one PHP file on to the default document root of Apache.

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Now open a web browser and type the following into the web address to access the test page:

http://your-ip-addr.ess/info.php

The page will look like below.

PHP Information
PHP Information

Scroll the page down to see the PHP extensions for MySQL/MariaDB.

Database Support
Database Support

Conclusion

That’s All. I hope, by now, you have LAMP stack on Ubuntu 20.04. Please share your feedback in the comments section.

Prev Post
Next Post
comments powered by Disqus