Using custom php.ini file with php CLI

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
fi
 
# 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 … 🙂