How to change PHP configuration (memory, upload limit and others) using the htaccess file?

 

PHP configuration (directives) like PHP memory limit, maximum upload size, post max size, and others can be set or changed using a [.htaccess] file. WordPress and other content management systems create a [.htaccess] file by default to store various configurations.

 

Modify PHP Configuration


You can add respective PHP directives to the [.htaccess] file to change PHP configuration:

  1. Go to cPanel > File Manager > [public_html] (or to the directory where your website files are).

    If you do not see the [.htaccess] file, enable "Show Hidden Files (dotfiles)" in "Settings" (on the top right of the window).


  2. Select the [.htaccess] file and click Edit.

    Or, right-click on the file and click Edit.


  3. Add the following directives to the [.htaccess] file, one in each line. Add only the ones you need:


    • Memory Limit

      php_value memory_limit 512M

      Replace 512 with the limit you want in megabyte. Larger values do not necessarily provide better performance. Consider assigning a value that is required for your website.


    • Maximum Upload File Size and/or Post Max Size

      php_value upload_max_filesize 62M
      php_value post_max_size 63M

      For file uploads to work correctly, the post_max_size directive should be slightly larger than the upload_max_filesize. For example, to set a file upload limit of 62 MB, you could set the upload_max_filesize directive to 62M and the post_max_size directive to 63M.


    • Maximum Execution Time and/or Maximum Input Time

      php_value max_execution_time 180
      php_value max_input_time 180

      Replace 180 with the value you want in seconds.


    • Maximum Input Variables

      php_value max_input_vars 1000

      Replace 1000 with the value you want.


    • Time Zone

      php_value date.timezone 'Region/Zone'

      Replace Region/Zone with your time zone to change the time zone that PHP uses for your scripts.

      List of supported time zones in PHP:
      http://www.php.net/manual/en/timezones.php
      http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone


    • Display PHP Error

      php_flag display_errors On

      Do not enable this directive unless you are debugging or troubleshooting, as it can expose vulnerable information about your website.


  4. Save the changes to the [.htaccess] file and exit the editor.

  5. Done!

 

Verify


To verify that the new setting is active:

  1. Create a PHP test file that contains the following code in the same directory where the [.htaccess] file is located:

    <?php phpinfo(); ?>

  2. Load the test file in your web browser and search for the directive's name. The Local Value column should display the new setting that you specified in the [.htaccess] file.

 

More Information


  • Complete list of PHP directives
    https://www.php.net/manual/en/ini.list.php.

  • memory_limit
    https://www.php.net/manual/en/ini.core.php#ini.memory-limit.

    upload_max_filesize
    https://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize
    .

    post_max_size
    https://www.php.net/manual/en/ini.core.php#ini.post-max-size
    .

    display_errors
    http://www.php.net/manual/en/errorfunc.configuration.php#ini.display-errors
    .

    max_execution_time
    http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time.

    date.timezone
    http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone.

 

  • 416 Users Found This Useful
Was this answer helpful?

Related Articles

How to enable Server Side Includes (SSI) using htaccess?

If you are using Server Side Includes SSI in your HTML files, and they are not working, you most...

How to secure your htaccess file?

For security reasons, the [.htaccess] file contents should not be visible to the public. To...

Error uploading large files (memory, HTTP or timeout error)

Change the following values in the [.htaccesss] file to upload large files:...