How to Install and Tune PHP-FPM with Nginx for WordPress

WordPress on Nginx can feel fast or slow for the same traffic, and the difference often sits in the PHP layer rather than the theme. When you tune php-fpm with nginx the right way, requests stop queuing behind a pool that is too small, memory stops getting eaten by idle workers, and slow pages start coming back much faster. This guide walks through a clean install on Ubuntu, sensible pool settings for a small VPS, the Nginx side of the connection, and OPcache, so you can follow each step on a live server and check the result as you go.

How to tune php-fpm with nginx for WordPress sites

PHP-FPM runs PHP as a separate pool of workers, and Nginx hands each PHP request to that pool through a socket. If the pool is too small, visitors wait in a queue while workers finish earlier requests. If it is too large, idle workers eat RAM that MySQL and the system also need. Good tuning is a balance: enough workers for normal peaks, a process manager mode that matches your traffic, and a socket setup that keeps the link between Nginx and PHP short and local.

Install PHP-FPM and Nginx on Ubuntu

On Ubuntu 24.04, install Nginx and the PHP version your WordPress install needs. WordPress runs well on PHP 8.2 or 8.3, so pick one version and keep every extension on that same version. The command below installs Nginx, PHP-FPM, and the extensions WordPress normally needs, including MySQL support, curl, XML handling, and image work.

sudo apt update
sudo apt install nginx php8.3-fpm php8.3-mysql php8.3-curl php8.3-xml php8.3-mbstring php8.3-zip php8.3-gd -y
sudo systemctl enable --now nginx php8.3-fpm
sudo systemctl status php8.3-fpm --no-pager

Check that both services are active before you touch any config file. If PHP-FPM fails to start, run php-fpm8.3 -t to test the syntax, and read the error log at /var/log/php8.3-fpm.log. Confirm the running version with php -v so you edit the pool file for the right one. For a wider look at caching in front of WordPress, read about Nginx caching for WordPress at scale before you add more layers.

Pick the right process manager mode

PHP-FPM offers three process manager modes: static, dynamic, and ondemand. Static keeps a fixed number of workers running at all times, which gives steady response times and fits a site with steady traffic. Dynamic starts with a small number of workers and grows toward a maximum when traffic rises, which suits a blog with quiet hours and busy spikes. Ondemand starts no workers at all and spawns them per request, which saves RAM on a very small VPS but adds a start-up delay to the first request after idle time. Most WordPress sites on a 2 GB or 4 GB VPS do best with dynamic, because it keeps a warm set of workers ready without holding the maximum all day.

Set pool size from RAM and traffic

Pool size is a maths problem, not a guess. A typical WordPress worker uses around 30 to 50 MB of RAM, depending on the theme and active plugins, so a 2 GB server that also runs MySQL and Nginx can safely hold about 10 to 15 workers, while a 4 GB server can hold about 25 to 35. Measure your own number instead of trusting a round figure: check a worker with ps, take the average size, subtract the RAM MySQL and the system need, and divide what is left by that worker size. When you research php-fpm pool settings wordpress guides, compare the RAM behind each recommendation with your own server, because a pool sized for 16 GB will crash a 2 GB VPS.

; /etc/php/8.3/fpm/pool.d/www.conf
pm = dynamic
pm.max_children = 12
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6
pm.max_requests = 500

The pm.max_requests value recycles each worker after 500 requests, which clears slow memory growth from plugins without hurting speed. After editing the pool file, test and reload with sudo php-fpm8.3 -t && sudo systemctl reload php8.3-fpm. If the log often warns that the pool reached its maximum, raise pm.max_children by two or three. A deeper walkthrough lives in this guide to PHP-FPM tuning for high-traffic sites, which explains each process manager mode in more detail.

Connect Nginx to the PHP-FPM socket

Nginx talks to PHP-FPM through a Unix socket by default on Ubuntu, which is faster than TCP for traffic on the same machine because it skips the network stack. The socket path depends on your PHP version, for example /run/php/php8.3-fpm.sock, and your Nginx server block must point at exactly that path. A mismatch here is the usual cause of a 502 Bad Gateway error after an upgrade, so check the listen line in the pool file and the fastcgi_pass line in Nginx first. A working nginx php-fpm configuration for WordPress keeps the PHP location block short and sets a timeout that matches how long your slowest admin page really takes.

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

Keep a separate block for static files so Nginx serves images, CSS, and JavaScript without waking PHP at all. After editing, test with sudo nginx -t and reload with sudo systemctl reload nginx. If you see 502 errors, check that the socket file exists. Once the connection is stable, finish the HTTPS side with this guide to free HTTPS with Let’s Encrypt on Nginx.

Turn on OPcache and check the result

OPcache stores compiled PHP code in shared memory, so WordPress does not recompile the same files on every request. It ships with PHP and is usually already installed, but it needs a few settings to work well. Enable it in the OPcache config file, give it enough memory for your theme and plugins, and set a revalidation interval that fits how often you deploy changes. On a small VPS, 128 MB of OPcache memory is a sensible start. A useful wordpress php-fpm performance tuning pass always includes OPcache, because it cuts CPU use on every page load, including cached pages that still run some PHP.

; /etc/php/8.3/fpm/conf.d/10-opcache.ini
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60

After a reload, confirm OPcache is active with a small PHP info page on a staging copy, then delete the info page so it is never left on a live site. Measure before and after with the same tool and the same page: load the homepage five times, note the slowest and fastest times, change one setting, reload PHP-FPM, and measure again. If you want to compare web servers first, this comparison of Nginx versus Apache for high-traffic sites sets out where each one fits.

Common mistakes that slow WordPress down

The most common mistake is copying pool settings from a server with far more RAM and then wondering why the site runs out of memory under load. The second is leaving pm.max_children at its default of five on a busy site, so visitors queue while workers are missing in practice. A third is editing the wrong pool file: Ubuntu keeps a separate file per PHP version, and a change to the 8.2 file does nothing when 8.3 is the version running. A fourth is forgetting to reload PHP-FPM after an edit, so the old settings keep running while you test the new ones. Keep a dated backup copy of each config file before you edit it.

Your next step is a short test cycle: record homepage load time and RAM use today, apply the pool size from the maths above, turn on OPcache, reload both services, and measure the same page again tomorrow at the same hour. If load time drops and RAM stays steady, keep the settings and note them down. If the pool log still shows the maximum being hit, raise the worker count by a small step and test again.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.