How to Install LAMP Stack on Debian 11 Server (Apache, MariaDB, PHP)

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.

What is LAMP Stack

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:

Install MariaDB Database Server on Debian 11

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:

				
					● mariadb.service - MariaDB 10.5.15 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-05-06 03:20:16 UTC; 13s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
    Process: 11396 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SUCCESS)
    Process: 11397 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 11399 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $>
    Process: 11465 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 11468 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
   Main PID: 11446 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 18 (limit: 4679)
     Memory: 70.9M
        CPU: 575ms
     CGroup: /system.slice/mariadb.service
             └─11446 /usr/sbin/mariadbd
				
			

Next, you will need to secure the MariaDB installation and set the root password. You can do it with the following command:

				
					mysql_secure_installation
				
			

You will be asked to set the root password as shown below:

				
					Press y|Y for Yes, any other key for No:
Please set the password for root here.

New password:

Re-enter new password:

				
			

Set your root password and press the Enter key. You will be asked to remove the anonymous users:

				
					Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
				
			

Type Y and press the Enter key. You will be asked to disallow remote root login:

				
					Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
				
			

Type Y and press the Enter key. You will be asked to remove the test database:

				
					Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
				
			

Type Y and press the Enter key. You will be asked to reload the privilege table:

				
					Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
				
			

Type Y and press the Enter key to finish applying all changes:

				
					Success.

All done!

mariadb --version
mariadb  Ver 15.1 Distrib 10.5.15-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper
				
			

Install PHP Language

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:

				
					apt-get install php libapache2-mod-php php-mysql -y
				
			

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:

				
					<?php
phpinfo();
?>
				
			

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:

Test Database Connection from PHP

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:

				
					FLUSH PRIVILEGES;
				
			

Create a table with the following command:

				
					CREATE TABLE wpdb.website_list (
	item_id INT AUTO_INCREMENT,
	content VARCHAR(255),
	PRIMARY KEY(item_id)
);
				
			

Then insert some data into table with the following command:

				
					INSERT INTO wpdb.website_list (content) VALUES ("mywebsite.com");
INSERT INTO wpdb.website_list (content) VALUES ("ubuntu.org");
INSERT INTO wpdb.website_list (content) VALUES ("google.com");
INSERT INTO wpdb.website_list (content) VALUES ("wordpress.com");

				
			

Next, verify all inserted data with the following command:

				
					SELECT * FROM wpdb.website_list;
				
			

You will get the following output:

				
					+---------+---------------+
| item_id | content       |
+---------+---------------+
|       1 | mywebsite.com |
|       2 | ubuntu.org    |
|       3 | google.com    |
|       4 | wordpress.com |
+---------+---------------+
				
			

Now exit from the MariaDB with the following command:

				
					EXIT;
				
			

Create PHP Page

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:

				
					<?php
$user = "wpuser";
$password = "securepassword";
$database = "wpdb";
$table = "website_list";

try {
  $db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);
  echo "<h2>Types of Operating System</h2><ol>"; 
  foreach($db->query("SELECT content FROM $table") as $row) {
    echo "<li>" . $row['content'] . "</li>";
  }
  echo "</ol>";
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

				
			

Save and close the file when you are done.

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.

Avatar for Hitesh Jethva
Hitesh Jethva

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.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x