Table of Contents
The LEMP stack — Linux, Nginx, MySQL/MariaDB, and PHP — is the gold standard for hosting WordPress on a self-managed server. It delivers better performance than the traditional LAMP stack (which uses Apache) while being straightforward to install and maintain on modern Ubuntu. Whether you’re setting up a new VPS for a client, building your own tech blog, or preparing a production WordPress environment, knowing how to install Nginx, PHP, and MariaDB on Ubuntu for WordPress is a fundamental skill every server administrator needs. This guide covers the complete installation and configuration process on Ubuntu 24.04 LTS.

Why the LEMP Stack Is the Right Choice for WordPress
The LEMP stack replaces Apache with Nginx as the web server, which brings fundamental architectural advantages for WordPress hosting. Nginx uses an asynchronous, event-driven model where a small number of worker processes handle thousands of simultaneous connections. Apache uses a process-per-connection model that creates a new process or thread for each request, consuming far more memory as traffic scales.
For WordPress specifically, this difference is most pronounced during traffic spikes. An Apache server might struggle and return 503 errors when hit with 500 concurrent visitors; a properly tuned Nginx server with PHP-FPM handles the same load gracefully with a fraction of the memory footprint.
MariaDB vs MySQL for WordPress
MariaDB is a community-developed fork of MySQL, created by MySQL’s original developers after Oracle’s acquisition of MySQL. For WordPress, MariaDB is functionally identical — WordPress cannot tell the difference at the application level. MariaDB typically offers slightly better performance on read-heavy workloads, is fully open-source, and is the default database in Ubuntu’s repositories. Either works perfectly; MariaDB is the modern recommendation for new WordPress installations.
Benefits of Running LEMP vs Managed Hosting for WordPress

