How to increase the file upload limit in PHP on an Ubuntu server with NGINX?
I have a PHP web application deployed on an Ubuntu server using the NGINX web server. When trying to upload files larger than 10 MB, I encounter an error. How can I increase the file upload limit in PHP and configure NGINX to support larger files?
Answers
James Smith
Everything worked great.
Your tips helped a lot!
Something must not have updated when I checked, now everything works.
Jonas Becker
If issues persist:
Ensure your PHP code does not restrict the upload size. For example, in the upload form, add a hidden field MAX_FILE_SIZE:
Check client-side JavaScript for any limitations on file size.
Verify the file upload directory has the correct write permissions for PHP.
Double-check that no other configuration files impose lower file size limits. Consult the code developer if you did not write it.
James Smith
Did as you suggested, but the file won't upload...
Jonas Becker
To increase the PHP upload limit on Ubuntu with NGINX:
1. Modify php.ini settings
Open the PHP-FPM php.ini file for editing:
sudo nano /etc/php/7.x/fpm/php.iniReplace 7.x with your PHP version and use your preferred text editor if not nano.
Locate and adjust the following parameters:
upload_max_filesize = 50Mpost_max_size = 50M
max_execution_time = 300
max_input_time = 300
memory_limit = 256M
Ensure that post_max_size is not less than upload_max_filesize.
Save and close the editor (Ctrl + O, Enter, then Ctrl + X in nano).
2. Configure NGINX
Open your site’s configuration file, usually in /etc/nginx/sites-available/:
sudo nano /etc/nginx/sites-available/example.comAdd or modify the client_max_body_size directive with the desired value (e.g., 50M) inside the server block:
server {listen 80;
server_name example.com www.example.com;
client_max_body_size 50M;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
# Other settings...
}
Save and close the editor.
3. Check and Restart NGINX and PHP-FPM
Verify NGINX configuration syntax:
sudo nginx -tIf no errors, reload NGINX:
sudo systemctl reload nginxRestart PHP-FPM:
sudo systemctl restart php7.x-fpmReplace 7.x with your PHP version.
4. Confirm Settings via phpinfo()
To verify the changes, create an info.php file in your website's root directory:
sudo nano /var/www/html/info.phpAdd the following code:
Open the file in a browser at http://example.com/info.php. Check upload_max_filesize and post_max_size values to ensure they are updated.
After verification, delete info.php for security:
sudo rm /var/www/html/info.php