Tutorials

PHP Performance Tuning: Speed Up Your Web Application on a VPS

Muhammad SaadMay 10, 20266 min read
PHP Performance Tuning: Speed Up Your Web Application on a VPS

Most PHP applications run far slower than they need to. Default PHP configurations are designed for compatibility, not performance. With a few targeted changes to your php performance tuning setup, you can cut response times in half and handle significantly more traffic on the same hardware.

This guide covers the highest-impact optimizations — the ones that make a real, measurable difference on your VPS.

Why PHP Performance Tuning Matters

PHP powers 77% of websites with a known server-side language, including WordPress, Laravel, and most custom web applications. Yet most developers deploy with default settings and never touch the configuration.

The result? Slow page loads, wasted server resources, and premature scaling costs. A properly tuned PHP installation on a $20/month VPS can outperform a default setup on a $100/month server.

1. Use the Latest PHP Version

This is the single biggest performance gain with zero code changes:

PHP 7.4 → PHP 8.0: ~20% faster
PHP 8.0 → PHP 8.1: ~5% faster
PHP 8.1 → PHP 8.2: ~5% faster
PHP 8.2 → PHP 8.3: ~5% faster

Check your current version:

php -v

Upgrade on Ubuntu:

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-redis -y

Update your Nginx configuration to use the new PHP-FPM socket:

fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;

Each major PHP version brings JIT improvements, internal optimizations, and reduced memory usage. Upgrading is the closest thing to free performance.

2. Enable OPcache

OPcache stores precompiled PHP bytecode in shared memory, eliminating the need to parse and compile scripts on every request. This typically reduces response times by 30-50%.

Edit /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=10000
opcache.revalidate_freq=60
opcache.validate_timestamps=1
opcache.save_comments=1
opcache.fast_shutdown=1

Key settings explained:

  • memory_consumption=256 — Allocates 256MB for cached scripts (increase for large codebases)
  • max_accelerated_files=10000 — Maximum number of files to cache (WordPress + plugins easily needs 5,000+)
  • revalidate_freq=60 — Check for file changes every 60 seconds (set to 0 in development)

Verify OPcache is active:

php -i | grep opcache.enable

3. Tune PHP-FPM Process Manager

PHP-FPM handles concurrent requests through worker processes. Default settings are conservative — too few workers and requests queue up; too many and you exhaust memory.

Edit /etc/php/8.3/fpm/pool.d/www.conf:

For a 2GB VPS:

pm = dynamic
pm.max_children = 15
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pm.max_requests = 500

For a 4GB VPS:

pm = dynamic
pm.max_children = 30
pm.start_servers = 8
pm.min_spare_servers = 4
pm.max_spare_servers = 16
pm.max_requests = 500

How to calculate max_children:

# Check average memory per PHP-FPM process
ps aux | grep php-fpm | awk '{sum+=$6; count++} END {print sum/count/1024 " MB average"}'

Formula: max_children = (Total RAM - OS/DB overhead) / Average PHP process size

For a 2GB VPS with ~512MB for OS/MySQL: (2048 - 512) / 100MB ≈ 15

The pm.max_requests = 500 setting restarts workers after 500 requests, preventing memory leaks from accumulating.

4. Optimize php.ini Settings

Edit /etc/php/8.3/fpm/php.ini:

; Memory and execution limits
memory_limit = 256M
max_execution_time = 30
max_input_time = 60

; File uploads
upload_max_filesize = 64M
post_max_size = 64M

; Error handling (production)
display_errors = Off
log_errors = On
error_log = /var/log/php/error.log

; Session handling
session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"

; Realpath cache (reduces filesystem stat calls)
realpath_cache_size = 4096K
realpath_cache_ttl = 600

; Output buffering
output_buffering = 4096

Important settings for PHP performance tuning:

  • realpath_cache_size = 4096K — Caches filesystem lookups. Default is 16K, which is far too small for modern frameworks
  • session.save_handler = redis — Redis sessions are faster than file-based sessions and work across multiple servers

