PHP sets a limit on how much memory a script can use to prevent system instability. When this limit is reached, PHP will terminate the script and display an error. You may need to increase this limit if your scripts require more memory.
PHP Fatal error: Allowed memory size of xxxx bytes exhausted (tried to allocate yyyy) in yourscript.php
To increase the PHP memory limit, you can modify configuration files or use specific PHP functions. Methods include editing the php.ini file, updating .htaccess, or using the ini_set() function in your script. These methods allow you to control how much memory your scripts can use, depending on your environment and needs.
For users managing PHP through cPanel, the memory limit can also be adjusted through the web interface. Each method serves a different purpose and applies the changes at different levels. Select the method that best suits your requirements.
Methods to increase PHP memory limit:
Increase PHP memory limit via file (php.ini)
The php.ini file is the main configuration file for PHP. Modifying the memory_limit directive in this file changes the memory limit for all PHP scripts running on the server. This method is suitable when you need a system-wide adjustment, especially if you're not managing multiple hosting environments on the same server.
Editing the php.ini file provides centralized control over PHP settings, making it ideal for consistent memory allocation across all scripts. Once the changes are made, the new memory limit will apply globally after restarting the web server. This method requires administrative access to the server.
- Open PHP configuration file using your preferred text editor.
$ sudo vi /etc/php/7.2/apache2/php.ini
Related: PHP configuration files
- Search for memory_limit directive within the configuration file.
; Maximum amount of memory a script may consume (128MB) ; http://php.net/memory-limit memory_limit = 128MB
- Set the memory limit value with appropriate unit.
; Maximum amount of memory a script may consume (128MB) ; http://php.net/memory-limit memory_limit = 512MB
- Restart your web server for the changes to take effect.
Increase PHP memory limit via .htaccess
The .htaccess file provides a way to configure the server for a specific directory. This method is beneficial for users without administrative access, allowing them to change the memory_limit for all scripts within a particular directory.
To increase the memory limit for all scripts within a specific directory, edit the .htaccess file in that directory. Add the following line to set the desired memory limit:
php_value memory_limit 512M
This method is especially useful when you do not have access to the server's php.ini file or need to apply the change to a specific project folder.
Increase PHP memory limit via ini_set() function
The ini_set() function allows you to override the memory_limit setting within a specific PHP script. This method is useful when you need to increase the memory limit for a single script without affecting the entire server.
Add the ini_set() function to your PHP script to set the desired memory limit. This change will only apply to the script where it is included, allowing the rest of the server to maintain the default configuration. An example of the function call is as follows:
ini_set('memory_limit', '512M');
Increase PHP memory limit for cPanel
cPanel provides a web-based interface for managing your hosting environment. It includes an option to adjust the memory_limit for PHP scripts. This method is ideal for users who prefer a graphical interface over editing configuration files manually.
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.