When traffic climbs, a PHP site rarely slows down for one reason. Pages queue for a free PHP worker, the database takes longer to answer, sessions lock, and slow jobs run inside the request. Fixing all of them at once wastes time. The quicker path is to measure first, then fix the biggest bottleneck, then measure again.
This guide covers that order of work: how to find where the time goes, then the ten changes that make the most difference on busy PHP applications, from PHP version and OPcache to PHP-FPM, the database, caching and scaling out. It applies to custom PHP, Laravel, Symfony and WordPress alike.
Start by measuring, not guessing
Before you change a setting, find out which part of a slow request is slow. Four sources usually answer that:
- A profiler. Blackfire, Tideways or New Relic in production, or Xdebug’s profiler on a staging copy, shows which functions and queries take the time.
- The PHP-FPM status page. Set
pm.status_pathand it reports active, idle and queued processes. A growing listen queue means requests are waiting for a free worker. - The PHP-FPM slow log. Set
request_slowlog_timeout(for example5s) and PHP writes a backtrace for every request that runs longer, so you can see exactly where it was stuck. - The database slow query log. MySQL, MariaDB and PostgreSQL can all log queries above a time threshold.
Record a baseline (response time at the 95th percentile, requests per second, error rate, CPU and memory) so you can prove each change helped.
Where the time usually goes
| Symptom | Likely cause | First fix |
|---|---|---|
| High CPU on every request | OPcache off, too small or revalidating constantly | Tune OPcache (step 2) |
| Requests queue while CPU is low | Too few PHP-FPM workers, or workers waiting on slow I/O | Size PHP-FPM (step 3), then find the slow I/O |
| Slow pages that hit the database | Missing indexes, N+1 queries, large result sets | Fix the database (step 4) |
| Same data built again on every request | No object or page cache | Add caching (step 5) |
| Slow form submits and checkouts | Email, PDFs or API calls done inside the request | Use a queue (step 6) |
| One user’s parallel requests run one at a time | Session file locking | Fix sessions (step 7) |
| Memory climbs until servers swap | Too many workers for the RAM, or a memory leak | Lower pm.max_children, set pm.max_requests |
1. Run a supported PHP version
Each PHP release brings engine improvements, and only supported versions get security fixes. According to php.net, PHP 8.1 and older no longer receive security fixes, and PHP 8.2’s security support ends on 31 December 2026. PHP 8.3, 8.4 and 8.5 are supported into 2027, 2028 and 2029.
Upgrade on a staging copy, run your tests and watch the error log for deprecations. Our guides to PHP version compatibility issues and migrating from PHP 7.4 to PHP 8 cover what usually breaks. On WordPress, see how outdated PHP affects a WordPress site.
2. Tune OPcache for production
OPcache stores compiled PHP bytecode in shared memory, so PHP doesn’t parse and compile every file on every request. It is on by default, but the defaults suit a small site. The php.net OPcache reference lists every setting. The ones that matter most on a busy server:
opcache.memory_consumption(default 128 MB). If the cache fills, scripts stop being cached. Check usage withopcache_get_status()and raise it if it runs near full.opcache.max_accelerated_files(default 10,000). Large frameworks with many Composer packages can exceed this. Set it above your real file count.opcache.interned_strings_buffer(default 8 MB). Busy framework apps often need more.opcache.validate_timestamps(default on). In production you can turn it off so PHP stops checking files for changes, but then every deploy must reset OPcache, by reloading PHP-FPM or callingopcache_reset(). Forget that and old code keeps running.opcache.preload(PHP 7.4 and later). Loads chosen classes into memory once at server start. It helps some framework apps; measure before and after.
What about the JIT? The JIT compiler arrived in PHP 8.0, and since PHP 8.4 it is disabled by default (opcache.jit=disable). It mainly speeds up CPU-heavy code such as calculations and image work. Typical web pages spend most of their time waiting on the database and network, so the gain there is usually small. Turn it on only if profiling shows CPU-bound PHP code, and test it under load.
3. Size PHP-FPM to your memory
PHP-FPM runs a pool of worker processes, and each worker handles one request at a time. pm.max_children caps how many requests run at once. Too few and requests queue; too many and the server runs out of memory and swaps, which is worse.
- Work out
pm.max_childrenfrom RAM. Take the memory you can give PHP (after the database, cache and operating system), divide by the average memory of one worker under real traffic, and leave some headroom. - Pick a process manager mode.
statickeeps a fixed number of workers and suits dedicated servers with steady traffic.dynamicscales between spare-server limits.ondemandstarts workers only when needed and saves memory on quiet pools. - Set
pm.max_requests. The default of 0 means workers never restart. A value such as 500 recycles each worker after that many requests, which contains slow memory leaks in third-party code.
The PHP-FPM configuration reference documents each setting.
Free resource
Upgrade and patching checklist
Want this applied to your project? Our engineers will send it with notes for your stack.
4. Fix the database first
On most busy PHP sites the database, not PHP, is the real limit. Start with the slow query log and work down the list:
- Add the right indexes. Run
EXPLAINon slow queries and index the columns used inWHERE,JOINandORDER BY. - Remove N+1 queries. A loop that runs one query per row turns 1 query into 100. Load related data in one go, for example with eager loading in Laravel.
- Fetch only what you need. Select the columns you use instead of
SELECT *, and paginate large lists. - Move reads to a replica once one database server is busy all day. Reports and search can read from a replica while writes go to the primary.
5. Cache at every layer
The fastest request is one PHP never has to build. Work from the outside in:
- CDN and HTTP caching. Serve images, CSS and JavaScript from a CDN with long cache headers. Pages that are the same for every visitor can be cached at the edge too.
- Full-page cache. For logged-out visitors, a page cache (Varnish, NGINX FastCGI cache or a WordPress page cache plugin) returns stored HTML without running PHP.
- Object cache. Redis or Memcached keep the results of expensive queries and calculations in memory, shared across servers.
Every cache needs a clear rule for when it is cleared. Stale prices or stock levels cost more than a slow page.
6. Move slow work out of the request
Sending email, generating PDFs, resizing images and calling third-party APIs don’t need to finish before the visitor sees a response. Put them on a queue and let background workers process them: Laravel Queues, Symfony Messenger, or a broker such as RabbitMQ or Redis. The request finishes faster, and a slow outside service no longer holds a PHP-FPM worker.
7. Fix session locking
PHP’s default file session handler locks the session for the whole request. If a page fires several AJAX calls at once, they run one after another. Call session_write_close() as soon as a request has finished writing to the session, and on more than one server, store sessions in Redis or the database so any server can handle any user.
8. Optimise the Composer autoloader
In production, install without development packages and build a class map so PHP doesn’t search the file system for classes:
composer install --no-dev --optimize-autoloader
# or, if every class is in the class map:
composer dump-autoload --classmap-authoritative
Frameworks add their own caches on top. In Laravel, for example, php artisan optimize caches configuration, routes and views.
9. Let the web server and CDN do their job
- Serve static files directly from NGINX, Apache or the CDN, never through PHP.
- Turn on HTTP/2 or HTTP/3 and compress text responses with Brotli or gzip.
- Set sensible timeouts so a stuck request can’t hold a worker for minutes.
10. Scale out when one server isn’t enough
Once a single server is tuned and still busy, add servers behind a load balancer. That only works if the application is stateless: sessions in Redis or the database, uploads in shared or object storage, and scheduled jobs running on one server only. Make those changes before the traffic peak, not during it.
Load test before the traffic arrives
A sale, a launch or a news mention can multiply traffic in minutes. Test your busiest journeys (home page, search, cart, checkout, login) with a load testing tool such as k6 or Apache JMeter on a production-like copy. Raise the load until response times or errors climb, note where it breaks, fix that, and run it again.
When to get help
Tuning is usually quick once you know the bottleneck. Get help when the site slows down under load and nobody can say why, when the app is stuck on an old PHP or framework version, or when the code itself needs restructuring: queries inside templates, no tests, and one server that can’t be cloned. Those problems need a structured plan, not another setting change.
Frequently asked questions
What is the fastest way to speed up a slow PHP site?
Measure first. Profile a slow request and check the database slow query log. On most sites the quick wins are a supported PHP version, a properly sized OPcache, missing database indexes and a page or object cache.
Should I enable the PHP JIT?
Only if profiling shows CPU-heavy PHP code. The JIT has been off by default since PHP 8.4, and most web pages spend their time waiting on the database and network, where it helps little. Test it under load before you keep it.
How many PHP-FPM workers do I need?
Divide the memory available to PHP by the average memory one worker uses under real traffic, then leave headroom. More workers than your RAM can hold makes the server swap and slows everything down.
Is Redis or Memcached better for PHP caching?
Both are fast in-memory caches. Redis also offers persistence and richer data types, and can hold sessions and queues, so many teams use it for all three. Memcached is a simple, proven choice for a pure cache.
Does upgrading PHP improve performance?
Newer PHP versions include engine improvements, and supported versions also get security fixes. The size of the gain depends on your code, so measure before and after on a staging copy.
Need a faster PHP application?
Vibidsoft tunes, upgrades and maintains PHP and Laravel applications at a fixed price, never by the hour. Start with a fixed-price assessment from $1,500, credited to the work: we profile your application, review the server, database and code, and give you a written plan ranked by impact. For ageing or stalled applications, Modernize & Rescue projects start from $5,000. To keep PHP, the framework and the server current after that, care plans start from $1,500 a month, with response times in writing.
