Docker Container: How to change PHP settings with inline replacements

October 8, 2019

When we created previously, Docker Containers with Apache and PHP, we set them up using the default settings of PHP.

However on many occasions we need to adjust those settings with ones more suitable to our projects requirements.
For example, we may realize that our project requires more memory, or that some of our scripts will run longer processes that need more than the default maximum 30 seconds time assigned to them.

In this post, we are going to find how we can change those settings with ones appropiate to our projects requirements.

There are several ways we could do this. But in this and the next post I am going to show two methods that can be easily implemented.

Change Php setting with inline replacements

The first method consists on using the sed command to edit and replace the settings on the php.ini file.

If you are not familiar with the sed command, what it does is to let perform "edit" operation on files without having to "open", "make the edits" and "save" them. That is particularly useful when running it on a terminal window, or, as in our case, on a Dockerfile.

So, lets say we want to change the memory_limit setting. We would invoke the sed command as follows:

sed -E -i -e 's/memory_limit = 128M/memory_limit = 512M/' /etc/php.ini

• The last part of the command, indicates the file we are editing, /etc/php.ini
• The middle section, which appears surrounded with single-quotes, indicates that we are doing a "substitution" of the memory_limit = 128M string, with memory_limit = 512M.

Then, in order to add this instruction on a Dockerfile we must use the RUN instruction as follows:

RUN sed -E -i -e 's/memory_limit = 128M/memory_limit = 512M/' /etc/php.ini

And, if we want to change several PHP settings, we can concatenate several sed commands:

RUN sed -E -i -e 's/max_execution_time = 30/max_execution_time = 120/' /etc/php.ini \
 && sed -E -i -e 's/memory_limit = 128M/memory_limit = 512M/' /etc/php.ini \
 && sed -E -i -e 's/post_max_size = 8M/post_max_size = 64M/' /etc/php.ini \
 && sed -E -i -e 's/upload_max_filesize = 2M/upload_max_filesize = 64M/' /etc/php.ini

Our updated Dockerfile

Then we have our updated Dockerfile with the settings replaced on the php.ini file:

FROM centos:7

# Install Apache
RUN yum -y update
RUN yum -y install httpd httpd-tools

# Install EPEL Repo
RUN rpm -Uvh \
 && rpm -Uvh

# Install PHP
RUN yum --enablerepo=remi-php73 -y install php php-bcmath php-cli php-common php-gd php-intl php-ldap php-mbstring \
    php-mysqlnd php-pear php-soap php-xml php-xmlrpc php-zip

# Update Apache Configuration
RUN sed -E -i -e '/<Directory "\/var\/www\/html">/,/<\/Directory>/s/AllowOverride None/AllowOverride All/' /etc/httpd/conf/httpd.conf
RUN sed -E -i -e 's/DirectoryIndex (.*)$/DirectoryIndex index.php \1/g' /etc/httpd/conf/httpd.conf

# Update Php Settings
RUN sed -E -i -e 's/max_execution_time = 30/max_execution_time = 120/' /etc/php.ini \
 && sed -E -i -e 's/memory_limit = 128M/memory_limit = 512M/' /etc/php.ini \
 && sed -E -i -e 's/post_max_size = 8M/post_max_size = 64M/' /etc/php.ini \
 && sed -E -i -e 's/upload_max_filesize = 2M/upload_max_filesize = 64M/' /etc/php.ini


# Start Apache
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]

You can also download this Dockerfile here

We can now create the Docker Container.

docker build -t image_apache .

docker run -tid -p 4000:80 --name=container_apache -v /path_to/my_website:/var/www/html image_apache

Don't forget to replace /path_to/my_website with the actual location of your site's root folder before running the docker run command.