How to Increase Magento PHP Memory Limit
Magento can require a significant amount of PHP memory, particularly when installing extensions, importing products, running Magento CLI commands, compiling code, indexing, or performing upgrades. If the PHP memory limit is too low, Magento may display errors such as “Allowed memory size exhausted.”
Increasing the PHP memory limit gives PHP more memory to complete resource-intensive Magento operations.
What Is the PHP Memory Limit?
The PHP memory_limit setting determines the maximum amount of RAM that a single PHP process can use.
For example:
memory_limit = 512M
means one PHP process can use up to approximately 512 MB of memory.
For Magento, 512M is a common starting point, although the appropriate value depends on your Magento version, workload, extensions, and server resources.
1. Check the Current PHP Memory Limit
Before changing anything, check the current value.
From the command line:
php -i | grep memory_limit
You may see:
memory_limit => 256M => 256M
You can also create a temporary PHP information page containing:
<?php
phpinfo();
Open the file in your browser and search for memory_limit.
Remove the
phpinfo()file after checking it because it exposes detailed server configuration.
2. Increase Memory Limit Using php.ini
The most common method is to modify the PHP configuration file.
First find the loaded PHP configuration:
php --ini
You may see something similar to:
Loaded Configuration File: /etc/php/8.2/cli/php.ini
Open the appropriate php.ini file:
nano /etc/php/8.2/fpm/php.ini
Find:
memory_limit = 256M
Change it to:
memory_limit = 512M
Save the file.
Then restart PHP-FPM:
systemctl restart php8.2-fpm
The exact service name depends on your installed PHP version.
3. Increase Memory Limit for Magento CLI
One important detail is that CLI PHP and web PHP can use different configuration files.
Check the CLI limit:
php -i | grep memory_limit
If Magento CLI commands require more memory, you can temporarily run a command with a higher limit:
php -d memory_limit=2G bin/magento cache:flush
For example, when running a resource-intensive operation:
php -d memory_limit=2G bin/magento indexer:reindex
This changes the memory limit only for that command.
4. Increase Memory Limit Using .user.ini
On hosting environments where you cannot edit the main php.ini, you may be able to use .user.ini.
Create or edit:
.user.ini
Add:
memory_limit = 512M
Place the file in the appropriate Magento web directory, commonly the site’s document root.
Depending on the PHP configuration, it may take some time for PHP to recognize changes to .user.ini.
5. Increase Memory Limit Using .htaccess
On Apache servers using PHP as an appropriate module, you may be able to set the limit through .htaccess:
php_value memory_limit 512M
However, this method does not work with every PHP setup, particularly common PHP-FPM configurations.
If it causes an HTTP 500 error, remove the setting and configure the limit through php.ini, .user.ini, or your hosting control panel instead.
6. Change PHP Memory Limit in DirectAdmin
If your Magento website is hosted with DirectAdmin, you may be able to change the PHP memory limit from the control panel.
Depending on the DirectAdmin configuration:
- Log in to DirectAdmin.
- Open PHP Settings or the relevant domain PHP configuration.
- Locate
memory_limit. - Change it from something such as:
256Mto:512M - Save the changes.
The exact interface depends on the server’s PHP mode and DirectAdmin configuration.
7. Change PHP Memory Limit in cPanel
If you are using cPanel:
- Log in to cPanel.
- Open MultiPHP INI Editor.
- Select your Magento domain.
- Find
memory_limit. - Set it to:
512M - Save the configuration.
You can then verify the setting from the command line or with a temporary PHP information page.
8. Verify the New Memory Limit
After changing the configuration, verify it.
For CLI PHP:
php -i | grep memory_limit
You should see:
memory_limit => 512M => 512M
For web requests, verify the PHP-FPM/web PHP configuration separately because it may use a different php.ini.
9. Choose an Appropriate Memory Limit
There is no single value that is correct for every Magento installation.
A general starting point could be:
| Magento Task | Suggested Starting Point |
|---|---|
| Normal storefront | 512M |
| Magento CLI operations | 1G |
| Large catalog operations | 1–2G |
| Large imports/indexing | 1–2G+ |
| Magento upgrades | 1–2G |
These are starting points, not mandatory requirements. Your server must have enough available RAM to support the selected limits.
For example, setting:
memory_limit = 4G
does not automatically make Magento faster. If many PHP processes are allowed to consume several gigabytes simultaneously, the server can run out of RAM.
10. Don’t Confuse PHP Memory Limit With Server RAM
These are different settings.
For example, a server might have:
RAM: 16 GB
PHP memory_limit: 512 MB
The 512M limit applies to an individual PHP process. It does not mean that the entire server has only 512 MB available.
Similarly, increasing the PHP memory limit does not increase the physical RAM installed on the server.
11. Common Magento Memory Error
You may encounter an error similar to:
Fatal error: Allowed memory size of 268435456 bytes exhausted
This means PHP reached its configured memory limit.
For example:
268435456 bytes = 256 MB
Increasing the limit to:
memory_limit = 512M
may resolve the immediate problem.
However, if Magento repeatedly consumes excessive memory, investigate the underlying cause rather than continuously increasing the limit.
Potential causes include:
- Poorly coded extensions
- Large product imports
- Huge catalogs
- Inefficient custom code
- Database problems
- Large XML/CSV processing
- Resource-intensive indexing
- Magento upgrade processes
12. Restart PHP-FPM After Changes
When modifying the PHP-FPM configuration, restart the appropriate PHP-FPM service.
For example:
systemctl restart php8.2-fpm
Then verify:
php -i | grep memory_limit
Remember that the CLI and web server may use different PHP configurations.
Troubleshooting
The memory limit didn’t change
Check which PHP configuration is being used:
php --ini
For web requests, check the PHP-FPM configuration or hosting control panel.
Magento still reports the old limit
You may have changed the CLI configuration while Magento’s web requests use another PHP configuration.
Check both environments separately.
Increasing memory doesn’t solve the problem
If Magento continues consuming excessive memory, investigate:
- Extensions
- Custom modules
- Product imports
- Indexers
- Database queries
- PHP-FPM configuration
- Magento cron jobs
Increasing the limit indefinitely can hide the real problem.
Conclusion
Increasing Magento’s PHP memory limit is straightforward, but it should be done carefully. For many Magento installations, 512M is a reasonable starting point, while resource-intensive CLI operations may need 1G or more.
The basic process is:
Check current limit
↓
Choose an appropriate value
↓
Modify PHP configuration
↓
Restart PHP-FPM if required
↓
Verify the new value
↓
Test Magento operations
If Magento continues to require unusually large amounts of memory, focus on finding the underlying performance or code problem instead of simply increasing memory_limit.