php.ini settings: upload size, memory limit and error display
Set the PHP values most sites need: upload size, memory, run time, errors, time zone and OPcache.
Most PHP problems with large uploads, slow scripts or hidden errors come down to a few php.ini values. Set them here, save the result as a small override file, and restart PHP.
Create your config file
Type your own values. The file below updates as you type, and nothing you type leaves your browser.
; Values for uploads, memory and run time
upload_max_filesize = 64M
post_max_size = 64M
memory_limit = 256M
max_execution_time = 60
date.timezone = UTC
; Production: hide errors from visitors, keep them in the log
display_errors = Off
log_errors = On
opcache.enable = 1
opcache.memory_consumption = 128
opcache.max_accelerated_files = 10000How this setup works
Step-by-step setup
- Find the folder PHP reads extra files from: run php --ini and look at "Scan for additional .ini files". On Ubuntu with PHP-FPM it is /etc/php/8.3/fpm/conf.d/.
- Save the settings above as 99-custom.ini in that folder. The 99 makes it load last, so it wins.
- Restart PHP: sudo systemctl restart php8.3-fpm (use your version), or restart Apache for mod_php.
- Check the new values: create a file with <?php phpinfo();, open it, search for upload_max_filesize, then delete the file.
- For large uploads also raise client_max_body_size in nginx to the same size.
What the important lines mean
- upload_max_filesize is the largest single file. post_max_size must be at least as large, so both use 64M here.
- memory_limit caps the memory one script can use.
- max_execution_time stops scripts that run longer than 60 seconds.
- display_errors should be Off on a live site, since error messages can reveal paths and settings.
Common errors and how to fix them
- The uploaded file exceeds the upload_max_filesize directive
- The file is bigger than upload_max_filesize. Raise it and post_max_size, restart PHP, and check the web server limit as well.
- 413 Request Entity Too Large
- This comes from nginx, not PHP. Raise client_max_body_size in the server block.
- Allowed memory size of … bytes exhausted
- A script needs more than memory_limit. Raise the limit a little, or fix the script that uses too much, such as a huge query.
- Maximum execution time of 60 seconds exceeded
- The script ran too long. Raise max_execution_time for that job, or move long work to a background task.
- My changes do not apply
- PHP was not restarted, or you edited the wrong php.ini. The command line and PHP-FPM use different files. Check php --ini for CLI and phpinfo() for the web.
Frequently asked questions
- Where is php.ini?
- Run php --ini for the command line. For the web server, open a phpinfo() page and look at "Loaded Configuration File". CLI and web can use different files.
- Why do I need to change post_max_size too?
- A file upload is part of the whole POST request. If post_max_size is smaller than the file, the upload fails even when upload_max_filesize is large.
- Is it safe to display errors?
- Only on your own computer. On a live site keep display_errors off and read the log instead.
Fix a problem with this setup
- Laravel permission deniedFix "The stream or file storage/logs/laravel.log could not be opened: Permission denied": give the web user write access to storage and bootstrap/cache.
- nginx 502 Bad GatewayFix nginx 502 Bad Gateway with Node, PHP-FPM or any proxy: read the error log, check the app is running and the port or socket matches.
More server configs
- MySQL my.cnf settingsA small MySQL or MariaDB override file for connections, memory, character set and the slow query log.
- nginx reverse proxy configSend visitors from port 80 to an app running on port 3000.
- nginx PHP-FPM configRun a PHP site (WordPress, Laravel or plain PHP) on nginx with PHP-FPM.
- Caddyfile reverse proxySend visitors to an app on port 3000 and get HTTPS certificates automatically.
- crontab examplesRun a script every minute, hour, day or week.
- systemd service fileRun your app in the background and start it automatically at boot.