Home DebianHow to Install LAMP Stack on Debian 12 (Apache, MariaDB, PHP 8.2)

How to Install LAMP Stack on Debian 12 (Apache, MariaDB, PHP 8.2)

by Knowledge Base Linux
Install LAMP on Debian 12 web stack layers illustration

If you want to install LAMP on Debian 12, you are setting up the classic foundation for hosting dynamic websites: Linux as the operating system, Apache as the web server, MariaDB as the database, and PHP as the scripting language. This stack powers millions of WordPress, Joomla, and custom PHP sites, and Debian 12 (Bookworm) ships stable, well-tested versions of every component. In this guide you will set up the complete stack step by step, secure the database, and create your first Apache virtual host.

Prerequisites – How to Install LAMP Stack on

  • A server or VPS running a fresh Debian 12 installation.
  • A user account with sudo privileges (or root access).
  • A domain name pointing to your server if you plan to serve a public site (optional for testing).
  • Basic familiarity with the Linux command line and a text editor such as nano.

Step 1: Update the System – How to Install LAMP Stack on

Before installing anything, refresh the package index and apply pending updates so you start from a clean, patched base.

sudo apt update
sudo apt upgrade -y

Step 2: Install LAMP on Debian 12 – Apache Web Server

Apache 2.4 is available directly from the Debian repositories. Install it and enable it to start at boot:

sudo apt install apache2 -y
sudo systemctl enable --now apache2
sudo systemctl status apache2

If the UFW firewall is active, allow web traffic through it:

sudo ufw allow 'WWW Full'
sudo ufw status

Open your server’s IP address in a browser. You should see the Apache2 Debian Default Page, which confirms the web server is running.

Step 3: Install MariaDB 10.11

Debian 12 ships MariaDB 10.11 as its default MySQL-compatible database server. Install and enable it:

sudo apt install mariadb-server -y
sudo systemctl enable --now mariadb

Next, run the included hardening script. It sets a root password, removes anonymous users and the test database, and disables remote root login:

sudo mariadb-secure-installation

Verify you can log in to the database console:

sudo mariadb -u root -p -e "SELECT VERSION();"

Applications should never connect as the database root user. Create a dedicated database and user for your site instead:

sudo mariadb -u root -p
CREATE DATABASE mysite_db;
CREATE USER 'mysite_user'@'localhost' IDENTIFIED BY 'a-strong-password';
GRANT ALL PRIVILEGES ON mysite_db.* TO 'mysite_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 4: Install LAMP on Debian 12 – PHP 8.2 and Modules

Debian 12 includes PHP 8.2. Install PHP together with the Apache module and the extensions most applications need:

sudo apt install php8.2 libapache2-mod-php8.2 php8.2-mysql php8.2-cli php8.2-common php8.2-curl php8.2-gd php8.2-mbstring php8.2-xml php8.2-zip -y

Restart Apache so it loads PHP, then confirm the version:

sudo systemctl restart apache2
php -v

Step 5: Test PHP Processing

Create a small test file in the web root with a text editor, so you can verify that Apache is passing PHP files to the interpreter:

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

Inside the editor, type a single line of PHP code that echoes a short test message such as PHP is working on Debian 12, then save and close the file.

Visit http://your-server-ip/test.php in a browser. If the test message appears, Apache is executing PHP correctly. Delete the test file afterwards:

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

Step 6: Create an Apache Virtual Host

The default site works, but a dedicated virtual host is cleaner once you add a domain. Create a document root and an index page:

sudo mkdir -p /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example.com
echo "<h1>It works!</h1>" | sudo tee /var/www/example.com/public_html/index.php

Create the virtual host configuration file:

sudo nano /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>

Enable the site and the rewrite module, test the configuration, and reload Apache:

sudo a2ensite example.com.conf
sudo a2enmod rewrite
sudo apachectl configtest
sudo systemctl reload apache2

Step 7: Set Correct Ownership and Permissions

Apache runs as the www-data user, so your site files must be readable by it, while upload directories need write access. A safe baseline for the virtual host you created is:

sudo chown -R $USER:www-data /var/www/example.com
sudo find /var/www/example.com -type d -exec chmod 750 {} \;
sudo find /var/www/example.com -type f -exec chmod 640 {} \;

Avoid the common shortcut of making everything world-writable (chmod 777). It lets any process on the server modify your site files and is a frequent cause of hacked websites.

Step 8: Tune PHP Settings for Production

The default PHP configuration is conservative. For a typical CMS site, raise the upload and memory limits in the Apache PHP configuration file:

sudo nano /etc/php/8.2/apache2/php.ini
upload_max_filesize = 64M
post_max_size = 64M
memory_limit = 256M
max_execution_time = 120

Also set expose_php = Off in the same file so responses stop advertising the exact PHP version, then restart Apache to apply the changes:

sudo systemctl restart apache2

Troubleshooting

Apache fails to start

Run sudo apachectl configtest to find syntax errors, and check sudo journalctl -u apache2 -e for the exact failure. Port conflicts with another web server are a common cause.

PHP files download instead of executing

The Apache PHP module is missing or disabled. Reinstall libapache2-mod-php8.2, run sudo a2enmod php8.2, and restart Apache.

Database connection errors

Confirm MariaDB is running with sudo systemctl status mariadb, verify the credentials, and make sure the php8.2-mysql extension is installed.

Conclusion

You now know how to install LAMP on Debian 12 from scratch: Apache serves the pages, MariaDB stores the data, and PHP 8.2 runs the application logic. From here you can install WordPress or another CMS on top of the stack, then review the official Debian LAMP documentation for deeper configuration notes. For day-to-day server work, our guide to how to SSH into a Linux server is a useful companion.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.