Install LEMP Stack Nginx MariaDB PHP on Debian 12 with Easy Steps

Posted on

Install LEMP Stack Nginx MariaDB PHP on Debian 12 with Easy Steps

Install LEMP Stack Nginx MariaDB PHP on Debian 12 with Easy Steps

This tutorial is designed to guide you through the process of installing a LEMP stack – Nginx, MariaDB, and PHP – on Debian 12 Bookworm. A LEMP stack is a powerful and popular combination of software used for web development and hosting. Debian 12 comes with Nginx 1.22, MariaDB 10.11, and PHP 8.2. This guide specifically uses MariaDB as the database management system. Follow the steps below to Install LEMP Stack Nginx MariaDB PHP on Debian 12.

Before you begin to Install LEMP Stack Nginx MariaDB PHP on Debian 12, ensure you have the following prerequisites:

  • Access to your Debian 12 server as a non-root user with sudo privileges.
  • A basic firewall configured. You can follow the guide on Initial Server Setup with Debian 12 Bookworm for assistance.
  • A domain name pointed to your server’s IP address.

Step 1 – Install Nginx on Debian 12 Bookworm

First, update the system’s package list:

sudo apt update

Next, install Nginx:

sudo apt install nginx -y

Verify that Nginx is running after installation:

sudo systemctl status nginx
**Output**
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: **active** (**running**) since Thu 2023-06-15 05:58:38 EDT; 42s ago
       Docs: man:nginx(8)
    Process: 2812 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process off;
    Process: 2813 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 2838 (nginx)
      Tasks: 3 (limit: 4653)
     Memory: 2.3M
        CPU: 45ms
     CGroup: /system.slice/nginx.service
...

Step 2 – Configure UFW Firewall for Nginx on Debian 12

Assuming you have UFW firewall enabled, allow port 80 for Nginx HTTP traffic:

sudo ufw allow 'Nginx HTTP'

Reload the firewall to apply the changes:

sudo ufw reload

Check the UFW status to confirm the rule is active:

sudo ufw status
**Output**
Status: active

To                         Action      From
--                         ------      ----
Nginx HTTP                 ALLOW       Anywhere
Nginx HTTP (v6)            ALLOW       Anywhere (v6)

Step 3 – Access Nginx Default Page on Debian 12

You can now access the Nginx default landing page by entering your domain name or public IP address in a web browser. To find your public IP address, use:

hostname -I

Or:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's//.*$//'

Then, navigate to:

http://your_domain_or_IP

If you see the Nginx welcome page, the installation was successful.

Step 4 – Install LEMP Stack MariaDB on Debian 12

Install MariaDB server:

sudo apt install mariadb-server -y

Verify that MariaDB is running:

sudo systemctl status mariadb
**Output**
● mariadb.service - MariaDB 10.11.3 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; preset: enabled)
     Active: **active** (**running**) since Thu 2023-06-15 06:06:34 EDT; 26s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 4752 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 15 (limit: 4653)
     Memory: 161.6M
        CPU: 766ms
     CGroup: /system.slice/mariadb.service
...

Run the security script to harden the MariaDB installation:

sudo mysql_secure_installation

Answer the questions as follows (adjust the root password as needed):

Access the MariaDB shell:

sudo mariadb -u root -p

Enter the password you set during the mysql_secure_installation process.

Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 39
Server version: 10.11.3-MariaDB-1 Debian 12

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

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

MariaDB [(none)]>

Exit the MariaDB shell:

MariaDB [(none)]> EXIT;

Step 5 – Install the PHP part of the LEMP Stack on Debian 12

Install PHP, PHP-FPM, and necessary PHP modules:

sudo apt install php php-fpm php-cli php-mysql php-zip php-curl php-xml -y

Verify the PHP installation:

php -v
**Output**
PHP 8.2.7 (cli) (built: Jun  9 2023 19:37:27) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.7, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.7, Copyright (c), by Zend Technologies

Step 6 – Create a Virtual Host for Nginx on Debian 12

Create a directory for your website (e.g., example.com):

mkdir /var/www/html/example.com

Set the correct ownership for the directory:

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

Create an Nginx virtual host configuration file:

sudo vi /etc/nginx/conf.d/example.conf

