How to Install LAMP stack ( Linux – Apache – MySQL/MariaDB – PHP ) on Debian

Welcome to totatca.com. In this article, we will explore together How to Install the LAMP stack on Debian 12/11/10.

The LAMP stack is a fundamental technology stack widely used for building and deploying dynamic web applications. The term LAMP is an acronym for its key components: Linux (operating system), Apache (webserver), MySQL or MariaDB (database management system), and PHP (programming language).

  • Linux (Operating System):
    • An open-source and stable operating system is often chosen as the foundation for the LAMP stack.
    • Selected for its flexibility, high performance, and compatibility with various applications and projects.
  • Apache (Web Server):
    • The Apache HTTP Server is a popular, powerful, and flexible web server.
    • Handles HTTP requests from users and responds by providing dynamic and static web content.
  • MySQL or MariaDB (Database Management System):
    • Both MySQL and MariaDB are open-source relational database management systems.
    • Used for storing and managing data for web applications.
  • PHP (Programming Language):
    • PHP is an open-source programming language tightly integrated with HTML.
    • Used for building dynamic web pages and interacting with databases.

The LAMP stack provides a synchronous development environment and high performance for web application development. It is favored in the development community for its stability, seamless integration, and flexible scalability.

Prerequisites

  • Operating system: A Debian 12/11/10 server
  • User privileges: root or non-root user with sudo privileges.

Read more

Step 1 – Update system

To ensure that you are using the latest version of software packages on your Debian system, before proceeding with the installation, run the following command to update information about the software packages:

sudo apt update

Step 2 – Install Apache webserver

Run the command below to install the Apache webserver on your system:

sudo apt install apache2 -y

Start and enable the Apache service to automatically run with the system upon each start:

sudo systemctl start apache2

sudo systemctl enable apache2

Check the status of the Apache service, and make it’s running on the system:

sudo systemctl status apache2

Adjust the firewall to allow ports 80 (HTTPS) and 443 (HTTPS) through on the UFW firewall. Skip this step if you are not using the UFW firewall.

sudo ufw enable

sudo ufw allow 80

sudo ufw allow 443

sudo ufw reload

sudo ufw status

Once the installation of the web server with Apache is complete, try accessing the address http://IP-Server or http://FQDN .If the result matches the image, it means you have successfully installed it.

Step 3 – Install MySQL/MariaDB server

Run the command below to install the MySQL or MariaDB server on your system:

### for MySQL

sudo apt install mysql-server -y

### for MariaDB

sudo apt install mariadb-server -y

Start and enable the Apache service to automatically launch with the system every time it is rebooted:

### for MySQL

sudo systemctl start mysql

sudo systemctl enable mysql

### for MariaDB

sudo systemctl start mariadb

sudo systemctl enable mariadb

Check the status of MySQL/MariaDB service, and make sure it is running on the system

sudo systemctl status mariadb

Next, we need to configure security for MySQL/MariaDB using the following command:

sudo mysql_secure_installation

You need to answer some questions like the ones below. Take note of the highlighted information.

ttc@ttc05:~$ sudo mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): <== hit Enter
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n   <== Type N and hit Enter
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n]     <== Type Y and hit Enter
New password:      <== Enter your password for root account and hit Enter
Re-enter new password:       <== Re-enter your password for root account and hit Enter
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]       <== hit Enter
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]        <== hit Enter
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]        <== hit Enter
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]        <== hit Enter
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

After setting up, try logging into MySQL/MariaDB with the following command:

sudo mysql -uroot -p

Step 4 – Install PHP

PHP is the most common and widely used programming language that helps web applications deliver dynamic web pages and other functions with the help of various modules. To install the PHP and necessary modules, run the command below:

sudo apt install php libapache2-mod-php -y

After installation, run the following command to check the installed PHP version.

php -v

As in this example, PHP version 8.2 has been installed:

Step 5 – Testing

The LAMP stack has been successfully installed on Debian system. To check if PHP is working well with Apache, create a file named phpinfo.php in the root directory of Apache using the following command:

sudo vim /var/www/html/php.info.php

Press i key to add new content below:

<?php
     phpinfo();
?>

Save the file by pressing Esc, then type the command :x and hit Enter

install-lamp-stack-on-debian

Now, restart the Apache service to apply the changes

sudo systemctl restart apache2

Finally, access the address http://IP-Server/phpinfo.php, if the result appears as shown below, it means everything is perfect

install-lamp-stack-on-debian

Wishing you success!!!

Video

How to Install LAMP Stack on Debian

Leave a Reply

Your email address will not be published. Required fields are marked *