main logo

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

lamp stack,

The LAMP stack – a fusion of Linux, Apache, MariaDB, and PHP – is an open-source software stack that empowers developers to create dynamic web applications and websites. This guide will walk you through installing a LAMP stack on a Debian 11 server.

Prerequisites

Before you start, ensure you have a Debian 11 server with a non-root user with sudo privileges and a basic firewall setup. If you don’t have these, you can configure them using the initial server setup guide for Debian 11.

Step 1 – Installing Linux

Linux forms the operating system layer of the LAMP stack. Debian 11 is a popular version of the Linux operating system and is the one we’ll be using for this tutorial. As you install the LAMP stack on a Debian 11 server, the Linux part is already taken care of!

Step 2 – Installing Apache

Apache is a widely used web server software known for its reliability, robustness, and extensive documentation.

Begin by refreshing your package manager cache:

sudo apt update

Next, install Apache using the following command:

sudo apt install apache2

Once the installation is complete, adjust your firewall settings to allow HTTP and HTTPS traffic. Assuming you installed and enabled UFW (Uncomplicated FireWall) following the initial server setup instructions, you can adjust your firewall settings using the following commands:

sudo ufw app list sudo ufw app info "WWW Full" sudo ufw allow in "WWW Full"

You can verify the successful installation of Apache by visiting your server’s public IP address in your web browser:

http://your_server_ip

Step 3 – Installing MariaDB

MariaDB is a community-developed fork of MySQL, used as the database management system in the LAMP stack.

To install MariaDB, use the following command:

sudo apt install mariadb-server

After the installation is complete, it’s advisable to run a security script that comes with MariaDB. This script will help you secure your database system by removing some insecure default settings:

sudo mysql_secure_installation

To connect to the MariaDB server as the administrative database user root, use the following command:

sudo mariadb

Step 4 – Installing PHP

PHP is a server-side scripting language that processes code to display dynamic content to the end user. To install PHP, as well as the PHP modules that allow PHP to communicate with MySQL-based databases like MariaDB and enable Apache to handle PHP files, use the following command:

sudo apt install php libapache2-mod-php php-mysql

You can verify the successful installation of PHP by checking its version:

php -v

Step 5 – Creating a Virtual host for your Website

When using the Apache web server, you can create virtual hosts to host more than one domain from a single server.

Start by creating the root web directory for your domain:

sudo mkdir /var/www/your_domain

Assign ownership of the directory with the $USER environment variable:

sudo chown -R $USER:$USER /var/www/your_domain

Next, open a new configuration file in Apache’s sites-available directory:

sudo nano /etc/apache2/sites-available/your_domain.conf

In this new file, add the following configuration:

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

Save and close the file when you’re done.

Now, enable this virtual host:

sudo a2ensite your_domain

To make sure your configuration file doesn’t contain syntax errors, run the following command:

sudo apache2ctl configtest

Finally, reload Apache so these changes take effect:

sudo systemctl reload apache2

Step 6 – Testing PHP Processing on your Web server

To test that PHP is correctly installed and configured on your server, create a new PHP file named info.php inside your custom web root folder:

nano /var/www/your_domain/info.php

In this new file, add the following PHP code:

<?php phpinfo(); ?>

Save and close the file when you’re done.

To test the script, visit the following URL in your web browser:

http://your_domain/info.php

If you can see the PHP information page in your browser, then your PHP installation is working as expected.

For security reasons, it’s best to remove the info.php file once you’ve finished testing:

sudo rm /var/www/your_domain/info.php

Step 7 – Testing Database connection from PHP (optional)

If you want to test whether PHP can connect to MariaDB and execute database queries, you can create a test table with test data and query for its contents from a PHP script.

Start by connecting to the MariaDB console using the root account:

sudo mariadb

Create a new database and a new user with full privileges on the custom database you’ve created:

CREATE DATABASE example_database; CREATE USER 'example_user'@'%' IDENTIFIED BY 'password'; GRANT ALL ON example_database.* TO 'example_user'@'%'; FLUSH PRIVILEGES;

Next, create a test table and insert some content:

CREATE TABLE example_database.todo_list (item_id INT AUTO_INCREMENT, content VARCHAR(255), PRIMARY KEY(item_id)); INSERT INTO example_database.todo_list (content) VALUES ("My first important item");

Confirm that the data was successfully saved to your table:

SELECT * FROM example_database.todo_list;

Now, create a PHP script that will connect to MariaDB and query for your content:

nano /var/www/your_domain/todo_list.php

In this new file, add the following PHP code:

<?php $user = "example_user"; $password = "password"; $database = "example_database"; $table = "todo_list";  try {      $db = new PDO("mysql:host=localhost;dbname=$database", $user, $password);      echo "<h2>TODO</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’re done.

To test the script, visit the following URL in your web browser:

http://your_domain/todo_list.php

If you can see the content you’ve inserted in your test table, then your PHP environment is ready to connect and interact with your MariaDB server.

How can I use phpMyAdmin with the LAMP Stack on Debian 11?

To utilize the benefits of phpMyAdmin installation with the LAMP Stack on Debian 11, users can easily manage their MySQL databases through a web interface. This allows for efficient and secure database management, making it an essential tool for developers and system administrators working with LAMP stack environments.

Conclusion

Congratulations! You’ve successfully installed a LAMP stack on your Debian 11 server. You now have a flexible base for serving PHP websites and applications to your visitors.

As a next step, you should ensure that connections to your web server are secured by serving them via HTTPS. You can use Let’s Encrypt to accomplish this.

Finally, for a high-performance, scalable, and reliable VPS hosting solution, consider Medha Cloud VPS https://medhacloud.com/platform/vps-hosting/. With Medha Cloud VPS, you can easily manage and scale your applications and websites, ensuring optimal performance and security. Get started today with Medha Cloud VPS!

Rakshitha
Rakshitha
Rakshitha a cloud hosting expert who helps businesses get the most out of their cloud services. She has experience setting up and managing cloud environments to improve performance and scalability. With her cloud certification, Rakshitha provides clear advice on how to use cloud solutions effectively for better business results.
Share
Contents

Related Articles

medhacloud logo
USA:
Medha Cloud Solutions LLC
30 N Gould St Ste R, Sheridan, WY 82801,
Phone: +1 646 775 2855

India:
Medha Cloud Solutions Private Limited
#74, 7th Cross, Krishna Garden InCity Layout. Chikka Kammanahalli, Banneraghatta Road, Bangalore 560083
Phone:+91 93536 44646

E-Mail: sales@medhahosting.com
©Medha Cloud 2024. All rights reserved.