Add the following content to the file (replace example.com with your actual domain):

server {
  listen 80;
  server_name example.com;

  root /var/www/html/example.com;
  index index.php;
  access_log /var/log/nginx/example_access.log;
  error_log /var/log/nginx/example_error.log;

  client_max_body_size 100M;

  location / {
    try_files $uri $uri/ /index.php$is_args$args;
  }

  location ~ .php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_pass unix:/run/php/php8.2-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}

Save and close the file.

Test the Nginx configuration for errors:

nginx -t
**Output**
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 7 – Verify PHP Installation on Nginx Debian 12

Create a phpinfo.php file in your website directory:

sudo vi /var/www/html/example.com/phpinfo.php

Add the following PHP script:

<?php phpinfo(); ?>

Save and close the file.

Access the PHP page in your browser:

http://domain-name-or-server-ip/phpinfo.php

This will display the PHP information page, confirming that PHP is correctly configured and working with Nginx. This step is vital to Install LEMP Stack Nginx MariaDB PHP on Debian 12.

Alternative Solutions for Setting up a Web Server on Debian 12

While the LEMP stack provides a robust solution, alternative approaches exist for deploying web applications on Debian 12. Here are two different methods:

1. Using Docker Compose

Docker Compose simplifies the deployment of multi-container applications. Instead of manually installing and configuring each component of the LEMP stack, Docker Compose allows you to define the entire stack in a single docker-compose.yml file.

Explanation:

This approach leverages containerization to isolate each component of the stack (Nginx, MariaDB, PHP-FPM) within separate containers. This enhances portability, reproducibility, and simplifies management. Docker Compose orchestrates the containers, handling networking and dependencies.

Code Example (docker-compose.yml):

version: "3.8"
services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./app:/var/www/html
    depends_on:
      - php

  php:
    image: php:8.2-fpm
    volumes:
      - ./app:/var/www/html

  db:
    image: mariadb:latest
    environment:
      MYSQL_ROOT_PASSWORD: your_root_password
      MYSQL_DATABASE: your_database
      MYSQL_USER: your_user
      MYSQL_PASSWORD: your_password
    volumes:
      - db_data:/var/lib/mysql

volumes:
  db_data:

Steps to Deploy:

  1. Create directories for Nginx configuration (./nginx/conf.d) and your application code (./app).
  2. Create an Nginx configuration file (e.g., default.conf) in ./nginx/conf.d similar to the virtual host configuration in the original guide.
  3. Place your PHP application code in the ./app directory.
  4. Run docker-compose up -d to start the stack.

This method offers increased isolation and easier management of dependencies, particularly beneficial for complex applications.

2. Using a Webmin/Virtualmin Control Panel

Webmin is a web-based system administration tool for Unix-like systems. Virtualmin is a Webmin module that simplifies the management of virtual hosts, databases, and other web hosting-related tasks.

Explanation:

Webmin/Virtualmin provides a graphical interface to manage the LEMP stack. It automates many of the configuration tasks, such as creating virtual hosts, setting up databases, and managing users. This is a user-friendly option for those less comfortable with the command line.

Steps to Deploy:

  1. Install Webmin: Follow the instructions on the Webmin website to install Webmin on your Debian 12 server. This usually involves adding the Webmin repository to your system and using apt to install the webmin package.
  2. Install Virtualmin: Once Webmin is installed, log in to the Webmin interface. Virtualmin can be installed directly from within Webmin. Navigate to Webmin -> Webmin Configuration -> Webmin Modules. Upload the Virtualmin module package or install it from the official repository.
  3. Create a Virtual Server: After installing Virtualmin, use the Virtualmin interface to create a new virtual server for your domain. Virtualmin will automatically configure Nginx, create a database, and set up the necessary file system permissions.

While no code example is directly involved, the Virtualmin interface guides you through the configuration process, abstracting away the complexities of manual configuration. This approach significantly reduces the learning curve and simplifies server management, especially for users unfamiliar with command-line administration.

By utilizing Docker Compose or Webmin/Virtualmin, you can achieve similar results to the manual LEMP stack installation with varying degrees of automation and complexity. Choose the method that best suits your technical expertise and project requirements.