The LAMP stack is a collection of open-source software products that are frequently used in conjunction. The acronym LAMP is used to describe a computer system that has the following components: Linux, Apache HTTP Server (or just server), MySQL and PHP/Perl/Python.

A user can install all of these components separately on a single computer or, more commonly, on separate computers connected by a network; however, some components are dependent upon other components – for instance, it is not possible to install Apache without first installing Linux – hence the standard installation practice is to install all components on a single computer system.

The LAMP stack is the combination of open-source software to form a server environment most commonly used in web development.

Open source refers to something, especially an application or operating system, whose source code is made available for free use or distribution, although certain user restrictions may apply. This allows users to create copies of any product and make whatever changes they like.

Installing the LAMP stack on Debian 11 will be shown in this article. Once complete, you’ll have a web server that serves your sites and applications with ease. Here we show how to install them manually from the terminal if you want full control over your system settings like editing config files yourself.

Updating the System

To begin the “best practices,” you should start by updating the installed packages on your system. You can do this either from the GUI or command-line interface (shell).

sudo apt update
sudo apt upgrade

Installing Apache

The Apache web server is a free, open-source program that can be used on many different types of computer systems. It is the world’s most used web server. The Apache web server project was launched in 1995, and it has played a key role in the growth of the World Wide Web by providing a platform that allows anyone to set up their own website freely.

You can install apache on Debian 11 using the following command. This command will pull all Apache dependencies and support modules.

sudo apt-get install apache2 apache2-utils -y

Once installed, start and enable the Apache service to make Apache load automatically on boot.

sudo systemctl start apache2
sudo systemctl enable apache2

Apache listens on port 80 for HTTP requests and port 443 for HTTPS requests. You can verify this by checking the Apache listening ports with the ss command (similar to netstat ).

ss -antpl | grep apache2

How to install Apache, MariaDB and PHP (LAMP) on Debian 11 Debian linux

To check if the Apache server is running, use the systemctl status command to check its status.

sudo systemctl status apache2

How to install Apache, MariaDB and PHP (LAMP) on Debian 11 Debian linux  Advertisement

We can see that Apache is active and running and enabled on boot. It allocates 15.1MB of memory and has three processes.

The Apache service is running. The best way to test it, though, is to request a web page from the server.

You can request a web page by pointing your browser to your server’s IP address or domain name.

http://

A default Apache page will be shown if the Apache service is running properly on your system.

How to install Apache, MariaDB and PHP (LAMP) on Debian 11 Debian linux

Install MariaDB Database Server

MariaDB is an open-source relational database management system that provides a drop-in replacement for MySQL. MariaDB, like many open-source projects and databases, is free. It’s developed with a spirit of sharing and collaboration in mind—so much so that its name has been translated from Maria Damon Burton to mean ” promoter.” It is officially owned by MariaDB Corporation AB, a Swedish company and the primary developer of MariaDB.

You can install MariaDB on Debian 11 using the following command. This will pull all MariaDB dependencies and support modules.

sudo apt-get install mariadb-server -y

Once installed, start and enable the Mariadb service to make MariaDB load automatically on boot.

sudo systemctl start mariadb
sudo systemctl enable mariadb

To check if the MariaDB server is running, use the systemctl status command to check its status.

sudo systemctl status mariadb

How to install Apache, MariaDB and PHP (LAMP) on Debian 11 Debian linux

MariaDB is a database server program that can be used for storing and retrieving data. It’s more secure when you don’t allow the default configuration settings, which should protect your MariaDB installation from any vulnerabilities. You may want to run the mysql secure installation script provided by mariadb-server package to protect your installation.

sudo mysql_secure_installation

This script takes you through a series of prompts that will help you to make your MariaDB installation secure.

The first prompt asks you to enter the password for the administrative user root. You enter the current password for the root user. Since you haven’t set a root password, just hit Enter for none.

The next prompt asks you to switch to unix_socket authentication, type Y and hit Enter.

How to install Apache, MariaDB and PHP (LAMP) on Debian 11 Debian linux

