Era Host hosting
EraHost – Free Domain, Cheap Hosting!
Client Area
Support 24/7
Menu

Modify php.ini to Set memory_limit = -1 (Unlimited Memory Usage)

3 min read
10.05.2026

The memory_limit directive in PHP controls the maximum amount of memory a script can use. Setting it to -1 removes all memory restrictions, allowing PHP scripts to use as much RAM as needed.

php.ini memory_limit -1
php.ini memory_limit = -1 — affects all scripts; usually wrong on prod.

For closely related memory_limit topics, see How to Update PHP Memory Limit for Magento 2, Magento CLI PHP Memory Limit, and Increasing PHP Limits.

Locate the php.ini File

To modify php.ini, you first need to locate it.

Check Current PHP Configuration

Run this command:

php --ini

It will return something like:

Configuration File (php.ini) Path: /etc/php/7.4/apache2

Loaded Configuration File: /etc/php/7.4/apache2/php.ini

This tells you the correct php.ini file to edit.

Alternatively, create a PHP script:

<?php

phpinfo();

?>

Look for "Loaded Configuration File" in the output.

Modify php.ini to Set memory_limit = -1

Open php.ini with a text editor

sudo nano /etc/php/7.4/apache2/php.ini  # Ubuntu/Debian (Apache)
sudo nano /etc/php/7.4/cli/php.ini      # Ubuntu/Debian (CLI)
sudo nano /etc/php.ini                  # CentOS/RHEL

Find the line

memory_limit = 128M

or

memory_limit = 512M

Change it to

memory_limit = -1

Save and exit

Press Ctrl + X, then Y, then Enter.

Restart PHP and Web Server

For Apache:

sudo systemctl restart apache2

For Nginx with PHP-FPM:

sudo systemctl restart php7.4-fpm
sudo systemctl restart nginx

For cPanel (if applicable):

service httpd restart

Verify the Change

Run:

php -i | grep memory_limit

Expected output:

memory_limit => -1 => -1

Alternatively, create a PHP file:

<?php

echo ini_get('memory_limit');

?>

Visit the page in your browser and check if it returns -1.

Alternative: Modify .htaccess (For Shared Hosting)

If you cannot edit php.ini, try adding this to .htaccess:

php_value memory_limit -1

Then restart your web server.

Alternative: Modify via ini_set() (For Specific Scripts)

If you only want to remove memory limits for a specific PHP script, use:

ini_set('memory_limit', '-1');
Linux VDS
High performance for your projects
  • Root access and flexible setup
  • Control panel
  • NVMe disks
  • DDR5
Linux VDS

Warnings & Best Practices

When to Use memory_limit = -1:

  • Running memory-heavy scripts (e.g., image processing, database exports, large file processing).
  • Debugging memory allocation issues.

When Not to Use memory_limit = -1:

  • On shared hosting (may crash other users websites).
  • In production without monitoring (can cause server crashes).

Alternative:

Instead of unlimited memory, set a high but safe value, e.g.:

memory_limit = 512M
memory_limit = 1G
  1. Edit php.ini, set memory_limit = -1.
  2. Restart Apache/Nginx.
  3. Verify using php -i | grep memory_limit.
  4. Consider alternatives like .htaccess or ini_set() if needed.
  5. Use unlimited memory only when necessary to prevent crashes.
Frequently asked questions
Single runaway request consumes all server RAM, OOM killer fires, takes down PHP-FPM AND possibly other processes. memory_limit is a safety valve; remove it only if you know the workload's bounded. For deploy tasks, use CLI `-d memory_limit=-1`; for production web requests, set a generous but capped limit (4G typical for heavy apps).
`.user.ini` in document root: `memory_limit = 4G`. Or PHP-FPM pool config: `php_admin_value[memory_limit] = 4G`. Pool config wins over .user.ini for FPM. Per-vhost limits let one site have higher cap without affecting others on same server.
For dev: yes. Dev environment is single-user, no production traffic, no real OOM risk. setup:upgrade and other operations succeed without fiddling with CLI flags. For production-mirroring dev: cap at production value so dev catches what prod will refuse.
Different php.ini files. CLI: `/etc/php//cli/php.ini`. Web FPM: `/etc/php//fpm/php.ini`. Apache mod_php: `/etc/php//apache2/php.ini`. Each has independent memory_limit. Need different values per SAPI — CLI usually higher (or -1) for deploy scripts; web capped for safety.
Related articles
PHP memory_limit = -1: What Does It Mean?
Recommended PHP Memory Limit for Magento 2
Set PHP memory_limit = -1 via Command Line (For CLI Scripts)