How do I upload WordPress files larger than 2 MB to my Elastic Beanstalk environment?

4 minute read
0

When I upload WordPress files larger than 2 MB to my AWS Elastic Beanstalk environment, I receive the following error message: "The uploaded file exceeds the upload_max_filesize directive in php.ini."

Short description

The default PHP configuration on an Amazon Elastic Compute Cloud (Amazon EC2) instance doesn't allow you to upload WordPress files larger than 2 MB. To upload files larger than 2 MB, change the default value of the upload_max_filesize parameter in the /etc/php.ini file.

Important: M and MB are equivalent expressions for "megabyte." For example, 2M is equal to 2 MB. However, use only M in your configuration file, as MB isn't valid in a configuration file.

Resolution

Choose the resolution that applies to your solution stack.

Update the upload file size for an Elastic Beanstalk environment that runs a PHP solution stack

To increase the file size across all instances, complete the following steps to update the underlying Amazon Machine Image (AMI).

1.    In a code editor, create a new file named change_upload_size.config, and then add the following code to the file:

files:    "/etc/php.d/99uploadsize.ini":
        mode: "000644"
        owner: root
        group: root
        content: |
            upload_max_filesize = 100M
            post_max_size = 100M
commands:
    remove_old_ini:
        command: "rm -f /etc/php.d/99uploadsize.ini.bak"

Note: In the preceding example, the value of the post_max_size parameter is updated from the default 32M to 100M.

To modify php.ini parameters such as memory_limit, see the php.ini directives for post_max_size. The maximum memory_limit value in Elastic Beanstalk is 256M.

2.    In the root of your application source bundle, create a new directory called .ebextensions, and then move the change_upload_size.config file to the .ebextensions directory.

If the .ebextensions directory is already inside your application source bundle, then keep the change_upload_size.config file in the .ebextensions directory with the other .config files.
Example:

├── wp-site.zip/     ├── .ebextensions/
         ├── change_upload_size.config
    ├── wp-content/
    ├── wp-includes/
    ├── wp-admin/
    ├── LICENSE
    ├── README.md
    ├── index.php
    ├── license.txt
    ├── readme.html
    ├── ...
    ├── ...
    ├── ...
    ├── ...
    ├── wp-settings.php
    ├── wp-signup.php
    ├── wp-trackback.php
    └── xmlrpc.php

Important: For Amazon Linux 2 platforms based on Elastic Beanstalk, change the file name 99uploadsize.ini to z99uploadsize.ini. To allow the new PHP settings to override the defaults, change the file name everywhere in the .ebextension.

3.    Open the Elastic Beanstalk console, and then create a zip archive of your application source bundle.

4.    To upload your code to an existing Elastic Beanstalk environment, choose your environment, and then choose Upload and Deploy.
Note: To deploy your code in a new Elastic Beanstalk environment, see Creating an Elastic Beanstalk environment.

After you upload the .ebextensions file, if the error message remains, then use a YAML validator to check for white space errors in your code. Also, use the remove_old_ini command to remove unnecessary backup files. For more information, see Extending php.ini.

Change the upload file size for a single Amazon EC2 instance that runs a LAMP stack

1.    To open the /etc/php.ini file with the vi editor, run the following command:

$ sudo vi /etc/php.ini

Note: The /etc/php.ini file name or path can vary depending on your version of PHP or the AMI that you use.

2.    In the vi editor, change the value of the upload_max_filesize parameter to a larger size than 2 MB, and then save the file.
Note: To modify php.ini parameters such as memory_limit, see the php.ini directives for post_max_size. For more information, see the post_max_size entry on the PHP website.

3.    To restart the Apache server gracefully, run the appropriate command for your environment.

For Amazon Linux:

$ sudo service httpd graceful

For Amazon Linux 2:

$ sudo service httpd reload

Note: The /etc/php.ini file name or path can vary depending on your version of PHP or the AMI that you use. The command to restart the Apache server can also vary if you use a distribution other than Amazon Linux AMI. WordPress limits the memory of its PHP processes by default.

Related information

Advanced environment customization with configuration files (.ebextensions)

Deploying a high-availability WordPress website with an external Amazon Relational Database Service (Amazon RDS) database to Elastic Beanstalk

Host a WordPress blog on Amazon Linux 2

Description of core php.ini directives on the PHP website

AWS OFFICIAL
AWS OFFICIALUpdated 5 months ago
2 Comments

Using the above code caused a parsing error like:

The configuration file .ebextensions/change_upload_size.config in application version code-pipeline-1706283118500-64f61c4d04875f47692113e6a79cb65b0ff665fb contains invalid YAML or JSON. YAML exception: Invalid Yaml: mapping values are not allowed here in 'reader', line 1, column 37: files: "/etc/php.d/99uploadsize.ini": ^ , JSON exception: Invalid JSON: Unexpected character (f) at position 0.. Update the configuration file.

Correct code is:

files:
  "/etc/php.d/99uploadsize.ini":
    mode: "000644"
    owner: root
    group: root
    content: |
      upload_max_filesize = 100M
      post_max_size = 100M
commands:
  remove_old_ini:
    command: "rm -f /etc/php.d/99uploadsize.ini.bak"

Note the 2 space indent and the file name on line 2 on a separate line

replied a month ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied a month ago