How to Correct “PHP Fatal error: Allowed memory size” Errors from WHM

When managing a web server, especially one running multiple websites or applications, encountering the “PHP Fatal error: Allowed memory size” error is common. This error occurs when a PHP script exceeds the memory limit allocated to it, which is defined by the memory_limit directive in the php.ini file. Addressing this error from WHM (Web Host Manager) involves adjusting the PHP memory limit and ensuring the changes take effect across all relevant domains and applications.

 

Understanding the Error

 

The error message typically looks like this:

 

PHP Fatal error: Allowed memory size of X bytes exhausted (tried to allocate Y bytes)

 

Here, X represents the current memory limit, and Y represents the additional memory the script attempted to use.

 

Steps to Correct the Error from WHM

 

  1. Access WHM

 

Log into your WHM interface using your administrator credentials.

 

  1. Navigate to PHP Configuration Editor

 

  1. Search for “PHP Configuration Editor”:

   – In the WHM search bar, type “PHP Configuration Editor”.

   – Click on “PHP Configuration Editor” under the “Service Configuration” section.

 

  1. Basic Mode vs. Advanced Mode:

   – WHM offers two modes for editing PHP settings: Basic and Advanced.

   – For most users, the Basic Mode is sufficient. If you need more detailed configuration options, switch to Advanced Mode.

 

  1. Increase the Memory Limit

 

  1. In Basic Mode:

   – Scroll down to find the memory_limit directive.

   – Increase the value according to your needs. Common values are 128M, 256M, or higher, depending on the application’s requirements.

 

   Example:

   plaintext

   memory_limit = 256M

  

 

  1. In Advanced Mode:

   – Locate the memory_limit directive in the list.

   – Change its value to a higher limit.

 

   Example:

   ini

   memory_limit = 256M

  

 

  1. Save Changes:

   – After making the changes, click on the “Save” button to apply the new configuration.

 

  1. Apply Changes to All PHP Versions

 

If your server hosts multiple PHP versions, you need to ensure that the memory limit is increased for all PHP versions in use:

 

  1. MultiPHP INI Editor:

   – In WHM, search for “MultiPHP INI Editor”.

   – Select the PHP version you want to configure from the drop-down menu.

 

  1. Update Memory Limit:

   – Locate the memory_limit directive.

   – Increase the value as required.

   – Save the changes.

 

   Repeat this for each PHP version that is active on your server.

 

  1. Adjust Memory Limit for Specific Domains

 

If only specific domains require a higher memory limit, you can adjust their settings individually:

 

  1. MultiPHP INI Editor – Editor Mode:

   – In WHM, search for “MultiPHP INI Editor” and select “Editor Mode”.

   – Choose the domain you want to configure.

 

  1. Set Memory Limit:

   – Add or modify the memory_limit directive for the selected domain.

   – Save the changes.

 

   Example:

   ini

   memory_limit = 256M

  

 

  1. Using .htaccess to Override PHP Settings

 

For individual sites, you can also override PHP settings using an .htaccess file if your server configuration allows it:

 

  1. Edit .htaccess File:

   – Navigate to the root directory of the website.

   – Open or create the .htaccess file.

 

  1. Add PHP Value:

   – Add the following line to increase the memory limit:

   apache

   php_value memory_limit 256M

  

 

  1. Save Changes:

   – Save the .htaccess file.

 

  1. Restart Apache and PHP-FPM

 

After making changes to PHP configuration files, it’s often necessary to restart the Apache and PHP-FPM services to apply the new settings:

 

  1. Restart Apache:

   – In WHM, go to “Restart Services”.

   – Select “HTTP Server (Apache)” and click “Yes” to restart.

 

  1. Restart PHP-FPM:

   – If you use PHP-FPM, restart the PHP-FPM service for the changes to take effect.

 

  

   sudo systemctl restart php-fpm

  

 

  1. Verify the Changes

 

To ensure the changes have been applied correctly:

 

  1. Create a PHP Info File:

   – Create a phpinfo.php file in the root directory of the domain:

 

   php

   <?php

   phpinfo();

   ?>

  1. Access the PHP Info File:

   – Open a web browser and navigate to http://your-domain/phpinfo.php.

   – Look for the memory_limit directive to confirm the new value.

  1. Delete the PHP Info File:

   – For security reasons, delete the phpinfo.php file after verification:

   rm /path/to/phpinfo.php

Additional Considerations

– Monitor Resource Usage: Increasing the memory limit can help resolve errors, but it may also lead to higher memory usage on your server. Monitor your server’s resource usage to ensure it can handle the new configuration without performance issues.

– Script Optimization: Sometimes, high memory usage is due to inefficient scripts. Consider reviewing and optimizing your PHP code to use memory more efficiently.

– Regular Updates: Keep your PHP versions and associated modules up to date to benefit from performance improvements and security patches.

By following these steps, you can effectively correct “PHP Fatal error: Allowed memory size” errors from WHM, ensuring that your PHP scripts have the necessary memory to function correctly while maintaining optimal server performance.