How to Install LAMP Stack on Debian 11 Server (Apache, MariaDB, PHP). LAMP is a popular open source web development stack consisting of Linux, Apache web server, MySQL server or MariaDB, and PHP. This article explains how to install a LAMP stack on Debian 11.
LAMP stack is a group of four software components that create a complete environment for application development. Each component is independent and open source, but put together makes them a software stack. LAMP stack software provides a complete web development platform that uses Linux as an operating system, Apache as a web server, MySQL/MariaDB as a database server, and PHP as a processing language. It has four layers in its platform and is suitable for building and deploying dynamic websites and web applications.
The first letters of each software component’s names make up the LAMP acronym:
Linux is a free and open source operating system used to run all components.
Apache is an open source web server used to serve web pages over the internet.
MariaDB/MySQL is a free and relational database management system used to store web contents and data.
PHP is a programming language used to create web applications.
Follow this post to show you how to install LAMP Stack on Debian 11 server.
How to Install LAMP Stack on Debian 11 Server (Apache, MariaDB, PHP)
Install Apache Web Server on Debian 11
Apache web server is one of the key component of LAMP stack. By default, the Apache webserver is included in the Debian 11 default repository. You can install it using the following command:
apt-get install apache2 -y
After installing the Apache webserver, start and enable the Apache service using the following command:
systemctl start apache2
systemctl enable apache2
To check the Apache service status, run the following command:
systemctl status apache2
You will get the Apache running status in the following output:
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2022-05-06 03:18:31 UTC; 7s ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 10216 (apache2)
Tasks: 55 (limit: 4679)
Memory: 8.8M
CPU: 58ms
CGroup: /system.slice/apache2.service
├─10216 /usr/sbin/apache2 -k start
├─10218 /usr/sbin/apache2 -k start
└─10219 /usr/sbin/apache2 -k start
May 06 03:18:31 debian11 systemd[1]: Starting The Apache HTTP Server...
To verify the Apache version, use the following command:
apache2 -v
You will get the Apache version in the following output:
Server version: Apache/2.4.53 (Debian)
Server built: 2022-03-14T16:28:35
You can now open your web browser and access the Apache test page using the URL http://your-server-ip. You should see the Apache test page on the following screen:
MariaDB server is an open source relational database management system used to store application data. By default, the MariaDB server package is included in the Debian 11 default repository. You can install it with the following command:
apt-get install mariadb-server -y
Once the MariaDB is installed, start the MariaDB service and enable it to start after the system reboot.
systemctl start mariadb
systemctl enable mariadb
To check the MariaDB status, run the following command:
systemctl status mariadb
You will get the MariaDB status in the following output:
PHP is an open source programming language that combines all the elements of the LAMP stack. It works with Apache to create dynamic web pages. It is also used to retrieve the data from the database. In a simple terms, it allows web applications to run efficiently.
You can install PHP along with other extensions using the following command:
Once PHP is installed with other extensions, you can verify the PHP version using the following command:
php -v
You will get the following output:
PHP 7.4.28 (cli) (built: Feb 17 2022 16:17:19) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.28, Copyright (c), by Zend Technologies
Next, create a simple PHP page to test PHP processing on the Apache webserver.
nano /var/www/html/info.php
Add the following PHP code:
Save and close the file then open your web browser and access the PHP test page using the URL http://your-server-ip/info.php. You should see the PHP test page on the following screen:
In this section, we will test whether the PHP can connect to the MariaDB database and run the database queries. First, connect to the MariaDB shell with the following command:
mysql -u root -p
Once you are connected, create a database and user with the following command:
CREATE DATABASE wpdb;
CREATE USER 'wpuser'@'%' IDENTIFIED BY 'securepassword';
Next, grant all the privileges to the wpdb database:
GRANT ALL ON wpdb.* TO 'wpuser'@'%';
Please flush the privileges with the following command:
In this section of how to install LAMP Stack on Debian 11 Server (Apache, MariaDB, PHP), we will create a PHP page that connects to the MariaDB database and retrieves the table content:
nano /var/www/html/web_list.php
Add the following code:
Types of Operating System";
foreach($db->query("SELECT content FROM $table") as $row) {
echo "
Now, open your web browser and access the PHP page using the URL http://your-server-ip/web_list.php. If everything is fine, you will get the content you’ve inserted in your table:
How to Install LAMP Stack on Debian 11 Server (Apache, MariaDB, PHP) Conclusion
In this post, we explained how to install LAMP Stack on Debian 11 server. We also explained how to connect to the MariaDB database using PHP and execute queries. I hope you can now easily deploy your PHP application using the LAMP stack.
I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.