Using custom php.ini file with php CLI

Featured Image ©

Considering that I have never been real Linux user before, there was few things that I had to learn to successfully handle my code on server through SSH.

One of these things is forcing php CLI to use my custom php.ini file on server.

Let me start this story from beginning …

Situation was that we took managed dedicated server for my Symfony2 project installation to be used as production server. I have no root access there (it’s level 2 managed server), but I always have possibility to ask support to enable me some things that I need inside my project’s account.

Of course, during the testing server configuration with my project I asked support to enable local php.ini file inside my account, so I can change setting as necessary without every time sending mail to support and waiting their response.

So, I got my local php.ini in which I first needed to remove shell_exec function from disabled functions – used in my custom command for handling database backups with cron job (independently and slightly different from default backups handling in control panel)

Yeah, I removed the function from php.ini and made custom command for back up and restore my data using php console command (Symfony2 shell).

Then I noticed that shell_exec is not working from php CLI. It’s still disabled ….
When I add shell_exec function in controller and run the code via web browser, it is working, but within SSH: php console backup create not working and it says that “shell_exec function is disabled for security reasons”…

Luckily, my colleague @Tomas Novoselic was near and thanks to him that this agony was not took too long. (He is hardcore Linux user … :-))

If you want to force specific php.ini file to be used with Php CLI, you just have to add path to php.ini file that you want to use inside command, so instead of:

$ php console backup create

you should use something like this:

php -c /home/divineof/etc/php.ini console backup create

… and that solved my problem…

In meantime, I found one more elegant solution, considering that I am using bash shell with SSH:
Inside root installation of my hosting account, there is hidden file called ‘.bashrc’, so I opened it and entered inside:

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
# User specific aliases and functions

alias php='php -c /home/divineof/etc/php.ini'

After defining alias for php command, I am able to write again like this:

$ php console backup create

… and it works now!

That’s all from me for today … 🙂


  1. If you don’t have possibility to modify global php.ini file (f. ex. shared hosting) and you can’t just pass -c flag or use bash alias (try to run composer with its post-update commands with custom php.ini in all cases) then there is even more elegant solution:

    export PHPRC=/home/you

    and place /home/you/php.ini .
    You can add this variable to bash_profile and it will work everytime.

  2. You can make it easier. Just create file /etc/php/php-cli.ini and php automatic read php-cli.ini

    “If php-SAPI.ini exists (where SAPI is the SAPI in use, so, for example, php-cli.ini or php-apache.ini), it is used instead of php.ini. The SAPI name can be determined with php_sapi_name().”

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.