Install LAMP Stack on Debian 11

The LAMP Stack is a common set of open-source tools used to host websites and applications on the web. LAMP Stack is a web development platform that uses Linux as an operating system, Apache as a web server, MariaDB/MySQL as a database server, and PHP as a scripting language. It is so widely used and provides a proven set of software for delivering high-performance web applications.

In this tutorial, we will show you how to install the LAMP stack on Debian 11.

Prerequisites

  • A server running Debian 11.
  • A valid domain name pointed with your server IP.
  • A root password is configured on the server.

Install Apache Web Server

Apache webserver package is included in the Debian 11 default repository. You can install it by just running the following command:

apt-get install apache2 apache2-utils -y

After installing the Apache web server, verify the installed version of Apache with the following command:

apache2 -v

You should see the following output:

Server version: Apache/2.4.48 (Debian)
Server built:   2021-08-12T11:51:47

To start the Apache service, run the following command:

systemctl start apache2

To enable the Apache service so that it starts at system boot, run the following command:

systemctl enable apache2

You can also check the full status of the Apache with the following command:

apt-get install elinks
apache2ctl fullstatus

You should see the following output:

 Apache Server Status for localhost (via ::1)

   Server Version: Apache/2.4.48 (Debian)

   Server MPM: prefork

   Server Built: 2021-08-12T11:51:47

   --------------------------------------------------------------------------

   Current Time: Tuesday, 17-Aug-2021 09:29:41 UTC

   Restart Time: Tuesday, 17-Aug-2021 09:29:33 UTC

   Parent Server Config. Generation: 1

   Parent Server MPM Generation: 0

   Server uptime: 7 seconds

   Server load: 0.05 0.01 0.00

   Total accesses: 0 - Total Traffic: 0 kB - Total Duration: 0

   CPU Usage: u.03 s.02 cu0 cs0 - .714% CPU load

   0 requests/sec - 0 B/second

   1 requests currently being processed, 4 idle workers

By default, Apache runs on port 80, you can check it using the following command:

ss -antpl | grep apache2

You should get the following output:

LISTEN 0      511                *:80              *:*    users:(("apache2",pid=44022,fd=4),("apache2",pid=44021,fd=4),("apache2",pid=44020,fd=4),("apache2",pid=44019,fd=4),("apache2",pid=44018,fd=4),("apache2",pid=44017,fd=4))

You can also check the Apache installation by browsing the URL http://your-server-ip. You should see the Apache test page on the following screen:

Debian default page

Install MariaDB Database Server

MariaDB is a database management system that is used to store and manage data for your website. You can install it using the following command:

apt-get install mariadb-server -y

Once the MariaDB is installed, start the MariaDB service and enable it to start at system boot using the following command:

systemctl start mariadb
systemctl enable mariadb

You can also check the status of the MariaDB service using the following command:

systemctl status mariadb

You should see the following output:

? mariadb.service - MariaDB 10.3.31 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-08-21 04:13:25 UTC; 1min 36s ago
       Docs: man:mysqld(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 1838 (mysqld)
     Status: "Taking your SQL requests now..."
      Tasks: 31 (limit: 2353)
     Memory: 66.1M
     CGroup: /system.slice/mariadb.service
             ??1838 /usr/sbin/mysqld

It is also recommended to secure your MariaDB installation and set a MariaDB root password. You can run the mysql_secure_installation script to secure the MariaDB installation:

mysql_secure_installation

You will be asked to set a MariaDB root password, remove anonymous users, disallow root login remotely and remove the test database as shown below:

Enter current password for root (enter for none): 
Change the root password? [Y/n] Y
New password: 
Re-enter new password: 
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

By default, MariaDB runs on port 3306. You can check it using the following command:

ss -antpl | grep mariadb

You should see the following command:

LISTEN 0      80         127.0.0.1:3306      0.0.0.0:*    users:(("mariadbd",pid=12181,fd=15))

To connect the MariaDB shell, run the following command:

mysql -u root -p

Provide your MariaDB root password and hit Enter to connect to the MariaDB shell. Once you are connected, you should see the following output:

Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 64
Server version: 10.5.11-MariaDB-1 Debian 11

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

Now, run the following command to check the MariaDB version:

MariaDB [(none)]> SELECT VERSION();

You should see the following output:

+-------------------+
| VERSION()         |
+-------------------+
| 10.5.11-MariaDB-1 |
+-------------------+
1 row in set (0.001 sec)

Install PHP Processing Language

PHP is the processing language used to serve PHP applications on the webserver. By default, the version of PHP available in the Debian 11 is PHP 7.4. You can install PHP with other commonly used extensions with the following command:

apt-get install php libapache2-mod-php php-cli php-mysql php-zip php-curl php-xml -y

Once PHP is installed, verify the PHP version using the following command:

php -v

You should see the following output:

PHP 7.4.21 (cli) (built: Jul  2 2021 03:59:48) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.21, Copyright (c), by Zend Technologies

Create a Virtual Host for Apache

At this point, the LAMP stack is installed in your server. Now, you will need to create an Apache virtual host configuration file to host any application.

Here, we will create an example.com directory for hosting a PHP page. You can create it with the following command:

mkdir /var/www/html/example.com

Next, set the ownership of the example.com directory to www-data:

chown -R www-data:www-data /var/www/html/example.com

Next, create an Apache virtual host configuration file with the following command:

nano /etc/apache2/sites-available/example.com.conf

Add the following lines:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName example.com
    DocumentRoot /var/www/html/example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file then activate the example.com virtual host and disable the default virtual host with the following command:

a2ensite example.com.conf
a2dissite 000-default

Next, verify the virtual host file for any syntax error with the following command:

apache2ctl configtest

If everything is fine, you should see the following output:

Syntax OK

Finally, restart the Apache service to apply the configuration changes:

systemctl reload apache2

You can also check the status of the Apache service using the following command:

systemctl status apache2

You should see the following output:

? apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-08-21 04:13:30 UTC; 2min 56s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 2257 (apache2)
      Tasks: 55 (limit: 2353)
     Memory: 5.3M
     CGroup: /system.slice/apache2.service
             ??2257 /usr/sbin/apache2 -k start
             ??2259 /usr/sbin/apache2 -k start
             ??2260 /usr/sbin/apache2 -k start

Aug 21 04:13:30 debian11 systemd[1]: Starting The Apache HTTP Server...
Aug 21 04:13:30 debian11 apachectl[2246]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 216.9>
Aug 21 04:13:30 debian11 systemd[1]: Started The Apache HTTP Server.

Verify PHP Installation on Apache

Next, you will need to create a sample PHP file to serve over the Apache webserver. You can create a phpinfo.php file inside your example.com directory:

nano /var/www/html/example.com/phpinfo.php

Add the following line:

<?php phpinfo(); ?>

Save and close the file then open your web browser and access your PHP page using the URL http://example.com/phpinfo.php. You should see your PHP page on the following screen:

PHP on Debian 11

Conclusion

In the above guide, we explained how to install the LAMP stack on Debian 11. You can now start hosting your first website using the LAMP stack. Feel free to ask me if you have any questions.

Share this page:

0 Comment(s)