New Try managed PHP hosting on Amezmo for FREE with a $50 account credit. Create account →

The Complete Guide To Increasing Your PHP Max Upload Size

Avatar
Ryan McCullagh on
How to change the Max Upload Size in PHP

Overview

Increasing or decreasing the max upload size for our PHP applications is a common requirement when building real world apps. This guide will walk you through all the required steps to changing the max upload size in PHP and Nginx. Nginx ultimately affects PHP’s max upload size.

In our default installation of Ubuntu 16.04, we have Nginx, and PHP-FPM installed. Let’s take a look at these values and their defaults.

To change the PHP Max Upload Size, we must consider 3 components within the configuration of Nginx and PHP-FPM:

Component Configuration Setting Default
Nginx client_max_body_size 1M
PHP-FPM upload_max_filesize 2M
PHP-FPM post_max_size 8M

In this guide, you will increase your PHP-FPM max upload size as well as the Nginx max upload size to be 512MB. The configuration we’ll end up with matches the default settings for PHP hosting on Amezmo.

Step 1 – Change The Nginx max upload size configuration setting

For Nginx, the setting that we’re changing is client_max_body_size. Nginx comes by default with a relatively low maximum HTTP body size. The term “max upload size” is used, but more technically, we mean the maximum size of the body part of an HTTP request Additionally, because Nginx is a HTTP server, there won’t be any reference to the term “upload” in its configuration.

The setting that controls the maximum body size of an HTTP request in Nginx is called client_max_body_size. This setting can be defined in any .conf file in our Nginx configuration directory. For the sake of simplicity, we will edit the default nginx.conf file which is available in the default installation of Nginx.

To start editing our Nginx configuration, open up /etc/nginx/nginx.conf with your favorite text editor. I’m using VIM.

sudo vim /etc/nginx/nginx.conf

Find the start of the http section, and modify it to resemble the following.

/etc/nginx/nginx.conf
http {
    # Other settings omitted for the purpose of this document only
    # ... 
    client_max_body_size 512m;
    # ...
    # Other settings omitted for the purpose of this document only
}

Next, save the file and test the configuration.

sudo nginx -t

After confirming the configuration is valid, reload Nginx.

sudo systemctl reload nginx

Nginx is able to reload its configuration without having to completely restart.

Tired of editing configuration files?
Sign up to receive latest PHP hosting and deployment guides from Amezmo.

Step 2 – Update the max upload size configuration settings in PHP-FPM

In PHP, and more specifically, PHP-FPM, there are two settings that need to be changed. upload_max_filesize and post_max_size. Additionally, because we don’t want to modify the default PHP-FPM configuration files, we’ll create a new file specifically for our use. PHP-FPM loads all of the .ini files found in the configuration directory.

To modify PHP-FPM max upload size configuration entries, first determine your PHP version. Run the following command.

php -v

The output of this command should be similar to the output below. Take a note of the PHP version. In this guide, we’re using PHP 7.4

PHP 7.4.13 (cli) (built: Nov 28 2020 06:24:27) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.13, Copyright (c), by Zend Technologies

Next, navigate to the PHP-FPM configuration directory by running the following command.

cd /etc/php/7.4/fpm/conf.d

Once you’re in this directory, use your favorite text editor to create a new .ini file called 99-amezmo.ini which contains our custom configuration settings. Amezmo uses this naming convention in order for the file to be loaded last by PHP and ultimately take precedence over any other files in the directory. In case a file already defines the max upload settings, our file will take precedence.

sudo vim /etc/php/7.4/fpm/conf.d/99-amezmo.ini
/etc/php/7.4/fpm/conf.d/99-amezmo.ini
; Custom configuration settings

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 512M

; Maximum size of POST data that PHP will accept.
; Its value may be 0 to disable limit. It is ignored if POST data reading
; is disabled through enable_post_data_reading.
; http://php.net/post-max-size the
post_max_size = 512M

Next, save the file and reload PHP-FPM.

sudo systemctl reload php7.4-fpm

Conclusion

In this guide, you learned about the configuration settings that control the max upload size in PHP. We changed the default maximum to be 512MB and defined a custom PHP-FPM .ini file to hold our settings. Additionally, because Nginx is used as our HTTP server, we changed the default max upload size to be 512MB.

Host and Deploy Your PHP Apps for Free
Sign up today and see if Amezmo is right for you.