Local installation of PHP vs. PHP in Docker

Local installation of PHP vs. PHP in Docker
October 30, 2020
PHP is a server-side scripting language designed for web development, but which can also be used as a general-purpose programming language. PHP can be added to straight HTML or it can be used with a variety of templating engines and web frameworks. PHP code is usually processed by an interpreter, which is either implemented as a native module on the webserver or as a common gateway interface (CGI).

Both types of installation look the same, but the difference is in the localisation of ini files. The standard path to ini files for local PHP is /usr/local/etc/php. The Docker installation uses the same path, but you can find two files, php.ini-development and php.ini-production, and a folder called conf.d. If you add phpinfo() into both systems, you can see this:

And that is the problem. Unfortunately, PHP is looking for the default php.ini file at the default path. Files named ini-development or ini-production are not accepted. Other ini files can be loaded from a different location: /usr/local/etc/php/conf.d.

How to solve it

1/ If you want to upload files with default values, you can rename one of the files in /usr/local/etc/php/. In Dockerfile like this:

{% c-block language="markdown" %}
FROM PHP:apache
           WORKDIR /var/www/html
           COPY . . #copy your awesome app to workdir
           RUN mv /usr/local/etc/php/php.ini-development /usr/local/etc/php/php.ini

{% c-block-end %}

2) If you want to upload some files with default values and some with custom values you can use a command echo after renaming the files as in solution no. 1 like this:

{% c-block language="markdown" %}
FROM PHP:apache
           WORKDIR /var/www/html
           COPY . . #copy your awesome app to workdir
           RUN mv /usr/local/etc/php/php.ini-development /usr/local/etc/php/php.ini \
                       && echo “my_values” > /usr/local/etc/php/conf.d/my_php_ini.ini
{% c-block-end %}

3) For using only files with custom values, we can use echo or multiple echo commands, or we can use our own ini file in repo and copy to the correct path. If we are using pipeline from GitLab, we can use the echo environment (GitLab ENV) for an ini file to path 
           /usr/local/etc/php/conf.d/

{% c-block language="markdown" %}
FROM PHP:apache
           WORKDIR /var/www/html
           COPY . . #copy your awesome app to workdir
           COPY “path_to_your_ini_file(s)” /usrlocal/etc/php/conf.d
{% c-block-end %}

or:

{% c-block language="markdown" %}
RUN echo “my_values” > /usr/local/etc/php/conf.d/my_php_ini.ini (creating file)
           RUN echo “my_other_values” >> /usr/local/etc/php/conf.d/my_php_ini.ini (add_lastline_into_file)
{% c-block-end %}

See you soon for the next episode of our DevOps series.

Share:
Pavel is a trained DevOps guru and keen SysAdmin in charge of the application lifecycle, continuous integration, continuous deployment, and continuous delivery for both customer and internal SABO projects. He trains software developers in our team and coordinates all DevOps activities. He always has some puns up his sleeve, unpredictably switches to Polish, and likes to cook and bake.

Article collaborators

SABO Newsletter icon

SABO NEWSLETTER

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

About SABO Mobile IT

We focus on developing specialized software for our customers in the automotive, supplier, medical and high-tech industries in Germany and other European countries. We connect systems, data and users and generate added value for our customers with products that are intuitive to use.
Learn more about sabo