How to Install Apache, MySQL, PHP (LAMP Stack) on Ubuntu 22.04

Channel: Linux
Abstract: Installing Other Required Packages You may also need to install modules like MySQL and other extensions for PHP based on the application requirements.

The LAMP (Linux, Apache, MySQL, and PHP) stack is wildly used for deploying PHP-based applications on Linux systems. The LAMP server installation is pretty easy and straightforward. You need some basic knowledge of the Linux package manager to complete this setup.

Here Linux is an open-source operating system. Apache is a popular web server. MySQL is a relational database server and PHP is the programming language.

This tutorial will help you to install PHP, Apache & MySQL on Ubuntu 22.04 LTS Linux system.

Before We Begin

Assuming that you have a running Ubuntu 22.04 Linux system with sudo (or root) privileged access.

Access your system and open a terminal. It will be good to update the package manager cache and upgrade currently installed packages. To do this execute:

sudo apt update && sudo apt upgrade 

Let’s begin the LAMP (Linux, Apache, MySQL, and PHP) stack installation on Ubuntu 22.04 Jammy Jellyfish Linux system.

PHP Installation

First, you need to decide on the PHP version to install on your system. You can also install multiple PHP versions on a single system. Currently the repository contains PHP 5.6, PHP 7.1, 7.2, 7.3, 7.4 and PHP 8.0, 8.1. The below instruction will install PHP 8.1. Please change the version as per your requirements.

The ondrej/php PPA contains all PHP version’s for Ubuntu systems. So add this repository in your system with command below:

sudo add-apt-repository ppa:ondrej/php 

Now update the apt cache and install PHP 8.1.

sudo apt update 
sudo apt install php8.1 

This will install PHP on your Ubuntu system along with some useful PHP extensions.

Apache Installation

Generally, the PHP installation also installs the Apache and its module on your system. Still, you can run the following commands to confirm the installations.

sudo apt install apache2 libapache2-mod-php8.1 -y 

This will install Apache and start the service.

Now, you need to allow web server ports in the firewall. To allow ports 80 and 443 in the UFW firewall, execute the following commands.

sudo ufw allow 80/tcp 
sudo ufw allow 443/tcp 

Open a web browser on your system and type the server’s IP in the address bar. You will get the default Apache server page

Apache default pageMySQL Installation

The default Ubuntu repositories contain MySQL 8.0. Which can be directly installed using the package manager. To install the available MySQL server version, execute the following command.

sudo apt-get install mysql-server 

Once the installation is finished, you can secure the MySQL server by executing the following command.

sudo mysql_secure_installation 

This will ask for a few questions to secure the MySQL server.

  1. Press ‘y’ to enable validate password plugin. This will allow you to set a strict password policy for user accounts.
    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
  2. Chose the password complexity level. Read the all 3 options and choose one:
    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: 1
  3. Enter a new password and re-enter it. Make sure it matches the complexity level as described above.
    New password: *************
    Re-enter new password: *************
  4. Press ‘y’ to continue with provided password.
    Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
  5. Remove default anonymous users from MySQL server:
    Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
  6. Disable root login from remote systems
    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
  7. Remove test database form MySQL created by default during installation.
    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
  8. Reload all privileges to apply the above changes immediately.
    Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y

You have secured the MySQL server in the LAMP stack on Ubuntu 22.04 Linux system.

Remember that the above password set for the root accounts is used for remote users only. To log in from the same system, just type mysql on terminal.

sudo mysql 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.28-0ubuntu4 (Ubuntu)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

Installing Other Required Packages

You may also need to install modules like MySQL and other extensions for PHP based on the application requirements. Use the following command to find our available PHP extensions.

sudo apt search php8.1-* 

The above command will list all available PHP7 modules for installation, Let’s begin the installation of modules.

sudo apt install php8.1-mysql php8.1-curl php8.1-xml 
Verify Setup

You have successfully completed the installation of Apache, MySQL, and PHP on the Ubuntu 22.04 Linux system. To verify the PHP integration with Apache, create a PHP script (example: info.php) on the website document root and write the below content.

<?php phpinfo(); ?>123<?php   phpinfo();?>

Now access this file in the web browser. It will so all the details about versions and installation.

PHP details by phpinfo() function.Conclusion

This tutorial helped you to set up the LAMP stack on Ubuntu 22.04 LTS system. Now, you can host PHP-based web applications on your server.

Ref From: tecadmin

Related articles