Website Performance
WordPress on a VPS: Nginx, PHP-FPM, OPcache and Redis for Speed
By NileVPS Team · Published · 4 min read
اقرأ المقال بالعربيWordPress can be very fast or painfully slow, and the difference is rarely WordPress itself. It is usually the stack underneath: how PHP is executed, whether compiled code is cached, how often the database is queried for the same data, and whether finished pages are served without running PHP at all. A VPS lets you control every one of those layers. This guide walks through a proven stack, Nginx with PHP-FPM, OPcache, Redis and page caching, with settings you can adapt to your own server.
The stack at a glance
| Layer | Tool | What it saves |
|---|---|---|
| Web server | Nginx | Serves static files efficiently and passes PHP to FPM |
| PHP runtime | PHP-FPM | Keeps a pool of PHP workers ready instead of starting one per request |
| Opcode cache | OPcache | Skips recompiling PHP files on every request |
| Object cache | Redis | Stores repeated database query results in memory |
| Page cache | Nginx fastcgi_cache or a plugin | Serves complete HTML without running PHP |
| Database | MariaDB or MySQL | Tuned buffer pool keeps hot data in RAM |
A server with 2 vCPU and 4 GB RAM comfortably runs this stack for a typical business site or blog. Busy sites, membership sites and stores benefit from 4 vCPU and 8 GB.
Install the base packages
On Ubuntu 24.04 the distribution packages are a good starting point:
sudo apt update
sudo apt install nginx mariadb-server redis-server \
php-fpm php-mysql php-curl php-gd php-intl php-mbstring \
php-xml php-zip php-imagick php-redisCheck which PHP version you got with php -v; Ubuntu 24.04 ships PHP 8.3, so the FPM service is php8.3-fpm and the configuration lives under /etc/php/8.3/.
Nginx server block
A minimal, correct block for WordPress:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
location ~* \.(css|js|jpg|jpeg|png|gif|webp|svg|woff2)$ {
expires 30d;
access_log off;
}
}Test and reload with sudo nginx -t && sudo systemctl reload nginx, then add HTTPS with sudo certbot --nginx.
Tune PHP-FPM workers
The most common WordPress bottleneck on a VPS is the wrong number of PHP workers. Too few and requests queue; too many and the server runs out of RAM and starts swapping.
- Measure the average memory per PHP-FPM process under real traffic:
``bash ps -C php-fpm8.3 -o rss= | awk '{s+=$1; n++} END {print s/n/1024 " MB"}' ``
- Decide how much RAM PHP may use after the database, Redis and the OS take their share.
- Divide: available RAM for PHP divided by memory per process gives
pm.max_children.
For example, with about 2 GB for PHP and 60-80 MB per worker, roughly 25 workers is a sensible ceiling. In /etc/php/8.3/fpm/pool.d/www.conf:
pm = dynamic
pm.max_children = 25
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pm.max_requests = 500pm.max_requests recycles workers periodically, which contains slow memory leaks in plugins.
Enable and size OPcache
OPcache is usually enabled by default, but the defaults are small for WordPress with many plugins. In /etc/php/8.3/fpm/conf.d/10-opcache.ini:
opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=20000
opcache.validate_timestamps=1
opcache.revalidate_freq=60Restart with sudo systemctl restart php8.3-fpm. If you deploy by replacing files and want instant changes, keep validate_timestamps=1; setting it to 0 is faster but then you must reload PHP-FPM after every update.
Add a Redis object cache
WordPress runs many identical queries for options, menus and metadata. A persistent object cache keeps their results in memory between requests.
- Confirm Redis is running:
redis-cli pingshould answerPONG. - Install a Redis object cache plugin, such as Redis Object Cache, and enable it. It installs an
object-cache.phpdrop-in. - If several sites share one Redis, give each a unique prefix in
wp-config.php:
define('WP_REDIS_PREFIX', 'example_');Set a memory limit and eviction policy in /etc/redis/redis.conf so the cache cannot consume the whole server:
maxmemory 256mb
maxmemory-policy allkeys-lruPage caching
Page caching gives the biggest single gain for anonymous visitors because PHP and MySQL are skipped completely. You can use Nginx fastcgi_cache or a caching plugin that writes static HTML. Whatever you choose, bypass the cache for logged-in users, comment authors, POST requests and, on stores, cart and checkout pages. Our guide on VPS hosting for online stores lists the cookies to exclude for WooCommerce.
Database and housekeeping
- Set
innodb_buffer_pool_sizeto a size that fits your database in memory, without starving PHP. - Limit post revisions:
define('WP_POST_REVISIONS', 10); - Replace WordPress pseudo-cron with a real system cron job.
- Remove plugins you do not use; every active plugin adds code to each request.
- Measure with your browser's developer tools and server logs before and after each change, so you know what actually helped.
Ready to build it? Create a Linux server from /deploy and follow the steps above.
FAQ
Is Nginx faster than Apache for WordPress?
Nginx handles static files and many concurrent connections with less memory, which is why it is popular for WordPress. A well-tuned Apache with PHP-FPM also performs well. The bigger gains come from caching and correct PHP worker settings rather than the web server choice.
Do I need both Redis and a page cache?
They solve different problems. The page cache serves anonymous visitors without running PHP. Redis speeds up the requests that cannot be cached, such as logged-in users, admin pages, carts and searches. Most busy sites benefit from both.
How much RAM does a WordPress VPS need?
A single typical site runs well on 2-4 GB of RAM with the stack above. Plan more for WooCommerce, membership sites, many sites on one server, or heavy page builders, and base the final decision on measured memory use.