Friday, November 15, 2024
Google search engine
HomeGuest BlogsHow To Install LAMP Stack on Ubuntu 22.04|20.04|18.04

How To Install LAMP Stack on Ubuntu 22.04|20.04|18.04

This guide will discuss how you as a user can install and setup Apache, MariaDB and PHP ( LAMP Stack) on Ubuntu 22.04|20.04|18.04 Linux system. LAMP is an acronym for – Linux, Apache, MySQL/MariaDB and PHP. LAMP Stack is not a single package but a set of open-source tools that are used to power web applications and websites. Each component can be used independently to serve an application.

LAMP Stack comprises of the following open source software applications.

  • Linux – This is the operating system hosting the Applications.
  • Apache – Apache HTTP is a free and open-source cross-platform web server.
  • MySQL/MariaDB – Open Source relational database management system.
  • PHP – Programming/Scripting Language used for developing Web applications.

You can use a Virtual Machine on Premise, in the cloud or a dedicated server to install and configure LAMP Stack on Ubuntu22.04/20.04 operating system. A use account used in this setup needs sudo privileges to install software, edit configuration files, and manage services.

Step 1: Update Ubuntu System

Before we can start installation of LAMP Stack packages on Ubuntu, it is recommended to keep the repository and packages up to date. Run the commands below to update your system.

sudo apt update && sudo apt -y upgrade

Step 2: Install MariaDB Database Server

MariaDB is a relational database management system forked from MySQL. It is free and Open source. Install it using the guide below.

sudo apt update
sudo apt install mariadb-server mariadb-client

After the installation, ensure you secure the database server. This includes:

  • Setting strong root password
  • Removing anonymous users
  • Disabling remote login for root user.
  • Removing test database and access to it

Run the command below to secure your database server.

$ 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] 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? [Y/n] Y
  … 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
 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
  … 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!

Test MariaDB database installation.

$ sudo mysql -u root -p
 Enter password: 
 Welcome to the MariaDB monitor.  Commands end with ; or \g.
 Your MariaDB connection id is 67
Server version: 10.6.12-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.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)]> SELECT VERSION();
 +----------------------------------+
| VERSION()                        |
+----------------------------------+
| 10.6.12-MariaDB-0ubuntu0.22.04.1 |
+----------------------------------+
1 row in set (0.001 sec)
 MariaDB [(none)]> EXIT

Step 3: Install Apache Web Server

Apache Web server packages are available on Ubuntu official repositories. All that’s needed is execution of install command with sudo.

sudo apt install -y apache2 apache2-utils

Confirm Apache build and version.

$ sudo apache2 -v
Server version: Apache/2.4.52 (Ubuntu)
Server built:   2023-05-03T20:02:51

Service is started automatically after installation.

$ systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-08-21 08:51:55 UTC; 24s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 2678 (apache2)
      Tasks: 55 (limit: 18678)
     Memory: 5.3M
        CPU: 84ms
     CGroup: /system.slice/apache2.service
             ├─2678 /usr/sbin/apache2 -k start
             ├─2679 /usr/sbin/apache2 -k start
             └─2680 /usr/sbin/apache2 -k start

Aug 21 08:51:55 jammy systemd[1]: Starting The Apache HTTP Server...
Aug 21 08:51:55 jammy systemd[1]: Started The Apache HTTP Server.

You can restart service or reload when a change is made by using systemctl command.

sudo systemctl reload apache2
sudo systemctl enable apache2

To enable the service to start at boot, use

sudo systemctl is-enabled apache2

To view Apache server full status, use apache2ctl command.

sudo apt install elinks
sudo apache2ctl fullstatus 

Your output should be similar to below.

apache full status debian 10

Open server IP address on your browser to see default Apache page.

debian 10 lamp apache default page

Step 4: Install PHP on Ubuntu

Now that we have both Apache and MariaDB installed, the missing piece is PHP. We will install PHP and standard extensions which are commonly used.

sudo apt install php libapache2-mod-php php-cli php-fpm php-json php-mysql php-zip php-gd  php-mbstring php-curl php-xml php-pear php-bcmath

Enable Apache module if not already enabled then restart the Web Server.

sudo a2enmod php*

Confirm your PHP version.

# Ubuntu 22.04
$ php -v
PHP 8.1.2-1ubuntu2.13 (cli) (built: Jun 28 2023 14:01:49) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2-1ubuntu2.13, Copyright (c), by Zend Technologies
root@jammy:~#

# Ubuntu 20.04
$ php -v
PHP 7.4.3 (cli) (built: Aug 17 2022 13:29:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

Create a php script to test your LAMP stack installation.

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

Open your server IP and URL: http://[ServerIP/hostname]/phpinfo.php

php info debian 10 buster

Recommended books to read:

This gives a detailed information about PHP and Apache web server. This marks the end our guide on how to Install LAMP Stack on Ubuntu Linux system.

Other interesting guides:

RELATED ARTICLES

Most Popular

Recent Comments