I want to change some php.ini
(php5.6) variables through the terminal.
Example: I need to get the post_max_size
value (that for now is 8M
), display it in the terminal, change it to 2048M
and display it again.
How could I do that?
I want to change some php.ini
(php5.6) variables through the terminal.
Example: I need to get the post_max_size
value (that for now is 8M
), display it in the terminal, change it to 2048M
and display it again.
How could I do that?
Get:
Replace:
Note that it's a good idea to create a backup of
php.ini
before runningsed
:I assume you have the values in your
php.ini
stored one per line and separated by=
with or without surrounding spaces. Neither the variable names nor the values contain a=
.To print the
post_max_size
value (choose one):To change the
post_max_size
value to2048M
creating a backup calledphp.ini.bak
:Explanations
<php.ini awk -F"= *" '/^ *post_max_size/{print$2}'
<php.ini
– let the shell openphp.ini
and assign it to the program's stdin, this has a number of advantages, see here-F"= *"
– set=
followed by zero or more space characters as the field delimiter/^ *post_max_size/{print$2}
– from the line beginning withpost_max_size
print field2
<php.ini sed '/^ *post_max_size/!d;s/.*= *//'
/^ *post_max_size/!d
–d
elete every line except the one beginning withpost_max_size
s/.*= *//
–s
ubstitute everything before=
and zero or more space characters after it by nothing (= delete it)<php.ini grep -oP '^ *post_max_size *= *\K.*'
-oP
– printo
nly the matched parts of a matching line and useP
erl-compatible regular expressions (PCRE)^ *post_max_size *= *\K.*
– search for a line beginning withpost_max_size
and=
surrounded by zero or more space characters, then remove the text matched so far from the overall regex match (\K
) and match everything after itsed -i.bak '/^ *post_max_size/s/=.*/= 2048M/' php.ini
-i.bak
– change the filei
n place making a backup with the extension.bak
/^ *post_max_size/…
– in the line beginning withpost_max_size
, do…
s/=.*/= 2048M/
–s
ubstitute=
and everything after it with= 2048M
I wrote a simple bash script that implements Arkadiusz Drabczyk's answer.
The script auto detects the active version of php.
The script assumes you are using apache2 (you can pass a third arg to override).
I named the file
php-iniset
and added it to one of my bin directories.now you can call:
or call it like this to comment out the variable:
You might need to tweak things like the filepath to your needs
Remember to backup your ini file first!
Here is the script: