How to install Apache, PHP and MySQL on Debian 12 or 11

Combined Apache, PHP, and MySQL create a basic but popular web development stack known as LAMP (Linux, Apache, MySQL, PHP). This open-source stack is widely used for building PHP-based web applications and websites. In this, article we will discuss the commands used to install the LAMP server on Debian 12 Bookworm or 11 Bullseye.

LAMP comprises a Linux OS running Apache, PHP, and MySQL. Apache is a popular open-source web server managing millions of websites to deliver content over the Internet. It is highly scalable web server software with support for multiple operating systems, such as Linux, Windows, and macOS.

PHP (Hypertext Preprocessor) is not some unknown programming server-side scripting language, instead, it has been used by most web developers around the globe. Often developers use it in combination with a web server such as Apache, Nginx, and more.

Further, PHP language apps can interact with various database systems to store data including MySQL or MariaDB. Well, MySQL is also an open-source relational database management system (RDBMS) widely used to store data for web applications, content management systems (CMS), and e-commerce websites.

1. Update the Debian 12 or 11 Server

The first step you should follow on the Debian server or desktop is to make sure the system is up to date. Therefore, on your command terminal run the given command which refreshes the APT package index cache as well.

sudo apt update && sudo apt upgrade -y

2. Install the Apache Web server

Next, we will install the Apache web server packages that are available to download using the default APT package manager of both Debian 12 and 11.

sudo apt install apache2

3. Start and Enable Apache Service

After completing the installation process, let’s start and enable the Apache service so that it can start automatically every time we reboot our system or in case it crashes.

sudo systemctl start apache2
sudo systemctl enable apache2

To confirm the service status, use:

systemctl status apache2

4. Install PHP on Debian 12 or 11

The default version of PHP to install on Debian would not be the latest one. For example, while doing this article, for Bookworm version 12 the PHP was 8.2, and for 11 Bullseye- PHP7.4.

So, if you want to go for the system’s default PHP versions, then you just need to run:

sudo apt install php

Whereas common PHP extensions can use:

sudo apt install php-{common,mysql,xml,xmlrpc,curl,gd,imagick,cli,dev,imap,mbstring,opcache,soap,zip,intl}

(optional) Now, if you want to have the latest available version of PHP, if not available on your Debian then use a third-party repository called Sury.

Download GPG key

sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg

Add the Sury.org repository.

sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'

Run system update

sudo apt update

After that run the default command we have shown at the beginning of this step.

5. Install MySQL/MariaDB Server

Those who want the complete stack of LAMP servers also have to install MYSQL or MariaDB. And to do that we need to execute the command as mentioned below:

For MySQL:

Unfortunately, MySQL is not available by default to install from the Debian 11 and 12 repositories, therefore, first, we need to configure it manually. See our article: How to install MySQL on Debian 11 or 12.

However, we recommend using MariaDB because it is a fork of MySQL and works exactly like MySQL.

sudo apt install mariadb-server

Other Articles:

2 thoughts on “How to install Apache, PHP and MySQL on Debian 12 or 11”

  1. Typo in “sudo systemclt enable apache2” should be systemctl.
    Typo in “systemctl status apache” should be apache2
    Typo in “sudo ap install php-{common,mysql,xml,xmlrpc,curl,gd,imagick,cli,dev,imap,mbstring,opcache,soap,zip,intl}” missing a t in apt

    Reply
  2. Correction:

    >sudo systemctl enable apache2
    [NOT]> sudo systemclt enable apache2 (ctl vs clt)

    >systemctl status apache2
    [NOT]> systemctl status apache (missing 2)

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.