Knowledge
How to increase the PHP memory limit
#Performance
The PHP memory limit caps how much memory a single script can use. Here is how to check it, find the right php.ini, and raise it safely for CLI, FPM, and per-app cases.
Published by Mark van Eijk on June 23, 2026 · 3 minute read
- Check the current limit
- Find the right php.ini
- Set memory_limit in php.ini
- Per PHP-FPM pool override
- Per-app and per-directory overrides
- Apply it only where needed
- Conclusion
memory_limit is the maximum amount of memory a single PHP script is allowed to allocate. When a script exceeds it, PHP kills the request with a fatal error rather than letting it eat the whole server. You usually meet the PHP memory limit the hard way, through the dreaded allowed memory size exhausted error:
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes)
That 134217728 is 128M, the common default. The fix is to raise the limit, but only in the right place. This guide shows where that place is.
Check the current limit
From the CLI, the quickest check:
php -i | grep memory_limit
Inside a request, drop a phpinfo() call or read the value directly:
echo ini_get('memory_limit'); // e.g. "128M"
Note that the CLI value and the web (FPM) value are often different, so check both, not just whichever is convenient.
Find the right php.ini
This is the step that trips people up. The PHP CLI and PHP-FPM load different configuration files. Editing the CLI's php.ini will do nothing for your web requests, and vice versa.
Ask PHP itself which file it uses:
php --ini
Configuration File (php.ini) Path: /etc/php/8.3/cli
Loaded Configuration File: /etc/php/8.3/cli/php.ini
That cli path is for command-line scripts. The web server uses the FPM file, typically /etc/php/8.3/fpm/php.ini. Confirm the FPM path from a phpinfo() page served through your web server.
Set memory_limit in php.ini
Open the correct file and set the value:
memory_limit = 256M
Use a plain integer plus a unit suffix: K, M, or G. A value of -1 means unlimited, which is discouraged on a web server, because a single runaway request can starve the whole machine. Pick a real ceiling instead.
Reload so the change takes effect. For the CLI, no reload is needed, the next php invocation picks it up. For FPM, restart the service:
sudo systemctl restart php8.3-fpm
Per PHP-FPM pool override
You often want a higher limit for one app, not every site on the box. PHP-FPM pools let you override memory_limit per pool, in the pool config (e.g. /etc/php/8.3/fpm/pool.d/www.conf):
php_admin_value[memory_limit] = 512M
php_admin_value sets the limit and prevents the application from lowering or overriding it at runtime. After editing the pool, restart FPM:
sudo systemctl restart php8.3-fpm
This is the cleanest way to give a memory-hungry app more headroom while keeping the global default conservative. While you are in the pool config, it is worth disabling unused PHP-FPM pools so they aren't holding resources.
Per-app and per-directory overrides
If you can't touch the global config, raise the PHP memory limit closer to the application.
.user.ini — drop a file in your app's web root (works with PHP-FPM/CGI):
memory_limit = 256M
Changes are cached and picked up after user_ini.cache_ttl (300 seconds by default), so they aren't instant.
ini_set() at runtime — raise it for a single script before the heavy work starts:
ini_set('memory_limit', '512M');
This fails if the limit was locked with php_admin_value, and it can't help a script that runs out of memory before this line executes.
.htaccess — on Apache with mod_php:
php_value memory_limit 256M
This does nothing under PHP-FPM, which ignores .htaccess PHP directives.
Apply it only where needed
Resist the urge to set a huge limit globally. A high web limit lets one bad request consume gigabytes; -1 everywhere removes the safety net entirely. Raise the limit for the specific pool, directory, or script that needs it, and leave the global default sane.
Remember the CLI/web split. Long-running command-line jobs legitimately need more memory than web requests, which is why Composer in particular hits the wall, see Composer out of memory: allowed memory size exhausted. Because the CLI uses its own php.ini, you can give it a generous limit (or -1) without loosening anything your web traffic touches.
Conclusion
Raising the PHP memory limit is straightforward once you know which php.ini is in play: confirm with php --ini, set memory_limit in the right file, and restart FPM for web changes. Override per pool, per directory, or per script when only one app needs more, and keep the global default modest so a single request can't take down the server. If you're tuning memory because pages are slow rather than crashing, that's a different problem, start with PHP performance.
Subscribe to our newsletter
Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!
Related articles
How to optimize server performance
The PHP memory limit caps how much memory a single script can use. Here is how to check it, find the right php.ini, and raise it safely for CLI, FPM, and per-app cases.
How to measure TTFB (Time To First Byte)
The PHP memory limit caps how much memory a single script can use. Here is how to check it, find the right php.ini, and raise it safely for CLI, FPM, and per-app cases.