| |

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:

  1. Nginx
  2. PHP
  3. 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 than upload_max_filesize.
  • memory_limit: Maximum amount of memory a script may consume. Set higher to handle large file processing.
  • max_execution_time and max_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

  1. Edit your Nginx configuration file (often located at /etc/nginx/nginx.conf or in the /etc/nginx/sites-available/ directory).
  2. Modify your PHP configuration (usually at /etc/php/8.2/fpm/php.ini, adjust for your PHP version).
  3. Edit your WordPress configuration file (wp-config.php in your WordPress root directory).
  4. 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:

  1. Try uploading a large file through the WordPress media library.
  2. Attempt a backup or migration using UpdraftPlus or WPvivid.
  3. 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:

  1. Only increase limits to what’s necessary for your use case.
  2. Implement proper file type restrictions in WordPress.
  3. Use security plugins to scan uploaded files for malware.
  4. Regularly update WordPress, themes, and plugins.

Troubleshooting

If you’re still encountering issues:

  1. Check your server’s error logs (/var/log/nginx/error.log and PHP error log).
  2. Ensure your hosting plan supports the increased resource usage.
  3. 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.

Similar Posts