How to fix Upload issues with WordPress running Nginx on Ubuntu Linux

|

,

|

The article provides a tutorial on troubleshooting file upload issues in WordPress on the Nginx web server and PHP-FPM on Ubuntu 17.04 or 17.10. The author explains how to grant correct directory permissions, adjust PHP-FPM for larger file uploads, configure Nginx for larger file sizes, and restart to apply new settings.

One everyday issue users encounter when managing a WordPress website or blog on the Nginx web server is uploading files.

WordPress allows users to upload new themes and plugin files; however, if your Nginx-powered website isn’t configured to allow the large file to be uploaded, the upload process will always fail.

Some common errors users get when dealing with file uploads with WordPress are Http errors attempting to upload media, the uploaded file exceeding the upload_max_filesize directive in php.ini, maximum execution time exceeded, allowed memory size exhausted, and many more.

This brief tutorial is going to show students how to resolve some of these issues with WordPress running on Nginx and PHP-FPM on Ubuntu 17.04 | 17.10

Configure WordPress Directory Permissions

First, ensure that the directory WordPress is running has the correct permission for the Nginx web server to operate. On Ubuntu systems, the root directory is almost always at /var/www/html.

So, run the commands below to give the Nginx web server full access to that directory.

sudo chown -R www-data:www-data /var/www/html/
sudo chmod -R 755 /var/www/html/

Adjust PHP-FPM Setting

Next, adjust PHP-FPM settings to allow larger file uploads. By default, PHP-FPM is only allowed to upload a specific file size. So there’s a limit. Adjust the file upload size limit and other directives in PHP-FPM.

On Ubuntu 17.04 and up, the default PHP-FPM configuration file is stored in the file below:

/etc/php/7.x/fpm/php.ini

The x in the line above can either be a 0 or 1

So, open the PHP-FPM configuration file by running the commands below and adjusting the settings to suit your environment.

sudo nano /etc/php/7.1/fpm/php.ini

Then scroll down the file line by line and adjust each directive with the value below:

memory_limit = 256M
post_max_size = 32M
upload_max_filesize = 100M
file_uploads = On
max_execution_time = 600
max_input_time = 600

Save your changes.

If the file size you’re uploading is greater than 100MB, then adjust the upload_max_filesize to be greater than the file size.

Adjust Nginx Configuration

Nginx also has limited definitions. For example, if you don’t define the size limit in the Nginx configuration, whatever you do in the PHP configuration file may not apply to Nginx.

To allow Nginx also to upload a larger file, open Nginx configuration and add the values as defined below:

On Ubuntu systems, Nginx default site configuration files are stored at /etc/nginx/sites-available/default

If you have a custom file in there, adjust the highlighted values also.

server {
    listen 80;
    listen [::]:80;
    root /var/www/html/wordpress;
    index index.php index.html index.htm;
    server_name example.com www.example.com;

     client_max_body_size 100M;

location / {
    try_files $uri $uri/ /index.php?$query_string;
   }

location ~ \.php$ {
      include snippets/fastcgi-php.conf;
      fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
      fastcgi_connect_timeout 300s;
      fastcgi_read_timeout 300s;
      fastcgi_send_timeout 300s;
  }
}

Save the file and continue.

Finally, restart Nginx and PHP-FPM for the new settings to take effect

sudo systemctl reload nginx.serive
sudo systemctl reload php7.1-fpm.service

This should do it. Now go and try to upload the file you want with a size smaller than 100MB

Enjoy~

You may also like the post below:

Like this:



3 responses to “How to fix Upload issues with WordPress running Nginx on Ubuntu Linux”

  1. rivaansh Avatar
    rivaansh

    Great post. Thanks for sharing such wonderful knowledge. I’m so happy to get the answer i was looking for.
    Anyways can you suggest me some better options to get Cheap Linux hosting other than redserverhost.com.

  2. K Avatar
    K

    Thanks so much! This helped so much!

  3. ranga Avatar
    ranga

    amazing tutorial

    getting error while adding the following

    include fastcgi_params;
    fastcgi_connect_timeout 300s;
    fastcgi_read_timeout 300s;
    fastcgi_send_timeout 300s;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.