Building DevSecOps solutions using AWS, Terraform and Kubernetes

Programmatically update php.ini

  • 13th January 2023
Programmatically update php.ini

Introduction

Fun fact; people are still running PHP applications on a server they manually configured 5 years ago.

Let’s look at how to automate a small php.ini configuration change using a simple bash script. This will give you the first steps needed to start treating your servers like cattle rather than pets.

For the sake of this blog post I’ll assume you’re running a linux server.

Find your php.ini file

First you’ll need to find the php.ini file you want to change. If you’re stuck then visit my last blog post.

In this example I will assume you’re running PHP 8.1, using php-fpm and you’re not overriding the values in your php-fpm config, so the path is most likely here:

/etc/php/8.1/fpm/php.ini

Update php.ini by using a bash script

We will utilise the sed command to find and replace text.

So in the default file we know the max execution time is 30 seconds, and the memory limit is 128M.

Now we want to double those values to 60 and 256M. Let’s write a script that will find and replace those values.

  1. Create a file called configure-php.sh:
#!/bin/bash -ex
export PHP_INI_PATH=etc/php/8.1/fpm/php.ini
sed -i 's/max_execution_time = 30/max_execution_time = 60/' $PHP_INI_PATH
sed -i 's/memory_limit = 128M/memory_limit = 256M/' $PHP_INI_PATH
  1. Next, allow it to be executed:
chmod +x configure-php.sh
  1. Finally run it:
./configure-php.sh

Summary

And that’s it! Everytime you would like to change a configuration value just add to this script.

This will give you a consistent and reproducible way to configure PHP. And perhaps more importantly, this exact approach can be used to update a range of configuration files on your server.

The more we automate, the better we can do our job.

Rhuaridh

Please get in touch through my socials if you would like to ask any questions - I am always happy to speak tech!