WordPress Upload Guide: Nginx & PHP-FPM Optimization
If you’re a WordPress user hosting your site on an Nginx server with PHP-FPM, you may encounter issues when uploading large files or migrating your site using popular plugins like UpdraftPlus or WPvivid. This guide will help you configure your server to handle large file uploads smoothly, ensuring a hassle-free experience when managing your WordPress site.
Understanding the Problem
The “413 Request Entity Too Large” error often occurs when trying to upload files that exceed the server’s configured limits. This can happen during regular file uploads or when using migration plugins that need to transfer large backup files.
Key Configuration Areas
To resolve these issues, we need to adjust settings in three main areas:
- Nginx
- PHP
- WordPress
Let’s dive into each of these:
1. Nginx Configuration
The primary setting we need to adjust in Nginx is the client_max_body_size
directive. This determines the maximum allowed size of the client request body.
Add or modify this directive in your Nginx configuration:
client_max_body_size 256M;
You can place this in the http
, server
, or location
block of your Nginx config file, depending on how granular you want the setting to be.
2. PHP Configuration
For PHP, we need to adjust several settings. Instead of modifying the main php.ini
file directly, it’s better to use a custom configuration file. This approach is more maintainable and less likely to be overwritten during PHP updates.
Create or edit the file /etc/php/8.2/fpm/conf.d/60-custom.ini
:
upload_max_filesize = 256M
post_max_size = 257M
memory_limit = 512M
max_execution_time = 300
max_input_time = 300
Here’s what each of these does:
upload_max_filesize
: Maximum size of an uploaded file.post_max_size
: Maximum size of POST data that PHP will accept. Should be slightly larger thanupload_max_filesize
.memory_limit
: Maximum amount of memory a script may consume. Set higher to handle large file processing.max_execution_time
andmax_input_time
: Maximum time in seconds a script is allowed to run and to parse input data, respectively.
3. WordPress Configuration
While WordPress itself doesn’t need much configuration for file uploads, you might want to add this line to your wp-config.php
file to increase the WordPress memory limit:
define('WP_MEMORY_LIMIT', '256M');
Implementing the Changes
- Edit your Nginx configuration file (often located at
/etc/nginx/nginx.conf
or in the/etc/nginx/sites-available/
directory). - Modify your PHP configuration (usually at
/etc/php/8.2/fpm/php.ini
, adjust for your PHP version). - Edit your WordPress configuration file (
wp-config.php
in your WordPress root directory). - After making changes, restart Nginx and PHP-FPM:
systemctl restart nginx
systemctl restart php8.2-fpm # Adjust for your PHP version
Testing Your Configuration
After implementing these changes, you can test your settings:
- Try uploading a large file through the WordPress media library.
- Attempt a backup or migration using UpdraftPlus or WPvivid.
- Use a PHP info page to verify your PHP settings have been applied correctly.
Security Considerations
While increasing these limits is often necessary, it’s important to consider security implications:
- Only increase limits to what’s necessary for your use case.
- Implement proper file type restrictions in WordPress.
- Use security plugins to scan uploaded files for malware.
- Regularly update WordPress, themes, and plugins.
Troubleshooting
If you’re still encountering issues:
- Check your server’s error logs (
/var/log/nginx/error.log
and PHP error log). - Ensure your hosting plan supports the increased resource usage.
- Consider temporary increases to
max_execution_time
for one-time large migrations.
By following this guide, you should be able to handle large file uploads and migrations in WordPress without encountering the dreaded 413 error. Remember to adjust these settings based on your specific needs and server capabilities.