- Full performance control: Tune PHP-FPM worker settings, Nginx worker processes, and MariaDB InnoDB buffer pool size specifically for your workload and VPS specs.
- Cost efficiency: A $10-20/month VPS with LEMP outperforms $50+/month managed WordPress hosting in raw throughput.
- Redis and OPcache integration: Easily add Redis object caching and PHP OPcache — critical performance layers that managed hosts often restrict or charge extra for.
- Multiple WordPress sites: Run dozens of WordPress sites on a single VPS using Nginx server blocks, each with isolated PHP-FPM pools.
- Security ownership: You control OS patching schedules, PHP version upgrades, and firewall rules without waiting for a hosting provider’s update cycles.
For related server setup guides, visit our How-To section.
Step-by-Step: How to Install Nginx, PHP, and MariaDB on Ubuntu for WordPress
Step 1 — Update the system
Start with a clean slate. This ensures all existing packages are current before adding new software.
sudo apt update && sudo apt upgrade -y
Step 2 — Install Nginx
Install Nginx, then verify it is running and enable it to start on boot.
sudo apt install nginx -y
sudo systemctl status nginx
sudo systemctl enable nginx
After installation, you should see “active (running)” in the status output.
Step 3 — Configure UFW to allow web traffic
Open ports 80 and 443 for web traffic, allow SSH to prevent locking yourself out, then enable the firewall.
sudo ufw allow 'Nginx Full'
sudo ufw allow OpenSSH
sudo ufw enable
Step 4 — Install MariaDB
Install MariaDB, enable it to start on boot, then run the security hardening wizard to set a root password, remove anonymous users, disallow remote root login, and remove the test database.
sudo apt install mariadb-server -y
sudo systemctl enable mariadb
sudo mysql_secure_installation
Step 5 — Create the WordPress database
Connect to MariaDB as root, then create a dedicated database and user for WordPress.
sudo mysql -u root -p
Once inside the MariaDB shell, run the following SQL statements:
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'YourStrongPassword!';
GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Step 6 — Add the PHP 8.3 repository and install PHP
Add Ondřej Surý’s PPA for the latest PHP, update apt, then install PHP 8.3 with all WordPress-required extensions.
sudo add-apt-repository ppa:ondrej/php -y && sudo apt update
sudo apt install php8.3-fpm php8.3-mysql php8.3-xml php8.3-mbstring php8.3-curl php8.3-zip php8.3-gd php8.3-imagick php8.3-intl php8.3-bcmath -y
Step 7 — Configure PHP-FPM for WordPress
Edit /etc/php/8.3/fpm/php.ini and apply the following recommended values for WordPress: set memory_limit = 256M, upload_max_filesize = 64M, post_max_size = 64M, and max_execution_time = 300. Enable OPcache with opcache.enable=1, opcache.memory_consumption=128, and opcache.max_accelerated_files=10000.
Step 8 — Create an Nginx server block for WordPress
Create the file /etc/nginx/sites-available/yourdomain.com with the standard WordPress Nginx configuration. Include the FastCGI pass to the PHP-FPM socket, the WordPress permalink rewrite rule (try_files $uri $uri/ /index.php$is_args$args;), and your root directory pointing to /var/www/yourdomain.com.
Step 9 — Enable the site and reload Nginx
Symlink the site configuration to enable it, test the Nginx config for syntax errors, then reload to apply changes.
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Step 10 — Install WordPress via WP-CLI
Download WordPress core, create the configuration file, then complete the setup via the browser wizard at your domain.
wp core download --path=/var/www/yourdomain.com
wp config create --dbname=wordpress --dbuser=wordpressuser --dbpass=YourStrongPassword! --path=/var/www/yourdomain.com
For official Nginx WordPress configuration recommendations, see Nginx’s FastCGI module documentation.
Common Questions — Installing Nginx, PHP, and MariaDB on Ubuntu for WordPress
Which PHP version should I use for WordPress in 2025?
PHP 8.2 and 8.3 are both fully supported by WordPress core and offer significant performance improvements over PHP 7.x. PHP 8.3 is the current stable release and the recommended choice for new installations. Avoid PHP 8.0 and 8.1 as they are approaching end-of-life. Always check your plugins’ PHP compatibility before upgrading an existing site.
How many PHP-FPM workers should I configure?
A practical formula for pm.max_children is: available RAM ÷ average PHP-FPM process size. On a 2GB VPS with 1.5GB available for PHP, and an average process size of 50MB, set pm.max_children = 30. Use pm = dynamic mode with pm.start_servers = 5, pm.min_spare_servers = 5, and pm.max_spare_servers = 10 for a balanced WordPress workload.
Can I run multiple WordPress sites on one LEMP server?
Yes. Create a separate Nginx server block, PHP-FPM pool, and MariaDB database for each site. Using separate PHP-FPM pools (one per site) provides process isolation — a PHP error on one site cannot affect another. Each pool runs under its own system user, improving security through file permission isolation.
Does MariaDB need tuning for WordPress?
The default MariaDB configuration is conservative. For a WordPress VPS, set innodb_buffer_pool_size to 50-70% of your available RAM, and enable query_cache_type = 0 (the query cache is deprecated and causes contention in MariaDB 10.5+). On SSDs, set innodb_flush_method = O_DIRECT to bypass the OS buffer cache for InnoDB data files.
Conclusion
Installing Nginx, PHP, and MariaDB on Ubuntu for WordPress is the foundation of every high-performance, self-managed WordPress deployment. Three key takeaways:
- Always use PHP-FPM with Nginx (never mod_php) and tune the worker pool size based on your VPS RAM to avoid memory exhaustion under load.
- Enable PHP OPcache immediately after installation — it alone can improve WordPress PHP execution speed by 3-5x with no application-level changes.
- Create isolated MariaDB users with only the permissions needed for each WordPress site; never use the root database account for application connections.
With your LEMP stack running, your next step is hardening the setup. Explore our WordPress Security guides to learn how to lock down Nginx and protect your fresh WordPress installation from automated attacks.
See also: DevOps and IT Operations: Complete Guide for Developers in 2026 — browse all Dev / IT Ops articles on Hubkub.
Related Articles
- How to Build a Home Server with Old Hardware
- Docker Basics: How to Get Started with Containerization
- Nginx Explained: How to Set Up a Web Server from Scratch
Last Updated: April 13, 2026








