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?

James Smith

7 months ago

4 answers

126 views

Rating

02
Answer

Answers

Jonas Becker

7 months ago

1 comment

Rating

00

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.ini

Replace 7.x with your PHP version and use your preferred text editor if not nano.

Locate and adjust the following parameters:

		
upload_max_filesize = 50M post_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.com

Add 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 -t

If no errors, reload NGINX:

		
sudo systemctl reload nginx

Restart PHP-FPM:

		
sudo systemctl restart php7.x-fpm

Replace 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.php

Add 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

Reply

    James Smith

    7 months ago

    1 comment

    Rating

    00

    Did as you suggested, but the file won't upload...

    Reply

      Jonas Becker

      7 months ago

      1 comment

      Rating

      00

      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.

      Reply

        James Smith

        7 months ago

        Rating

        00

        Everything worked great.

        Your tips helped a lot!

        Something must not have updated when I checked, now everything works.

        Reply