This guide will walk you through the process of installing a LAMP stack on a Debian 12 system. The LAMP stack is a very popular compilation of server applications used to host websites on Linux. LAMP stands for Linux, Apache, MariaDB, and PHP. In some guides MySQL may be used, however, MariaDB is faster, more scalable, and has more features.
Table of Contents
Getting Started
Note: I use nano in this guide for editing text files. Feel free to use vim, or any text editor of your choice.
Let’s create a non-root user if one doesn’t exist already. This will be used for installing and administering the web services:
useradd websrv
Set the new user’s password:
passwd websrv
Give the new user sudo privileges:
usermod -a -G sudo websrv
And then let’s login as the new user:
su websrv
Verify that we are logged in as websrv:
whoami
Now let’s update the repository list to prepare for installation:
sudo apt update
Installing MariaDB
Install MariaDB:
sudo apt install mariadb-server mariadb-client -y
Secure the MariaDB installation:
sudo mysql_secure_installation
When prompted to Enter current password for root (enter for none):
simply press Enter without typing a password.
Answer the following prompts accordingly:
Switch to unix_socket authentication [Y/n]
n
Change the root password? [Y/n]
n
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
Installing Apache
Install Apache2:
sudo apt install apache2 apache2-doc -y
Now navigate to your server’s IP address in a web browser. You should see an Apache landing page. If not, check your firewall and networking settings.
Installing PHP
At the time of this writing the newest version available for install on Debian 12 default repository is PHP 8.2.20.
Install PHP:
sudo apt install php php-mysql -y
Create a test PHP script:
sudo nano /var/www/html/test.php
Add the following content:
<?php phpinfo(); ?>
Navigate to your server’s IP address/test.php in a web browser. You should see a PHP information page.
Installing phpMyAdmin
phpMyAdmin is a commonly used web browser administration tool for creating and managing databases.
Install phpMyAdmin:
sudo apt install phpmyadmin -y
When asked to choose which web server to automatically configure, be sure to press the Space bar on your keyboard on apache2
to highlight the option with an asterisk *
, then Tab down to <Ok>
and press Enter.
When asked to Configure database for phpmyadmin with dbconfig-common?
choose <Yes>
When asked to Please provide a password for phpmyadmin to register with the database server.
you can leave this blank and press Enter.
Restart Apache:
sudo systemctl restart apache2
You should now be able to navigate to your server’s IP address/phpmyadmin to load the tool.
To add a new database (before being able to manage in phpMyAdmin):
sudo mysql
This will open the MariaDB CLI program
MariaDB [(none)]>
Replace mywpsite
with your preferred database name, and AstrongSecurepassword%$
to whatever you desire.
CREATE USER 'mywpsite'@'localhost' IDENTIFIED BY 'AstrongSecurepassword%$';
GRANT ALL PRIVILEGES ON . TO 'mywpsite'@'localhost';
FLUSH PRIVILEGES;
Exit MariaDB:
exit
Adding Domains and Subdomains
To add domains or subdomains to your server, first you must create Virtual Host configuration files for the site.
Let’s create a new directory for our website within /var/www, of course replacing examplesite
:
sudo mkdir /var/www/examplesite
Create an index.html:
sudo nano /var/www/examplesite/index.html
Add whatever content you wish as a test webpage (it doesn’t have to be properly formatted HTML):
Hello World!
Create the Virtual Host configuration file for the domain:
sudo nano /etc/apache2/sites-available/examplesite.conf
Add the following content:
#We can set subdomains using separate VirtualHost blocks
<VirtualHost *:80>
ServerName www.examplesite.com
ServerAlias example.com
DocumentRoot “/var/www/examplesite”
</VirtualHost><VirtualHost *:80>
ServerName staging.examplesite.com
DocumentRoot “/var/www/examplestagingsite”
</VirtualHost>
Enable the new VirtualHost site configuration:
sudo a2ensite examplesite
Reload Apache for changes to take effect:
sudo systemctl reload apache2
Update the DNS records for your domain so that the A record points to the IP address of your server. Use a tool like WhatsMyDNS.net to see DNS propagation in real-time. Navigate to your domain/subdomain in your web browser and you should now be able to access the website hosted on your server.