8. Installing the LEMP Stack (Linux, Nginx, MariaDB, PHP)
8.1. Introduction to the LEMP Stack
Section titled “8.1. Introduction to the LEMP Stack”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)
8.2. Installing and Configuring Nginx
Section titled “8.2. Installing and Configuring Nginx”Nginx is a high-performance web server and reverse proxy that will serve your website’s content to visitors.
8.2.1. Adding the Nginx Repository
Section titled “8.2.1. Adding the Nginx Repository”First, add the repository for the latest Nginx version:
# Add the Nginx repositorysudo add-apt-repository ppa:ondrej/nginx
# Update package listssudo apt update8.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 modulessudo apt install nginx libnginx-mod-http-cache-purge libnginx-mod-http-headers-more-filter libnginx-mod-http-brotli-filter libnginx-mod-http-brotli-staticThese 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
8.2.3. Troubleshooting IPv6 Issues
Section titled “8.2.3. Troubleshooting IPv6 Issues”If Nginx fails to start with an IPv6-related error:
# Check Nginx statussudo systemctl status nginxYou 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 configurationsudo vim /etc/nginx/sites-available/defaultFind and comment out the IPv6 listening directive:
# Change this line:listen [::]:80 default_server;
# To this:#listen [::]:80 default_server;8.2.4. Starting and Verifying Nginx
Section titled “8.2.4. Starting and Verifying Nginx”After making changes, test the configuration and start Nginx:
# Test the Nginx configurationsudo nginx -t
# Start Nginxsudo systemctl start nginx
# Check Nginx statussudo systemctl status nginx
# Ensure Nginx starts at bootsudo systemctl is-enabled nginx8.2.5. Testing the Default Website
Section titled “8.2.5. Testing the Default Website”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 contentcat /var/www/html/index.nginx-debian.html8.2.6. Checking Nginx Version
Section titled “8.2.6. Checking Nginx Version”Verify the installed Nginx version:
# Check Nginx versionnginx -v8.3. Installing and Configuring MariaDB
Section titled “8.3. Installing and Configuring MariaDB”MariaDB is a robust, open-source database server that will store your WordPress data.
8.3.1. Installing MariaDB
Section titled “8.3.1. Installing MariaDB”# Install MariaDB serversudo apt install mariadb-server8.3.2. Verifying MariaDB Installation
Section titled “8.3.2. Verifying MariaDB Installation”# Check MariaDB statussudo systemctl status mariadb
# Ensure MariaDB starts at bootsudo systemctl is-enabled mariadb8.3.3. Securing MariaDB
Section titled “8.3.3. Securing MariaDB”Run the security script to set a root password and remove insecure defaults:
# Run the MariaDB secure installation scriptsudo mysql_secure_installationFollow the prompts to:
- Set a root password
- Remove anonymous users
- Disallow root login remotely
- Remove test database
- Reload privilege tables
8.4. Installing and Configuring PHP
Section titled “8.4. Installing and Configuring PHP”PHP is the programming language that WordPress is built on. We’ll install PHP-FPM (FastCGI Process Manager) for better performance with Nginx.
8.4.1. Adding the PHP Repository
Section titled “8.4.1. Adding the PHP Repository”# Add the PHP repositorysudo add-apt-repository ppa:ondrej/php
# Update package listssudo apt update8.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 extensionssudo 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-ssh2These 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
8.4.3. Verifying PHP Installation
Section titled “8.4.3. Verifying PHP Installation”# Check PHP-FPM statussudo systemctl status php8.3-fpm
# Ensure PHP-FPM starts at bootsudo systemctl is-enabled php8.3-fpm
# Verify PHP versionphp -v8.5. Configuring Nginx to Work with PHP
Section titled “8.5. Configuring Nginx to Work with PHP”To make Nginx work with PHP, you need to configure it to pass PHP requests to PHP-FPM:
# Edit the default Nginx site configurationsudo vim /etc/nginx/sites-available/defaultFind 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 configurationsudo nginx -t
# Reload Nginxsudo systemctl reload nginx8.6. Testing PHP Processing
Section titled “8.6. Testing PHP Processing”Create a test PHP file to verify that PHP is working with Nginx:
# Create a PHP info fileecho "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.phpVisit 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.