Maximizing Your Server's Potential: How to Deploy a LAMP Stack on Debian 11
Published: 2023-12-17 01:55:20

When building custom web apps, developers need an operating system, a web server, database management software and a programming language to create and deliver web apps and deploy and host existing applications.

Made of 4 different Open- source software technologies, The **LAMP** stack consists of Linux, as the operating system, Apache, as the web server, MySQL as the database management system, and PHP/Pearl as the programming language which is used for Server-side/Backend development

Because the LAMP stack possesses flexibility, scalability, security, and ease of use, it became a popular, reliable and affordable option for developers to host content and handle a large amount of traffic and Data.

Debian is renowned for its extensive testing processes and lengthy release cycles, which guarantee the stability and bug-free operation of the system. This is highly vital for server installations, which must operate continuously and without interruption.

It also has an efficient package management system that makes it simple to install, update, and manage software packages, including the LAMP stack's constituent parts. For developers and system administrators who must maintain the server, this saves time and effort.

In this tutorial, you will learn the following:

* What is a LAMP stack is* Benefits of using LAMP stack
    
* How to deploy a LAMP stack to a Debian
    

## Prerequisites

* Git, Apache, Wget, Curl
    
* Php 8.0 and its dependencies
    
* Mysql/MariaDB Database
    
* Composer
    
* Debian 11 Server aka (Bullseye). Install here
    

## Installing LAMP

To install the LAMP stack, you must have root access to your server or create a user with root access with the su command.  Next, you need a domain pointed to your server IP to install the SSL certificate.

## Step 1 - Updating and Upgrading Package Installer

Before installing any new software on your system, ensure you start by updating and upgrading to the latest versions and security patches available with the following command:

`sudo apt update sudo apt upgrade`

## Step 2 - Install the wget package

The wget command downloads and retries files and content from various web servers.

Download the wget command using this command:

`sudo apt install wget`

## Step 3 - Install Apache and other packages

Install Apache and other dependencies using the following command:

`sudo apt install -y apache2 apache2-utils`

 \**Note: The* ***\-y*** *is to answer yes to all the prompts.*


## Step 4 - Setup Firewall

Uncomplicated Firewall (UFW) is a tool that helps to protect your computer or server from unwanted network traffic by managing the firewall rules. You can install and enable your firewall settings here.

You can modify your firewall settings using the app profiles that UFW ships with on Debian 11. Here are some steps to set up and adjust your `ufw` for your Debian 11 server:

Run the following command to view all application profiles:

`sudo ufw app list`

You will see the list of available application profiles

```plaintext
Output
      Available applications:
      Apache
      Apache Full
      Apache Secure
      OpenSSH
```

1. By default, all incoming traffic is automatically blocked by UFW, but all outgoing traffic is permitted. Use the next command to verify the default policies.

    
    `sudo ufw default deny incoming`
    
    `sudo ufw default allow outgoing`

    
2. Configure the firewall rules
    Add rules for specific ports, services, or IP addresses to configure the firewall rules. For example, to allow incoming traffic on port 80 (HTTP), use                             the following command:

  `sudo ufw allow 80/tcp`

## Step 5 - Check status to see if Apache is running

After installation, Apache starts up instantly and is running.  Run the following command to verify the Apache service's status:

`sudo systemctl status apache2`

If Apache is running, the output would be like this:

```plaintext
     ● apache2.service - The Apache HTTP Server
    Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor 
   preset: enabled)
   Drop-In: /lib/systemd/system/apache2.service.d
            └─apache2-systemd.conf
    Active: active (running) since Tue 2022-02-02 10:29:51 UTC; 5min ago
    Main PID: 10617 (apache2)
     Tasks: 55 (limit: 667)
    CGroup: /system.slice/apache2.service
            ├─10617 /usr/sbin/apache2 -k start
            ├─10619 /usr/sbin/apache2 -k start
            └─10620 /usr/sbin/apache2 -k start
 Feb 02 10:29:51 apache systemd[1]: Starting The Apache HTTP Server…
 Feb 02 10:29:51 apache systemd[1]: Started The Apache HTTP Server.
```


## Step 6 - Install MySQL

The installation of the database system is necessary in order to store and manage data for your website. To install MySQL, run:

`wget https://dev.mysql.com/get/mysql-apt-config_0.8.22-1_all.deb`

Next, install the release package:

`sudo apt install ./mysql-apt-config_0.8.22-1_all.deb`

Then run:

```plaintext
      sudo apt update
      sudo apt install mysql-server
```

After the successful installation of MySQL, start the MySQL service:

`sudo systemctl start mysql`

Run mysql_secure_installation to set root password and secure the installation: 

`sudo mysql_secure_installation`

Log in to MySQL shell as root:

`sudo mysql -u root -p`

Create a database and user, set permissions:

```plaintext
CREATE DATABASE mydb;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'localhost';
```

Restart MySQl to ensure its running:

`sudo systemctl restart mysql`

This should be your output:

```plaintext
mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-02-02 06:12:30 UTC; 17s ago
       Docs: man:mysqld(8)
             http://dev.mysql.com/doc/refman/en/using-systemd.html
   Main PID: 101929 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 1148)
     Memory: 369.3M
        CPU: 805ms
     CGroup: /system.slice/mysql.service
             └─101929 /usr/sbin/mysqld

Feb 02 06:12:29 demo systemd[1]: Starting MySQL Community Server...
Feb 02 06:12:30 demo systemd[1]: Started MySQL Community Server.
```

## Step 7 - Install PHP

Update package index and install PHP packages:

```plaintext
sudo apt update
sudo apt install php libapache2-mod-php php-mysql
```

Verify the PHP version you installed:

`php -v`

Restart Apache web server to load PHP:

`sudo systemctl restart apache2`

## Step 8 - Verify the PHP test page process on your web server :

Create a new file named "info.php" and add it under the custom web root folder, "/var/www/html" using nano:

`nano info.php`

Open the file and add the text below, inside the file:

```plaintext
?php
phpinfo();
```

Save the file and exit.

You can access the test page using your domain name or server-ip address, followed by the script name "info.php":

```plaintext
    http://your_domain/info.php
```

                 OR
```plaintext
      http://server_ip/info.php
```

## Step 9 - Restart Apache after making changes.

`sudo systemctl restart apache2`



Your LAMP stack is now fully deployed on Debian 11.

### Conclusion

The open-source nature, maturity, flexibility, performance and cost-efficiency of LAMP make it a reliable choice for hosting web applications.

This guide has equipped you to host PHP websites and apps by implementing a scalable Apache-MySQL platform. Apache serves as the web server while MySQL manages the database system. Together they provide a flexible LAMP stack foundation to build and serve dynamic PHP-powered applications that can deliver customized content to visitors.