Skip to content

8. Installing the LEMP Stack (Linux, Nginx, MariaDB, PHP)

The LEMP stack is a group of software that works together to host dynamic websites and web applications. LEMP stands for:

  • Linux (Operating System)
  • Engine-x (Nginx web server, pronounced “Engine-X”)
  • MariaDB (Database server)
  • PHP (Programming language)

Nginx is a high-performance web server and reverse proxy that will serve your website’s content to visitors.

First, add the repository for the latest Nginx version:

# Add the Nginx repository
sudo add-apt-repository ppa:ondrej/nginx
# Update package lists
sudo apt update

8.2.2. Installing Nginx and Useful Modules

Section titled “8.2.2. Installing Nginx and Useful Modules”

Install Nginx along with some helpful modules:

# Install Nginx with additional modules
sudo apt install nginx libnginx-mod-http-cache-purge libnginx-mod-http-headers-more-filter libnginx-mod-http-brotli-filter libnginx-mod-http-brotli-static

These modules provide:

  • cache-purge: Allows purging of content from Nginx’s cache
  • headers-more-filter: Gives more control over HTTP headers
  • brotli-filter/static: Enables Brotli compression for better performance than gzip

If Nginx fails to start with an IPv6-related error:

# Check Nginx status
sudo systemctl status nginx

You might see an error like:

nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)

This occurs when IPv6 is disabled but Nginx is configured to listen on IPv6 addresses. To fix this:

# Edit the default site configuration
sudo vim /etc/nginx/sites-available/default

Find and comment out the IPv6 listening directive:

# Change this line:
listen [::]:80 default_server;
# To this:
#listen [::]:80 default_server;

After making changes, test the configuration and start Nginx:

# Test the Nginx configuration
sudo nginx -t
# Start Nginx
sudo systemctl start nginx
# Check Nginx status
sudo systemctl status nginx
# Ensure Nginx starts at boot
sudo systemctl is-enabled nginx

Verify that Nginx is serving content correctly:

# Test using curl (replace with your server's IP address)
curl -i http://your_server_ip
# View the default page content
cat /var/www/html/index.nginx-debian.html

Verify the installed Nginx version:

# Check Nginx version
nginx -v

MariaDB is a robust, open-source database server that will store your WordPress data.

# Install MariaDB server
sudo apt install mariadb-server
# Check MariaDB status
sudo systemctl status mariadb
# Ensure MariaDB starts at boot
sudo systemctl is-enabled mariadb

Run the security script to set a root password and remove insecure defaults:

# Run the MariaDB secure installation script
sudo mysql_secure_installation

Follow the prompts to:

  1. Set a root password
  2. Remove anonymous users
  3. Disallow root login remotely
  4. Remove test database
  5. Reload privilege tables

PHP is the programming language that WordPress is built on. We’ll install PHP-FPM (FastCGI Process Manager) for better performance with Nginx.

# Add the PHP repository
sudo add-apt-repository ppa:ondrej/php
# Update package lists
sudo apt update

8.4.2. Installing PHP and Required Extensions

Section titled “8.4.2. Installing PHP and Required Extensions”

Install PHP 8.3 with extensions needed for WordPress:

# Install PHP 8.3 with extensions
sudo apt install php8.3-fpm php8.3-gd php8.3-mbstring php8.3-mysql php8.3-xml php8.3-xmlrpc php8.3-opcache php8.3-cli php8.3-zip php8.3-soap php8.3-intl php8.3-bcmath php8.3-curl php8.3-imagick php8.3-ssh2

These extensions provide:

  • fpm: FastCGI Process Manager for Nginx integration
  • gd: Image processing library
  • mbstring: Multi-byte string handling
  • mysql: MariaDB/MySQL database connectivity
  • xml/xmlrpc: XML processing capabilities
  • opcache: Improves PHP performance by caching precompiled script bytecode
  • And more essential extensions for WordPress functionality
# Check PHP-FPM status
sudo systemctl status php8.3-fpm
# Ensure PHP-FPM starts at boot
sudo systemctl is-enabled php8.3-fpm
# Verify PHP version
php -v

To make Nginx work with PHP, you need to configure it to pass PHP requests to PHP-FPM:

# Edit the default Nginx site configuration
sudo vim /etc/nginx/sites-available/default

Find the section for PHP processing (usually commented out) and update it to:

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
}

Test and reload Nginx:

# Test the configuration
sudo nginx -t
# Reload Nginx
sudo systemctl reload nginx

Create a test PHP file to verify that PHP is working with Nginx:

# Create a PHP info file
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Visit http://your_server_ip/info.php in your browser. You should see the PHP information page.

Your LEMP stack is now installed and configured, providing a solid foundation for installing WordPress in the upcoming sections.