The following prompt checks if you want to create a database root password. Setting a root password is an important step to securing MariaDB. It ensures that no one may access your database as an administrator without permission and gives you more control over security in general.

Type Y and hit the Enter keyboard. Provide a password of your choice for the MariaDB root user. You will need to use this password while connecting to MariaDB prompt. Choose a password that will be hard to guess but easy for you to remember.

How to install Apache, MariaDB and PHP (LAMP) on Debian 11 Debian linux

For the remaining prompts, you can type Y and press Enter to accept the default values. These prompts are about removing the test database, disallowing remote root login, and reloading privilege tables.

Now that you’ve finished securing your MariaDB installation. You can test your connection by using the mysqladmin utility, which will allow for establishing a database link. For example, the following command allows us to connect as root and display information about our MariaDB installation version.

sudo mysqladmin version

How to install Apache, MariaDB and PHP (LAMP) on Debian 11 Debian linux

Installing PHP Script Language

If you want to make a website that is dynamic and interactive, then PHP programming language should be your choice. It’s widely used in today’s web pages because of its ability for creating great interactivity with different features such as calendars or shopping carts, among others.

You can install PHP with other commonly used modules using the following command.

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

To list all installed PHP module, run the following command.

php -m

How to install Apache, MariaDB and PHP (LAMP) on Debian 11 Debian linux

To check the PHP version installed on your machine, use the php -v command.

php -v

How to install Apache, MariaDB and PHP (LAMP) on Debian 11 Debian linux

You can test your PHP instalation by writing simple php code in a file, putting it in the web root directory, and executing it.

echo "" > /var/www/html/info.php

Restart your apache server to access the php file in your browser.

sudo systemctl restart apache2

Open your web browser and navigate to you_server_ip/info.php, where you_server_ip is your actual server IP.

you_server_ip/info.php

This URL will open a page displaying php information, such as the version and configuration options used during the installation of php binaries.

How to install Apache, MariaDB and PHP (LAMP) on Debian 11 Debian linux

Creating a Virtual Host for Apache

At this point, your LAMP stack has been set up and is ready to work. Give yourself the freedom of hosting multiple websites by creating a virtual host so that you may use this server for all future projects. Virtual hosts are one of the most common ways to run websites with Apache. A virtual host allows you to map several domain names ( or aliases) to one IP address. In most cases, this is done for web servers that host multiple websites with different domain names from a single machine.

In this part, we will create a Virtual Host example.com. Feel free to substitute example.com with your own domain name while following along.

The first step is to create the document root directories for each of the sites you’ll be serving from your machine. In this example, we will create a directory called /var/www/html/example.com

sudo mkdir -p /var/www/html/example.com

Next, assign some permissions to these directories.

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

Now, move into the /www/html/example.com and create an example.html file and populate the file with the following lines.

cd /var/www/html/example.com
sudo nano example.html

Hello World, Vitux.com!

Save and close this file when you are done.

We can now create our virtual host file. Open /etc/apache2/sites-available/example.com.conf with your editor of choice.

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

Populate the file with the following lines.

  ServerAdmin [email protected]
  ServerName example.com
  DocumentRoot /var/www/html/example.com
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined

Save and close this file when you are done.

We can now enable this virtual host and restart Apache2:

a2ensite domain1.com.conf
a2ensite domain2.com.conf
systemctl restart apache2

You can verify the virtual host you just created with the following command.

apache2ctl configtest

If everything is correct, the OK output will be shown.

How to install Apache, MariaDB and PHP (LAMP) on Debian 11 Debian linux

You can also check the status of the Apache service again with the following command.

How to install Apache, MariaDB and PHP (LAMP) on Debian 11 Debian linux

You can test your virtual hosts by going to example.com in a web browser once you’ve set them up.

Make sure that any errors or strange behaviors from the server don’t appear on screen while testing it out, such as having only one-page load for several minutes, the server is giving an error message about not being able get access, etc.

Conclusion

In this blog post, we’ve given you a brief introduction to the LAMP stack and shown you how to install it on a Debian 11 system. We hope that the information provided will help get your web development project started faster. If you have any questions or need further assistance with installation, please let us know in the comment below.