Secure Your website with Evergreen LEMP Stack Wisdom

A Guide to the Latest Versions and Endless Freshness: Nginx, MariaDB, and PHP

Introduction: A Stack for the Modern Era

In the vast realm of web server configurations, the LEMP stack stands tall, comprising Nginx, MariaDB, and PHP. This guide is not just another outdated tutorial; it’s your ticket to a perpetually up-to-date LEMP stack. No matter which Debian version you embrace, this guide ensures your web server components are always fresh, with Nginx and PHP 8.1 leading the charge.

The LEMP Stack Unveiled: A Symphony of Nginx, MariaDB, and PHP

Why Nginx and MariaDB, you ask? Personal preference plays a role, but the focus here is on a configuration that enables the use of the latest versions of each program. While stability is crucial, the desire for the cutting edge led me to this setup. The following steps guarantee a robust stack, but remember, what works for one may need tweaking for another.

Step 1: Install Necessary Packages

sudo apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring

Step 2: Set Up Repositories

Before configuring your server’s repository, identify your Debian version’s codename using:

sudo lsb_release -sc

For a detailed system overview:

sudo lsb_release -a

Example result:

Distributor ID: Debian
Description: Debian GNU/Linux 12 (bookworm)
Release: 12
Codename: bookworm

Use the codename in the subsequent configuration.

Nginx: Embrace the Latest

Download Signing Key and Add Repository

curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/debian $(lsb_release -cs) nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

For mainline Nginx packages:

echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/mainline/debian $(lsb_release -cs) nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

Update and install the latest Nginx:

sudo apt update && sudo apt install nginx

PHP: Unleash the Power of 8.2

Add Repository and Download Key

sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg

Update and install PHP 8.2:

sudo apt update && sudo apt install php8.2-fpm
sudo apt install php8.2

Set up repository pinning for official sources:

echo -e "Package: php*\nPin: origin packages.sury.org\nPin-Priority: 900\n" \
    | sudo tee /etc/apt/preferences.d/99php

Check your PHP version:

sudo php -v

Extra PHP Steps

Install additional PHP modules (for WordPress, for instance):

sudo apt install php-bcmath php-curl php-gd php-imagick php-intl php-json php-mcrypt php-mysql php-ssh2 php-xml php-zip php-apcu php-mbstring php-soap

MariaDB: The Latest Symphony

Install Required Packages and Add Repository Key

sudo apt install apt-transport-https curl
sudo mkdir -p /etc/apt/keyrings
sudo curl -o /etc/apt/keyrings/mariadb-keyring.pgp 'https://mariadb.org/mariadb_release_signing_key.pgp'

Create a MariaDB repository:

sudo nano /etc/apt/sources.list.d/mariadb.sources

Add the following content:

# MariaDB 10.11 repository list - created 2023-07-30 13:57 UTC
# https://mariadb.org/download/
X-Repolib-Name: MariaDB
Types: deb
# deb.mariadb.org is a dynamic mirror if your preferred mirror goes offline. See https://mariadb.org/mirrorbits/ for details.
# URIs: https://deb.mariadb.org/10.11/debian
URIs: https://ftp.bme.hu/pub/mirrors/mariadb/repo/10.11/debian
Suites: sid
Components: main
Signed-By: /etc/apt/keyrings/mariadb-keyring.pgp

Update and install MariaDB:

sudo apt update && sudo apt install mariadb-server mariadb-client mariadb-backup

Configure MariaDB using the installation wizard:

sudo mariadb-secure-installation

Extra Steps for a Seamless Setup

SSL for Added Security

Consider adding a free Let’s Encrypt certificate for secure communication. Refer to the Encrypt SSL Cert for Nginx guide for detailed instructions.

Nginx Configuration Tweaks

Adjust the Nginx configuration:

sudo nano /etc/nginx/nginx.conf

Edit or add the line:

keepalive_timeout 2;

If you encounter port binding errors after installation or restart, resolve them with:

sudo fuser -k 80/tcp

PHP FPM: The Preferred Process Manager

If PHP FPM is your favored PHP process manager, edit your website configuration:

sudo nano /etc/nginx/conf.d/default.conf

Change the line with the PHP section:

location ~ \.php$ {
    try_files $uri =404;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
}

Edit php.ini:

sudo nano /etc/php/8.2/fpm/php.ini

Change the cgi.fix_pathinfo option:

cgi.fix_pathinfo=0

Adjust the web server user for PHP:

sudo nano /etc/php/8.2/fpm/pool.d/www.conf

Find the parameters listed below and set them accordingly:

user = nginx
group = nginx
listen = /run/php/php8.2-fpm.sock
listen.owner = nginx
listen.group = nginx

Reload the PHP FPM service:

sudo systemctl reload php8.2-fpm

PHP Info for Configuration Check

Create an info.php file in your web root:

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

Add the following code:

<?php
phpinfo();
?>

Access the file in your browser:

http://example.com/info.php

Remember to delete the file when no longer needed.

Conclusion: A Future-Proofed LEMP Stack

Congratulations! Your LEMP stack, comprising the latest Nginx, PHP 8.2, and MariaDB, is now a fortress of modernity. Enjoy the benefits of a perpetually fresh configuration, ensuring your web server is ready for whatever the internet throws its way.