Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open_basedir restriction when running script from command line

I'm learning how to use the command line by requirement for a recent project. I have a script that will be run during a cronjob, and am testing it by hitting it from the command line using Putty.

My script runs fine from the browser, but from the command line I get permission errors trying to include files or even check if they exist.

This only happens from the command line. This is what is in the console (I'm sure the correct script is executing, and all it does for now is check if a required file exists):

Here is the line in the terminal that executes the command:

secure:~/www/private/cron$ php -f ./update.php

Here is the output:

Warning: file_exists(): open_basedir restriction in effect. File(/usr/local/apache/htdocs/example.com/private/modules/real_estate/config/condos.php) is not within the allowed path(s): (.:/tmp/:/var/tmp/:/usr/local/apache/htdocs/example.com/images/:/home/example/www/images/) in /usr/local/apache/htdocs/example.com/private/cron/update.php on line 43

I do not get this error running the script in a browser, which is confusing to me - but as I mentioned I am new to using the command line. I've tried every way I can think of to reference the file from update.php, and tried changing the include path, but nothing seems to work. Either I get the permission error or it can't find the file.

Does anyone know why there is a difference between executing the script in a browser VS the command line, and what I can do to resolve this error? I'm happy to provide more information as requested.


More information:

This is my CLI output from php --ini:

Configuration File (php.ini) Path: /usr/local/lib
Loaded Configuration File: /usr/local/Zend/etc/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed: (none)

This is the Loaded Configuration File entry in phpinfo(), from the browser:

/usr/local/Zend/etc/php.ini

So Loaded Configuration File appears to be the same in both environments.

like image 369
Wesley Murch Avatar asked Apr 18 '26 01:04

Wesley Murch


2 Answers

You may want to run two different php.ini files for CLI vs Apache instances.

One way to do that is to copy your current php.ini file to another directory - say into "/usr/local/Zend/etc/php_cli/php.ini" or a more convenient directory. Then, change settings in "/usr/local/Zend/etc/php_cli/php.ini" to better suit a command line environment (expand open_basedir a bit, log file paths, and other CLI-specific settings). Then, when executing CLI scripts, include the -c "/usr/local/Zend/etc/php_cli" switch to have CLI reference that ini file.

like image 114
bob-the-destroyer Avatar answered Apr 19 '26 14:04

bob-the-destroyer


There is often a difference between the php.ini for your server and the php.ini for the command line. This means that whatever you're seeing in the browser is almost certainly not what you'll see when you enter php -r 'phpinfo();' on the command line (I mean, the formatting is different and it is less colorful)

You'll want to look for the other php.ini, and then you'll want to commend out open_basedir = <something>

like image 32
cwallenpoole Avatar answered Apr 19 '26 14:04

cwallenpoole



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!