Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override php cli configuration with custom php.ini

I'm struggling to override the php cli configuration with my custom php.ini

My custom php.ini

max_execution_time = 90
memory_limit = 256M
safe_mode = Off

Running php cli

php -c /home/env/php.ini -r 'phpinfo();' | grep 'memory_limit'

outputs

memory_limit => 256M => 256M

However the custom php.ini doesn't seem to override max_execution_time nor safe_mode as it outputs 0 and On rather than 90 and Off.

Running this simple script

#!/usr/bin/php -c /home/env/php.ini
<?php echo 'memory_limit: ' . ini_get('memory_limit');

outputs the default cli configuration (128M) rather than 256M as expected.

Running this simple script

#!/usr/bin/php -d max_execution_time=90
<?php echo 'max_execution_time: ' . ini_get('max_execution_time');

outputs 90 as expected.

So far I've managed to override only one single configuration directive at runtime, so any help much appreciated how to override multiple configuration directives.

Edit:

php -a
php > parse_ini_file('/home/env/php.ini');

outputs no errors, so I guess my custom php.ini is ok. I'd though just discover that executing

#!/usr/bin/php -c /home/env/php.ini
<?php echo 'memory_limit: ' . ini_get('memory_limit'); phpinfo();

says Loaded Configuration File => (none). That seems to be the issue. PHP CLI doesn't seem to load my custom php.ini file even though the path is correct and syntax seems fine too.

Solution:

It doesn't seem possible to override multiple configuration directive at runtime with shebang, at least for me. But removing shebang and executing the following command solved the issue for me.

php -d max_execution_time=90 -d memory_limit=256M -d safe_mode=Off -f test.php
like image 287
longtimejones Avatar asked Jul 02 '14 00:07

longtimejones


People also ask

Where do I put PHP ini file?

ini file is the default configuration file for running applications that require PHP. It is used to control variables such as upload sizes, file timeouts, and resource limits. This file is located on your server in the /public_html folder.

How do I know which PHP ini is being used?

Check php. ini in CLI (Command Line Interface): To know about php. ini, simply run on CLI. It look for Loaded Configuration File in output for the location of php.


1 Answers

I was trying to set max_execution_time by CLI too and I was unable to do it.

From documentation I understand it is hard-coded and cannot be changed in CLI enviroment.

Anyway, I found the timeout command useful :

timeout 60 php -d memory_limit=1024M script.php

(run script for 60 seconds max).

like image 154
adrianTNT Avatar answered Oct 22 '22 02:10

adrianTNT