How to Install LAMP on Debian 11

LAMP is a popular software stack that consists of Linux, Apache, MariaDB, and PHP. It allows you to host dynamic websites and web applications on your server. In this tutorial, we will show you how to install and configure LAMP on Debian 11 Bullseye.

Step 1: Update the system

Before installing any packages, it is recommended to update the system and its repositories:

sudo apt update
sudo apt upgrade

This will ensure that you have the latest versions of the software and security patches.

Step 2: Install Apache

Apache is a widely used web server that can serve static and dynamic content. To install Apache on Debian 11, run the following command:

sudo apt install apache2

This will install Apache and its dependencies. You can verify that Apache is running and enabled by checking its status:

sudo systemctl status apache2

You should see something like this:

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-07-31 12:50:53 CEST; 5min ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 1234 (apache2)
      Tasks: 55 (limit: 2286)
     Memory: 13.4M
        CPU: 325ms
     CGroup: /system.slice/apache2.service
             ├─1234 /usr/sbin/apache2 -k start
             ├─1235 /usr/sbin/apache2 -k start
             └─1236 /usr/sbin/apache2 -k start

If Apache is not running or enabled, you can start and enable it with:

sudo systemctl start apache2
sudo systemctl enable apache2

You can also test if Apache is working by visiting your server’s IP address or hostname in your web browser. You should see the default Debian 11 Apache web page.

Step 3: Install MariaDB

MariaDB is a fork of MySQL that provides a robust and scalable database management system. To install MariaDB on Debian 11, run the following command:

sudo apt install mariadb-server mariadb-client

This will install MariaDB server and client along with some common tools. You can verify that MariaDB is running and enabled by checking its status:

sudo systemctl status mariadb

You should see something like this:

● mariadb.service - MariaDB 10.5.12 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-07-31 12:53:21 CEST; 3min ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 2345 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 31 (limit: 2286)
     Memory: 64.3M
        CPU: 1.123s
     CGroup: /system.slice/mariadb.service
             └─2345 /usr/sbin/mariadbd

If MariaDB is not running or enabled, you can start and enable it with:

sudo systemctl start mariadb
sudo systemctl enable mariadb

After installing MariaDB, it is recommended to run a security script that comes with the package. This script will ask you to set a root password, remove anonymous users, disable remote root login, remove test database, and reload privilege tables. To run the script, execute:

sudo mysql_secure_installation

Answer the questions as follows:

  • Enter current password for root (enter for none): Press ENTER as there is no password set by default.
  • Set root password? [Y/n]: Y
  • New password: Enter a strong password of your choice.
  • Re-enter new password: Repeat the password.
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove test database and access to it? [Y/n]: Y
  • Reload privilege tables now? [Y/n]: Y

You can test if you can log in to MariaDB as root with the following command:

sudo mysql -u root -p

Enter the password you set earlier and you should see the MariaDB prompt:

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 42
Server version: 10.5.12-MariaDB-0+deb11u1 Debian 11

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)]>

To exit, type exit or press CTRL+D.

Step 4: Install PHP

PHP is a popular scripting language that can process dynamic content and interact with databases. To install PHP on Debian 11, run the following command:

sudo apt install php php-mysql php-cli php-common php-curl php-xml php-zip php-mbstring

This will install PHP and some common modules that are useful for web development. You can verify that PHP is installed by checking its version:

php -v

You should see something like this:

PHP 7.4.25 (cli) (built: Oct 22 2023 08:28:49) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.25, Copyright (c), by Zend Technologies

To configure PHP, you need to edit the main configuration file located at /etc/php/7.4/apache2/php.ini. You can use your preferred text editor to open the file, such as nano:

sudo nano /etc/php/7.4/apache2/php.ini

There are many options that you can modify in this file, but we will focus on some common ones:

  • memory_limit: This defines how much memory a PHP script can use. By default, it is set to 128M, which is usually enough for most applications. However, you can increase it if you need more memory for your scripts.
  • upload_max_filesize: This defines the maximum size of an uploaded file. By default, it is set to 2M, which is quite low for modern web applications. You can increase it to a higher value, such as 64M, depending on your needs.
  • max_execution_time: This defines how long a PHP script can run before it is terminated. By default, it is set to 30 seconds, which is reasonable for most applications. However, you can increase it if you have scripts that take longer to execute.

To change these settings, find the corresponding lines in the file and modify them as follows:

memory_limit = 256M
upload_max_filesize = 64M
max_execution_time = 60

Save and close the file when you are done. To apply the changes, you need to restart Apache:

sudo systemctl restart apache2

Step 5: Test PHP processing

To test if PHP is working properly with Apache, you can create a simple PHP script that displays information about your server. To do this, create a file named info.php in the document root directory of Apache, which is /var/www/html by default:

sudo nano /var/www/html/info.php

Add the following content to the file:

<?php
phpinfo();
?>

Save and close the file when you are done. Then, visit the following URL in your web browser:

http://your_server_ip/info.php

You should see a page like this:

This page shows various information about your PHP configuration, such as the version, modules, variables, etc. You can use this page to check if your PHP settings are correct and if your modules are loaded.

Once you have verified that PHP is working as expected, you should delete the info.php file as it can expose sensitive information about your server:

sudo rm /var/www/html/info.php

Conclusion

You have successfully installed LAMP on Debian 11 and tested its functionality. You can now use your server to host your own websites and web applications powered by Linux, Apache, MariaDB, and PHP.