How to change the PHP memory limit on Ubuntu
The PHP memory limit, memory_limit, caps how much memory one script may use, 128 MB by default. Next to it sit the limits people usually change together: the size of an upload, of a whole form and the time a script may run. On Ubuntu the hard part is finding the right file: the command line, PHP-FPM and Apache each read their own php.ini. The guide is for PHP 8.3 on Ubuntu 24.04. On 22.04 it is PHP 8.1, so write 8.1 in paths and service names. To see your version, run: ls /etc/php/
Which php.ini your PHP reads
Ubuntu keeps a separate configuration for each way PHP runs: /etc/php/8.3/cli/php.ini for the terminal, /etc/php/8.3/fpm/php.ini for PHP-FPM behind Nginx and /etc/php/8.3/apache2/php.ini for Apache’s mod_php. Each has a conf.d/ folder next to it whose files are read after php.ini. The first of these commands shows only the terminal’s files, so it says nothing about your site:
php --ini
php -r 'echo ini_get("memory_limit"), PHP_EOL;'
sudo php-fpm8.3 -i | grep -E '^(memory_limit|upload_max_filesize|post_max_size|max_execution_time)'On Ubuntu the terminal usually prints -1: its php.ini has no memory limit. The last command shows what PHP-FPM takes from its php.ini and conf.d. It doesn’t see values set in a pool or in .user.ini, so the final word is phpinfo() on the site itself: look at Loaded Configuration File, Additional .ini files parsed and the Local Value of memory_limit. Delete such a page right after use.
How to raise memory_limit, upload_max_filesize and post_max_size
Put your values in a file of their own in conf.d: it is read after php.ini and wins, and package updates don’t touch it. The rule from the PHP manual: post_max_size must be larger than upload_max_filesize, and memory_limit is generally larger than post_max_size. For PHP-FPM:
sudo nano /etc/php/8.3/fpm/conf.d/99-limits.inimemory_limit = 256M
upload_max_filesize = 50M
post_max_size = 60M
max_execution_time = 60sudo php-fpm8.3 -t
sudo systemctl restart php8.3-fpmWith Apache and mod_php, put the same file in /etc/php/8.3/apache2/conf.d/ and run:
sudo systemctl restart apache2For scripts in the terminal and cron, it goes to /etc/php/8.3/cli/conf.d/, and no restart is needed. Raise memory_limit step by step, 256M or 512M, not to -1: a limit stops one broken request before it eats the memory of the whole server.
Uploads: three limits, not two
A file upload passes three checks, and the smallest one wins:
upload_max_filesize(2 MB by default): the size of one file. A bigger file arrives with the error codeUPLOAD_ERR_INI_SIZEin$_FILES, and the rest of the form is still there.post_max_size(8 MB by default): the whole request with all files and fields. If the request is bigger,$_POSTand$_FILESare empty, and the log saysPHP Warning: POST Content-Length of … bytes exceeds the limit of 8388608 bytes.client_max_body_sizein Nginx (1 MB by default): Nginx answers413 Request Entity Too Largebefore PHP sees anything. Raise it in the site’sserverblock, as shown in how to set up an Nginx reverse proxy.
Limits for one site: FPM pool, .user.ini and ini_set
In a PHP-FPM pool you can set values for one site only. php_admin_value can’t be changed by the site’s code, php_value can. Add lines to the pool file of that site, for example www.conf, and restart PHP-FPM:
php_admin_value[memory_limit] = 512M
php_admin_value[upload_max_filesize] = 100M
php_admin_value[post_max_size] = 110MWithout access to the server settings, put a .user.ini file in the site’s root folder. PHP-FPM reads it for scripts in that folder and below, and only settings that may be set per directory work there: memory_limit, upload_max_filesize, post_max_size and max_execution_time all may. PHP rereads it every 5 minutes (user_ini.cache_ttl), so wait or restart PHP-FPM. With Apache’s mod_php, .user.ini isn’t read: use php_value memory_limit 256M in .htaccess instead.
memory_limit = 256M
upload_max_filesize = 50M
post_max_size = 60MIn code, ini_set('memory_limit', '512M') raises the limit for one script, for example an export, and set_time_limit(120) does the same for time. They don’t work for upload_max_filesize and post_max_size, because the request is already received when the code runs. They also don’t work against a php_admin_value.
max_execution_time and timeouts
max_execution_time is 30 seconds by default for the site and unlimited in the terminal. On Linux it counts the script’s own work, not the time spent waiting for the database, files or another server, so a script that waits on a slow API can run much longer. The script then stops with Fatal error: Maximum execution time of 30 seconds exceeded. Two more limits sit around it:
fastcgi_read_timeoutin Nginx, 60 seconds by default: after it Nginx gives the visitor504 Gateway Time-out, even if PHP is still working.request_terminate_timeoutin the FPM pool, off by default: kills the PHP process after that time, whatever it is waiting for.
Keep web requests short. Run long jobs such as imports and reports from the terminal, cron or a queue, where max_execution_time doesn’t apply.
How to fix “Allowed memory size of 134217728 bytes exhausted”
The full message is PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes). The first number is the current limit in bytes: 134217728 is 128 MB, 268435456 is 256 MB. If the number doesn’t change after you raise the limit, the script runs under another SAPI or pool than the file you edited: check with phpinfo() on the site, or, for the terminal and cron, with the commands from the first section.
- The error happens on a heavy page, an import or an image resize: raise
memory_limitfor that site or that script withini_set. - The error stays at any limit: the code loads too much at once, for example a whole table into an array, or loops forever. Read the data in parts. The file and line in the message show where memory ran out.
- The error comes from Composer. This lifts the limit for one run:
COMPOSER_MEMORY_LIMIT=-1 composer update
If you also work on a Mac

System Junk in MacWashing finds the caches of every app it knows, including those hidden inside app data, plus logs, Xcode junk and leftovers of deleted apps. It shows how much each takes and clears them in one go. Anything debatable stays unchecked until you decide.
Free trial · macOS 14 or later
Questions and answers
What is the default PHP memory limit?
128 MB for the site in PHP-FPM and Apache. In the terminal on Ubuntu there is no limit: memory_limit = -1 in /etc/php/8.3/cli/php.ini.
Is memory_limit = -1 safe on a web server?
No. One request with a bug can then take all the memory and slow down or stop the server. Give the site a real limit and lift it only for terminal scripts you trust.
I changed php.ini, but nothing changed. Why?
Usually it is the wrong file (the terminal’s cli/php.ini instead of fpm/php.ini), PHP-FPM wasn’t restarted, or a pool or .user.ini sets its own value. phpinfo() on the site shows which file and which value apply.