LAMP Installation for Ubuntu 20.04 LTS

In this article, we will guide you with the installation of LAMP (Linux,Apache,MySQL,PHP) on your server in order to use it to deploy your websites or web-based applications.

LAMP is a popular, open-source web development platform that can be used to run and deploy dynamic websites and web-based applications. Typically, a LAMP stack consists of Apache webserver, MariaDB/MySQL databases, PHP/Python/Perl programming languages. LAMP is the acronym of Linux, Apache, MariaDB/MYSQL, PHP/Python/Perl. This step-by-step tutorial describes how to install Apache, MySQL, PHP (LAMP stack) on Ubuntu 20.04 LTS server edition.

Installing Apache, MySQL, PHP On Ubuntu 20.04 LTS

In this tutorial, we will be using Ubuntu 20.04 LTS Server Edition.

Install Apache web server In Ubuntu

First of all, update Ubuntu server using commands:

$ sudo apt update
$ sudo apt upgrade

Next, install Apache web server:

$ sudo apt install apache2

Enable and start the Apache Service with the below commands :

$ sudo systemctl enable apache2
$ sudo systemctl start apache2

Check if Apache web server is running or not with the command:

$ sudo systemctl status apache2

Sample output would be:

● apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabl> Active: active (running) since Fri 2021-07-9 01:18:26 UTC; 12s ago Docs: https://httpd.apache.org/docs/2.4/ Main PID: 23996 (apache2) Tasks: 55 (limit: 2283) Memory: 5.1M CGroup: /system.slice/apache2.service ├─23996 /usr/sbin/apache2 -k start ├─23998 /usr/sbin/apache2 -k start └─23999 /usr/sbin/apache2 -k start July 9 01:18:13 ubuntuserver systemd[1]: Starting The Apache HTTP Server...July 9 01:18:13 ubuntuserver apachectl[23981]: AH00558: apache2: Could not reliably det> July 9 01:18:13 ubuntuserver systemd[1]: Started The Apache HTTP Server.

If no errors are indicated on Apache2 status, then your Apache server is running properly.

Allow Apache web server via UFW firewall

By default, the Apache server can’t be accessed from remote systems if you have enabled the UFW firewall in Ubuntu 20.04 LTS. You must allow the http and https ports by following the steps given below.

First, list out the application profiles available on your Ubuntu system using the command:

$ sudo ufw app list

Sample output:

Available applications: 
Apache 
Apache Full 
Apache Secure
OpenSSH

As you can see, Apache and OpenSSH applications have installed UFW profiles. You can list out information about each profile and its included rules using ufw app info "Profile Name" command.

Let us look into the “Apache Full” profile using the command:

$ sudo ufw app info "Apache Full"

Sample output:

Profile: Apache Full
Title: Web Server (HTTP,HTTPS) 
Description: Apache v2 is the next generation of the omnipresent Apache web server. 

Ports: 80,443/tcp

As we can see, "Apache Full" profile has included the rules to enable traffic to ports 80 and 443.

Now, run the following command to allow incoming HTTP and HTTPS traffic for this profile:

$ sudo ufw allow in "Apache Full"

You will see an output like below:

Rules updated 
Rules updated (v6)

If you don't want to allow HTTPS traffic, but only HTTP (80) traffic, run:

$ sudo ufw app info "Apache"

Test Apache Web server

Next step is to open your web browser and access Apache test page by navigating to http://IP-Address/.

If you see the "Apache 2 Ubuntu default page" this means you are good to go. Your Apache server is running properly!

Install MySQL In Ubuntu 20.04

To install MySQL On Ubuntu, run:

$ sudo apt install mysql-server

To enable MySQL service on your server :

$ sudo systemctl enable mysql
$ sudo systemctl start mysql

Verify if MySQL service is running or not using command:

$ sudo systemctl status mysql

Sample output:

● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2021-07-9 01:16:22 UTC; 2min 8s ago
   Main PID: 25242 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 2283)
     Memory: 323.7M
     CGroup: /system.slice/mysql.service
             └─25242 /usr/sbin/mysqld

July 9 01:16:13 ubuntuserver systemd[1]: Starting MySQL Community Server...
July 9 01:18:22 ubuntuserver systemd[1]: Started MySQL Community Server.

As we can see in the above output, MySQL is running!

Setup database as user (root) password for Mysql

By default, MySQL root user password is blank. It is not recommended to use empty password for database administrative account. You need to secure your MySQL server by running the following script:

$ sudo mysql_secure_installation

You will be asked whether you'd like to setup "VALIDATE PASSWORD" component or not. This component allows the users to configure strong password for database credentials. If enabled, It will automatically check the strength of the password and enforces the users to set only those passwords which are secure enough. It is safe to leave it disabled. However, you must use a strong and unique password for database credentials. If you don't want to enable this component, just press any key to skip the password validation part and continue the rest of the steps.

Enter y if you want to setup VALIDATE PASSWORD :

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

If your answer is Yes, you will be asked to choose the level of password validation. The available password validations are low, medium, and strong. Just enter the appropriate number (0 for low, 1 for medium, and 2 for strong password) and press ENTER key.

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: 0

Now, enter the password for MySQL root user. Please keep in mind that you must use a password for MySQL root user depending upon the password policy you choose in the previous step. If you didn't enable the plugin, just use any strong and unique password of your choice.

Please set the password for root here.

New password:

Re-enter new 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

Once you entered the password twice, you will see the password strength (In our case it is 100). If it is OK for you, press Y to continue with the provided password. If not satisfied with password length, press any other key and set a strong password. I am OK with my current password, so I chose y.

For the rest of the questions, just type y and hit ENTER. This will remove anonymous user, disallow root user login remotely and remove the test database.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
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
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
- 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
Success.

All done!

After we complete all the above actions on MySQL installation, then we have set MySQL root user's password.

Install PHP In Ubuntu

In order to install PHP on your server, run:

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

After installing PHP, create info.php file in the Apache root document folder. Usually, the Apache root document folder will be /var/www/html/ or /var/www/ and in our case, on Ubuntu 20.04 LTS, it is /var/www/html/.

Next step is to create the info.php file in the apache root folder:

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

Add the following lines:

<?php
phpinfo();
?>

Press ESC key and type :wq to save and quit the file.

Change the ownership at all files under directory /var/www/html :

sudo chown -R the user that apache service is running:the group that the user that belongs /var/www/html

Restart Apache service to take effect the changes.

$ sudo systemctl restart apache2

Test PHP on your Server

To confirm that PHP has been configured properly,open a web browser and navigate to http://IP-address/info.php URL.

You will see the php information such as version, server API and the details of all enabled php plugins.

  • lamp, apache, mysql, php
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Disable OpenDNS resolver on Windows Server Operating Systems

An open DNS resolver is a DNS server that responds to recursive DNS queries from any IP address...

Disable OpenDNS resolver on Windows Server Operating Systems

An open DNS resolver is a DNS server that responds to recursive DNS queries from any IP address...

Disable OpenDNS resolver on Windows Server Operating Systems

An open DNS resolver is a DNS server that responds to recursive DNS queries from any IP address...

Enable GUI on Ubuntu Server (18.04/20.04)

In this article, we take a look at how to install GUI on Ubuntu server 18.04 and 20.04. Because...

Enable GUI on Ubuntu Server (18.04/20.04)

In this article, we take a look at how to install GUI on Ubuntu server 18.04 and 20.04. Because...