0

Is there a php (or composer) terminal command to change the php.ini variables upload_max_filesize and post_max_size from terminal to some value directly from terminal, without sed or awk operations?

The reason; having a more a version-agnostic and more backward-compatible directive in my server-environment-establishment script, to change the values of these two variables.

Update

If I'm not wrong utilizing the confd CM (supported in PHP >= 5.0) should be good (I can't test right now):

1. Create something like /etc/php7/conf.d/local.ini.

2. Add to it:

upload_max_filesize = 2000M
post_max_size = 2000M
2
  • Change them in your php application with ini_set ?
    – Tensibai
    Commented Nov 20, 2018 at 12:51
  • Do not use ini_set!
    – Kyslik
    Commented Nov 29, 2018 at 9:44

3 Answers 3

3

The reason; having a more a version-agnostic and more backward-compatible directive in my server-environment-establishment script, to change the values of these two variables.

If you're trying to configure a server, you should use a configuration management system (Ansible, Puppet, Chef, Salt) or bake images for immutable infrastructure; trying to hack in pieces of configuration via PHP or bash or whatever will just make you unhappy.

3
  • I am sorry Xiong but it seems Ansible (which is helpful in so much) isn't very helpful in this particular case please look: unix.stackexchange.com/questions/489281/…
    – user5176
    Commented Dec 17, 2018 at 7:14
  • There isn't an Ansible module to change just that value, but I would throw the entire php.ini in a jinja template; you'll want to control it all anyways. Commented Dec 18, 2018 at 6:49
  • Oh curd, I linked to the answer instead to the question (which I'm sure you've seen)... Anyways, I have no idea what's a jinja template. If you have the spare time and will, I'll appreciate a short answer there - it would be helpful indeed...
    – user5176
    Commented Dec 18, 2018 at 7:13
1

PHP has supported conf.d configuration subdirectories for ages now (cf. PHP_INI_SCAN_DIR in its documentation, this stuff is as old as PHP 5 basically) - allowing you to simply add a small file containing just the snippet of config you need in there, and as long as you make sure it's named so that it's ordered in a way it overrides the defaults, you're done.

IOW you don't go searching for an existing value to fiddle with, you just add your own.

4
  • Seems a great way, but it's strange for me no one mentioned it before you (also in AskUbuntu and I never came across conf.d in Apache, only in Nginx). Are you sure you don't mean to Nginx?... Please example what you will do in Apache...
    – user5176
    Commented Nov 28, 2018 at 21:53
  • 1
    These are all separate programs, all of which have their own method of including configuration subdirectories. Apache has used it with the name conf.d, before they switched to conf-available/enabled, and they effectively use the same thing with mods-available/enabled and sites-available/enabled. nginx uses it for conf.d, sites-available/enabled. php uses it for conf.d, in each of the SAPI config directories. It's actually quite widespread. There may be platform differences, though, I'm primarily referring here to Debian/Ubuntu. Commented Nov 29, 2018 at 11:20
  • Nice to know ! (I'm not a PHP programmer nor I know Apache well). I primarily use Debian Ubuntu. I upvoted your answer after this explanation as this seems promising. I encourage you to show an example of what you would have changed. Of course I'll accept this answer if it'll include a way that works for me well.
    – user5176
    Commented Nov 29, 2018 at 12:57
  • Have a look at e.g. stackoverflow.com/a/34724226/4617744 Commented Nov 29, 2018 at 15:28
0

You can use regex and after that change, you need to restart the webserver:

sed -i 's/upload_max_filesize\s*=.*/upload_max_filesize=100M/g' path/to/php.ini
sed -i 's/post_max_size\s*=.*/post_max_size=100M/g' path/to/php.ini
sed -i 's/memory_limit\s*=.*/memory_limit=512M/g' path/to/php.ini

But it is preferable to use Configuration Management Tools

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.