How to increase the PHP memory limit - Rocketeers app

  [ Rocketeers ](/)   

[Login](https://rocketeersapp.com/login) 

 On this page

 Knowledge
---------

How to increase the PHP memory limit
====================================

### [\#Performance](https://rocketeersapp.com/knowledge/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](https://rocketeersapp.com/author/mark-van-eijk) on June 23, 2026 · 3 minute read

1. [Check the current limit](#content-check-the-current-limit)
2. [Find the right php.ini](#content-find-the-right-phpini)
3. [Set memory\_limit in php.ini](#content-set-memorylimit-in-phpini)
4. [Per PHP-FPM pool override](#content-per-php-fpm-pool-override)
5. [Per-app and per-directory overrides](#content-per-app-and-per-directory-overrides)
6. [Apply it only where needed](#content-apply-it-only-where-needed)
7. [Conclusion](#content-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](/php-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.

[\#](#content-check-the-current-limit "Permalink")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.

[\#](#content-find-the-right-phpini "Permalink")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.

[\#](#content-set-memorylimit-in-phpini "Permalink")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

```

[\#](#content-per-php-fpm-pool-override "Permalink")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](/disable-unused-php-fpm-pools) so they aren't holding resources.

[\#](#content-per-app-and-per-directory-overrides "Permalink")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.

[\#](#content-apply-it-only-where-needed "Permalink")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](/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.

[\#](#content-conclusion "Permalink")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](/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!

  Fill in your email address to receive updates  Subscribe 

#### More in [\#Performance](https://rocketeersapp.com/knowledge/performance)

- [How to optimize server performance](https://rocketeersapp.com/knowledge/optimize-server-performance)
- [How to optimize website performance](https://rocketeersapp.com/knowledge/optimize-website-performance)
- [How to measure TTFB (Time To First Byte)](https://rocketeersapp.com/knowledge/ttfb)
- [How to enable and configure OPcache for faster PHP](https://rocketeersapp.com/knowledge/enable-opcache-php)
- [A complete guide to caching in Laravel](https://rocketeersapp.com/knowledge/laravel-cache)
- [How database indexing works (with MySQL examples)](https://rocketeersapp.com/knowledge/database-indexing)

 [View all 13 articles →](https://rocketeersapp.com/knowledge/performance)
