Modifying php.ini with a One-Liner (POST & Memory Size Increase)

Wait! What is php.ini? The php.ini file is the default configuration file for running applications that require PHP. It is used to control variables such as upload sizes, file timeouts, and resource limits.

This Perl One-Liner is useful when setting up Content management websites like WordPress or Drupal on Linux servers that rely on LAMP stacks.

I find the default php.ini configuration is lacking for handling file uploads and also the memory limit could use a boost. I therefore increase the following variables in php.ini:

  • memory_limit = PHP allows a default amount of memory to be used when processing a website. Sometimes, a website needs more than the default amount of memory. If that’s the case, you can increase it to fit the needs of your site.
  • post_max_size = is the maximum size for all POST body data. If you are uploading multiple files, the total file size has to fit within this limit.
  • upload_max_filesize = Is a maximum size only for files that are POSTed. Other types of POST body data are not subject to this limit.

Perl One-Liner

The one liner below figures out the location of your php.ini, backs up the file and performs the following sweet tweaks 🙂

  • Increases the memory_limit to 128 megabytes
  • Increases the post_max_size to 500 megabytes
  • Increases the upload_max_filesize to 100 megabytes
php --ini | perl -nle 'if (m~(?i)Configuration\ File:\s+(.*)~){ print qq~\x{27}$1\x{27}~; $_ = $1; s~cli(?=/php.ini)~apache2~; print qq~\x{27}${_}\x{27}~}' | xargs -n 1 -I {} sudo perl -i"*_$(date +%s)" -ple 's~(?<=memory_limit\s=\s)\-?\d+M?(?{$__ = q!128M!})|(?<=post_max_size\s=\s)\d+M(?{$__ = q!500M!})|(?<=upload_max_filesize\s=\s)\d+M(?{$__ = q!100M!})~$__~g;' {}

Leave a Reply

Your email address will not be published. Required fields are marked *