HostDean's content is reader-supported. If you click on some of our links, we may earn a small commission.
HostDean Guides

4 Ways to Increase PHP Memory Limit: php.ini, WordPress, cPanel & .htaccess

Increase PHP Memory Limit

Ensuring your website runs smoothly is crucial for providing a seamless user experience. One key factor in achieving this is managing your PHP memory limit effectively. By increasing the PHP memory limit, you can enhance your site’s performance, especially when handling resource-intensive applications or plugins.

Understanding PHP Memory Limit

The PHP memory limit defines the maximum amount of memory a PHP script can utilize on your server. By default, this limit is often set to a conservative value, such as 128MB, which may suffice for basic websites.

However, as your site grows and incorporates more complex functionalities, this default limit might become insufficient, leading to errors or slow performance.

When to Increase PHP Memory Limit?

Before modifying the PHP memory limit, it’s essential to recognize scenarios where an increase is necessary:

  • Frequent Memory-Related Errors: Encountering errors like “Allowed memory size exhausted” indicates that your scripts require more memory than the current limit allows.
  • Performance Bottlenecks: If your website experiences sluggishness, especially during peak traffic or while running intensive processes, it may benefit from a higher memory allocation.
  • Advanced Plugins or Applications: Utilizing complex plugins, content management systems, or applications that demand substantial resources can necessitate an increased PHP memory limit.

How to Increase PHP Memory Limit

There are several approaches to increasing the PHP memory limit, depending on your server configuration and access level.

1. Modifying the php.ini File

The php.ini file is the primary configuration file for PHP settings.

  • Accessing php.ini: Locate the php.ini file on your server. Its location can vary; common paths include /etc/php.ini, /usr/local/lib/php.ini, or within your hosting control panel.
  • Editing the File: Open the file in a text editor and find the line containing memory_limit. Modify it to reflect your desired limit, for example:
  memory_limit = 256M
Code language: Apache (apache)
  • Saving Changes: After editing, save the file and restart your web server to apply the new settings.

Note: If you don’t have access to the global php.ini file, you might be able to create a local php.ini file within your website’s root directory with the above directive.

2. Updating the wp-config.php File (For WordPress Sites)

WordPress users can increase the PHP memory limit via the wp-config.php file.

  • Accessing wp-config.php: This file resides in your WordPress installation’s root directory.
  • Editing the File: Add the following line before the statement /* That's all, stop editing! Happy blogging. */:
  define('WP_MEMORY_LIMIT', '256M');Code language: PHP (php)
  • Saving Changes: Save the file, and the new memory limit should take effect immediately.

Note: This method is specific to WordPress and won’t affect other PHP applications on your server.

3. Utilizing Hosting Control Panel Tools

Many hosting providers offer user-friendly interfaces to adjust PHP settings without manual file edits.

  • cPanel: Navigate to the “MultiPHP INI Editor” under the “Software” section. Select your domain and modify the memory_limit value as needed.
  • Plesk: Go to “PHP Settings” for your domain and adjust the memory_limit parameter accordingly.

Note: The availability of these tools and their interfaces can vary between hosting providers.

4. Adjusting the .htaccess File

For Apache servers, the .htaccess file can override PHP settings.

  • Locating .htaccess: Find the .htaccess file in your website’s root directory. If it doesn’t exist, you can create one.
  • Editing the File: Add the following line to set a new memory limit:
  php_value memory_limit 256MCode language: Apache (apache)
  • Saving Changes: Save the file and ensure your server recognizes the updated configuration.

Note: Some hosting providers restrict the use of certain directives in .htaccess. If you encounter a “500 Internal Server Error” after adding this line, remove it and consider alternative methods.

Verifying the Updated PHP Memory Limit

After implementing changes, it’s important to confirm that the new memory limit is active:

  • Using phpinfo(): Create a PHP file (e.g., info.php) with the following content:
  <?php
  phpinfo();
  ?>
Code language: PHP (php)

Access this file through your browser to view the current PHP configuration, including the memory_limit setting.

  • WordPress Dashboard: For WordPress sites, navigate to Tools > Site Health > Info > Server to check the PHP memory limit.

Considerations When Increasing The Memory Limit for PHP

While increasing the PHP memory limit can resolve certain issues, it’s essential to consider the following:

  • Server Resources: Ensure your server has adequate RAM to accommodate the increased memory allocation without affecting overall performance.
  • Application Efficiency: Regularly review your website’s code and plugins for efficiency. Sometimes, optimizing code can reduce memory usage, mitigating the need for higher limits.
  • Hosting Policies: Some hosting providers enforce maximum memory limits. Consult your host’s documentation or support to understand any restrictions.

Was this content helpful?

Feel free to share it with your network!

Latest Guides

Latest How-To Guides from HostDean