4

I'm running php as a shell script.

(I am not sure if "shell script" is correct. The file starts with #!/usr/bin/php.)

This works great. But the MongoDB class doesn't get loaded as the correct php.ini file (having extension=mongo.so) is not used.

How do I make it use that php.ini file?

I already tried #!/usr/bin/php -c /usr/local/lib/php.ini

But I still get the same error - Fatal error: Class 'Mongo' not found

What can be done?

2
  • Please specify your server OS. (and if ubuntu, check if /etc/php5/cli/php.ini exists - that's the one to edit) Commented Jan 2, 2011 at 15:26
  • 1
    The output of php -i on the command line (basically doing a phpinfo() call) will list exactly what extensions are loaded, where PHP is looking for them, etc... Commented Jan 2, 2011 at 16:07

6 Answers 6

4

Try putting php.ini in the same folder as the php binary. It seems to look there first.

I know this because I used a very powerful and useful command-line program called strace to show me what's really going on behind my back

$ strace -o strace.log php --version
$ grep php.ini strace.log

Strace digs out kernel (system) calls that your program makes and dumps the output into the file specified after -o

It's easy to use grep to search for occurrences of php.ini in this log. It's pretty obvious looking at the following typical response to see what is going on.

open("/usr/bin/php.ini", O_RDONLY)      = -1 ENOENT (No such file or directory)
open("/etc/php.ini", O_RDONLY)          = 3
lstat("/etc/php.ini", {st_mode=S_IFREG|0644, st_size=69105, ...}) = 0
Sign up to request clarification or add additional context in comments.

Comments

4

Edit .bashrc located at your home directory and add this line:

alias php='php -c /path-to-custom/php.ini'

Comments

3

In this particular situation. I would

Most distributions already ship different versions of php.ini for Web Servers and CLI. Are there other reasons to add another php.ini configuration for script XYZ (in addition to normal configuration)?

1 Comment

Using extension_loaded() and then dl() did the trick. Thanks!
2

I came across this, because I had the same problem. Problem is there is more than one php.ini file used. The one used by Apache is located in

/etc/php5/apache2/php.ini

This is the one that is modified to run MongoDB with extension=mongo.so.

However, when running a cron job, or from the terminal, it loads a different ini file. You can find this by using the line

grep php.ini strace.log

It is mentioned by thomas-peter.

The path where it displays '=3' is the php.ini file loaded when running the engine from the terminal, this ini file will need "extension=mongo.so" placed in it as well.

Comments

1

This is a bit of a gotcha with PHP.

php -c /path/to/my/custom/php.ini works from the command line.

But try it in a script like this #!/usr/local/bin/php -c /path/to/my/custom/php.ini and your custom php won't load.

The Solution

Get rid of the space between -c and your path:

#!/usr/local/bin/php -c/path/to/my/custom/php.ini

Comments

1

The other option is to use a small sh wrapper, something like this below in a myapp.sh file. Don’t forget to chmod +x on the script to run it.

#!/usr/bin/env sh

SOMEVAR='Yea Baby!'

export SOMEVAR

php -c /path/to/my/custom/php.ini /path/to/my/old_script.php

With the added bonus of being able to set or override environment variables pre-run.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.