How to Install Apache, MySQL, and PHP (LAMP) Stack on Debian 11

Updated on November 21, 2023
How to Install Apache, MySQL, and PHP (LAMP) Stack on Debian 11 header image

LAMP is a popular open-source web development stack consisting of Linux, Apache, MySQL or MariaDB, and PHP. This guide explains how to install a LAMP stack on Debian 11.

Prerequisites

1. Install Apache

Install Apache and related utilities.

$ sudo apt install apache2 apache2-utils -y

Enable the Apache service to start at system boot.

$ sudo systemctl enable apache2

Start the Apache service.

$ sudo systemctl start apache2

Check the status of the Apache service.

$ sudo systemctl status apache2

2. Install MariaDB

Install the database server.

$ sudo apt-get install mariadb-server -y

Start the MariaDB service and enable it to start at system boot.

$ sudo systemctl enable mariadb
$ sudo systemctl start mariadb

Check the status of the MariaDB service.

$ sudo systemctl status mariadb

Secure the database with the mysql_secure_installation script and answer Y to all questions.

$ sudo mysql_secure_installation

3. Install PHP

Install PHP and common extensions.

$ sudo apt install php php-cli php-mysql libapache2-mod-php php-gd php-xml php-curl php-common -y

4. Configure the Firewall

Allow SSH, HTTP, and HTTPS in the firewall.

$ sudo ufw default deny incoming
$ sudo ufw default allow outgoing
$ sudo ufw allow ssh
$ sudo ufw allow 80/tcp
$ sudo ufw allow 443/tcp
$ sudo ufw enable
$ sudo ufw reload

See our UFW Quickstart Guide for more information.

5. Test the LAMP Stack

Create a test PHP file in Apache's document root folder.

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

Open the location in your browser. Replace example.com with your server's IP address or domain name.

https://example.com/info.php

You should see a PHP information page with details about your configuration.

More Information