1

Can we increase the memory limit of PHP directly from the command line?

I am having more than one PHP installation on my ubuntu system and I am using a simple php-switch-scripts script to setup and switch between the PHP versions.

But I often run into a situation where I face some issues with the php.ini config values.

for example, I would have updated my memory_limit value to some higher value on my php.ini that is for PHP 7.2 but might not increased it on the php.ini of PHP 7.4.

Yes, I do understand that I could have updated the values in all of my php.ini files in less time than I took to write to this question but if there is a way I can automate this using a shell script I want to do it. so that both I and my colleagues make use of it in the future.

Thanks in advance...

1

1 Answer 1

6

You can do just about anything from the command line if you're really invested in solving the problem. For what you want to do, though, a couple of simple commands can help you accomplish the goal: grep, sed, and ideally cp.

To see the current value of something, use grep:

grep '^memory_limit ' /etc/php/7.4/apache2/php.ini

Be sure to replace 7.4 with whatever version you want to view at the time.

To replace an existing value, use sed:

sed -i 's,^memory_limit =.*$,memory_limit = 8192M,' /etc/php/7.4/apache2/php.ini

Again, replace 7.4 with the version you want to update and the absurd 8192M bit with the actual value you want to set.

Once this is done, restart Apache with:

sudo service apache2 restart

You now have all the basic elements required to write a custom script to accomplish your goal and, perhaps, go a little further by making it work with any valid setting. For example:

./setPHPvariable 7.4 memory_limit 128M
./setPHPvariable 7.2 max_execution_time 300

Note that it's a good idea to create a backup of the php.ini file before making changes. This is where cp is your friend.

1
  • Thank you for your valuable inputs @Matigo Dec 24, 2020 at 5:26

Not the answer you're looking for? Browse other questions tagged .