Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Install a LAMP Stack on Debian 12

Install Apache, MySQL and PHP on Debian 12 (Bookworm), then create a database and test PHP.

VPS and Cloud Servers2 min read3 steps10 screenshots

Requirements

  1. A server that runs Debian 12 (Bookworm).
  2. A non-root user with sudo rights.
  3. Basic knowledge of Linux commands.

Step 1: Update the system

sudo apt update && sudo apt upgrade -y
Updating the system

Step 2: Install Apache

sudo apt install apache2 -y
Installing Apache
sudo systemctl enable apache2 && sudo systemctl start apache2
Starting Apache

Open http://your_server_ip in your browser. You see the Apache default page.

The Apache default page

Step 3: Install MySQL

Debian 12 ships MariaDB in its repositories and not MySQL. You have two choices.

Option A: MariaDB (the simplest)

sudo apt install mariadb-server -y

Option B: MySQL from the MySQL repository

Download the repository package (check the current file name on the MySQL website):

wget https://dev.mysql.com/get/mysql-apt-config_0.8.30-1_all.deb
Downloading the MySQL APT config package
sudo dpkg -i mysql-apt-config_0.8.30-1_all.deb
Installing the config package

In the menu, select MySQL Server & Cluster, press Enter, choose the version you want, then go down to Ok and press Enter.

sudo apt update
sudo apt install mysql-server -y
Installing MySQL server

Secure the database server:

sudo mysql_secure_installation

Step 4: Install PHP

sudo apt install php libapache2-mod-php php-mysql -y
Installing PHP
php -v
The PHP version

Step 5: Make Apache use PHP

Open the directory settings:

sudo nano /etc/apache2/mods-enabled/dir.conf

Make index.php the first file in the list:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

Restart Apache: sudo systemctl restart apache2.

Step 6: Test PHP

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

Add:

<?php
phpinfo();
?>
The PHP info page

Open http://your_server_ip/info.php. Delete the file after the test.

Step 7: Create a database

sudo mysql -u root -p

Then:

CREATE DATABASE your_database_name;
CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 8: Enable mod_rewrite

Many apps (such as WordPress) need it for pretty links:

sudo a2enmod rewrite
sudo systemctl restart apache2

Quick recap

  • Install Apache, a database (MariaDB or MySQL) and PHP.
  • Put index.php first in dir.conf.
  • Test PHP and delete the test file.