1

When I want to change the famous 2M scales for post_max_size and upload_max_size in php.ini, I use the following two-line sed solution which is version agnostic (see asterisk):

sed -i 's/post_max_size \= .M/post_max_size \= 200M/g'             /etc/php/*/fpm/php.ini
sed -i 's/upload_max_filesize \= .M/upload_max_filesize \= 200M/g' /etc/php/*/fpm/php.ini

Is there an even shorter, more minimalist way to do the same two-objects change in a single command? Something that will still be safe, and version agnostic.

As a web server I use Nginx.

  • Use vim (or other editor) and add two 00 twice then save maybe? BTW, php.ini is in a different location on some servers. You can also set these values per <VirtualHost> and per <Directory> in Apache which is a more secure way to limit uploads instead of a server wide answer. – Tigger Jan 6 '18 at 1:21
  • Sounds like an XY problem - instead of changing it in every single file, create an included file with the default settings, then have that file included in all such files (It looks like you're creating one per shared hosting site). Presto, problem solved and future-proofed. – Shadur Jan 6 '18 at 16:43
  • I have only one environment with one web server on it. The upload size of 200 megabytes is just fine for all my sites so I've no problem with 1 fixed upload value for all sites (nice you brought this point up!). I just think 2M default is very miser and lame and 200M is way more comfortable for many users. Loading an external conf file seems to me redundant for that specific task and the simplest approach would be just to change the default value. – Arcticooling Jan 6 '18 at 19:40
1

A kludgy way to do both parameters at once is

sed -i 's/^(\w*[td]_max_\w*size\s*=\s*).M/\1200M/' filename(s)

First of all, according to this List of php.ini directives, there are only three keywords that contain _max_ and end with size:

  • imlib2.font_cache_max_size
  • post_max_size
  • upload_max_filesize

Note that the letter immediately before the _max_ is different in each case:

  • imlib2.font_cache_max_sizee
  • post_max_size       → t
  • upload_max_filesize    → d

so, by searching for [td]_max_, we can eliminate the first one.

The command in detail:

  • ^ of course constrains the regular expression to match occurrences at the beginning of the line.  If you want to match strings that are not at the beginning of the line, remove the ^.  Note: unless you add something to specify otherwise, this will cause the command to change lines that are commented out, which might be confusing.
  • \(…\) identifies a group of characters.  We will use it to match everything up to (but not including) the parameter value (i.e., the number after the =).
  • \w*[td]_max_\w*size matches
    • any number of “word” (alphanumeric) characters,
    • a t or a d,
    • _max_,
    • any number (possibly zero) of word characters, and
    • size.
  • \s*=\s* matches any number of space characters (regular space or tab), an =, and any number of space characters again.  (You don’t need to say \=.)

    This is the end of the group, marked by \).

  • As you know, .M matches any character followed by M.  (You’re using this already.)
  • \1200M looks confusing, but it’s just
    • \1, which replaces the characters in the \(…\) group with themselves,
    • followed by 200M, which is what you want to replace the nM number with.

You don’t need the g unless you expect to match multiple strings on the same line (which is impossible if you anchor the regular expression to the beginning of the line with ^).

Replace filename(s) with your /etc/php/*/fpm/php.ini glob, or any other filename or list of filenames.

Before doing the above, you should run

sed -n 's/^(\w*[td]_max_\w*size\s*=\s*).M/\1200M/p' filename(s)
which will show what the first command will do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.