5. Add Redis for Object Caching

Redis stores frequently-accessed data in memory, reducing database queries by 50-90%:

sudo apt install redis-server php8.3-redis -y
sudo systemctl enable redis-server

For WordPress, install the Redis Object Cache plugin and add to wp-config.php:

define('WP_REDIS_HOST', '127.0.0.1');
define('WP_REDIS_PORT', 6379);
define('WP_REDIS_DATABASE', 0);

For Laravel, update .env:

CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis

6. Enable Nginx FastCGI Caching

For content that doesn't change every request (most pages), Nginx can serve cached responses without even hitting PHP:

# Add to http block in nginx.conf
fastcgi_cache_path /tmp/nginx-cache levels=1:2 keys_zone=PHPCACHE:100m max_size=1g inactive=60m;

# In your server block
set $skip_cache 0;

# Don't cache POST requests
if ($request_method = POST) { set $skip_cache 1; }

# Don't cache URLs with query strings
if ($query_string != "") { set $skip_cache 1; }

# Don't cache admin areas
if ($request_uri ~* "/wp-admin/|/wp-login.php|/admin") { set $skip_cache 1; }

location ~ \.php$ {
    fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
    fastcgi_cache PHPCACHE;
    fastcgi_cache_valid 200 60m;
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;
    add_header X-Cache-Status $upstream_cache_status;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

With FastCGI caching, cached pages serve in under 5ms — essentially static file performance from a dynamic PHP application.

7. Monitor and Measure

Never tune blindly. Measure before and after each change:

# Quick response time test
curl -o /dev/null -s -w "TTFB: %{time_starttransfer}s\nTotal: %{time_total}s\n" https://yoursite.com

# Load test with Apache Bench
ab -n 1000 -c 50 https://yoursite.com/

# Monitor PHP-FPM status
# Enable status page in pool config: pm.status_path = /fpm-status
curl http://localhost/fpm-status

Target benchmarks:

Metric Before Tuning After Tuning
TTFB 400-800ms 50-200ms
Requests/sec 50-100 200-500+
Memory per worker 80-150MB 40-80MB

FAQ

How much RAM does PHP-FPM need?

Each PHP-FPM worker typically uses 40-100MB of RAM depending on your application. A WordPress site with several plugins averages around 60-80MB per worker. Calculate your max_children based on available RAM minus OS and database overhead.

Should I use PHP-FPM static or dynamic mode?

Use pm = dynamic for most sites — it adjusts worker count based on demand. Use pm = static only for high-traffic sites where you want a fixed number of workers always ready. Static mode wastes memory during low-traffic periods.

Does OPcache work with file uploads and changes?

Yes. With validate_timestamps=1 and revalidate_freq=60, OPcache checks for file changes every 60 seconds. After deploying new code, either wait for revalidation or restart PHP-FPM to clear the cache immediately.

Can I use these optimizations with shared hosting?

Most of these settings require PHP-FPM configuration access, which shared hosting doesn't provide. A VPS gives you full control over PHP configuration — that's the main advantage for performance tuning.

Optimize PHP on DeployBase

Proper php performance tuning can transform a sluggish application into a fast, responsive experience. The optimizations above — upgrading PHP, enabling OPcache, tuning FPM workers, and adding caching — typically deliver 3-5x performance improvements.

At DeployBase, our VPS plans give you full root access to configure PHP exactly as described in this guide. Starting at $5/month with NVMe SSD storage, pre-installed PHP 8.3, and 24/7 support, you get the foundation for a properly tuned PHP application.

Get your VPS at DeployBase → — fast hosting starts with proper configuration.

Share this article

Muhammad Saad

Muhammad Saad

DeployBase Team

Ready to Get Started?

Join thousands of developers who trust DeployBase for their hosting needs.