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.
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
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
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
Then we have our updated Dockerfile with the settings replaced on the php.ini file:
You can also download this Dockerfile here
FROM centos:7 # Install Apache RUN yum -y update RUN yum -y install httpd httpd-tools # Install EPEL Repo RUN rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm \ && rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-7.rpm # 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 EXPOSE 80 # Start Apache CMD ["/usr/sbin/httpd","-D","FOREGROUND"]
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.