skip to Main Content

I am running out of memory when using command line when logged in over SSH as a user mike.

When I run the script via browser or via command line as root, its all good and stalls at 1000M as expected.

The test script I am using is:

<pre>
<?php 
echo //phpinfo();
error_reporting(E_ALL);
ini_set('display_errors', 'on');

for ($i=1; $i<1500; $i+=50) {
  $a = loadmem($i);
  echo "You have allocated ". $i . "M (". memory_get_usage() . ") memory in this php script" . "<br />";
  unset($a);
}

function loadmem($howmuchmeg) {
  $a = str_repeat("0", $howmuchmeg * 1024 * 1024); // alocating 10 chars times million chars  
  return $a;
}
?>
</pre>

When I run as user mike the script errors at around 200M.

I am using php-fpm and tried both PHP 5.6 and 7.2 and tried both memory_limit settings as 1000M.

When I run this as mike:

php -i | grep memory_limit

I get:

memory_limit => 1000M => 1000M

I cannot for the life of me see where it’s getting this 200M limit from.

Is there any reason why a user account would restrict memory usage over root?

2

Answers


  1. The ini files used for your server(browser) and the cli are different.
    You can either specify a configuration file using -c or get the location of php’s cli configuration file by
    $ php -i |grep php.ini

    Login or Signup to reply.
  2. It can be file permission issue on the php.ini file.

    If you find that memory limits differ between root and other users
    when PHP scripts are run from the command line, there may be an issue
    with your php.ini or your script. To verify that it isn’t your script,
    try this:

    $ echo "<? var_dump(ini_get('memory_limit')); ?>" >> memtest.php
    $ php -f memtest.php
    string(3) "8M"
    $ su -
    # php -f memtest.php
    string(3) "64M"
    

    If you get the same two values from both users, there’s probably a
    problem with your script. Make sure that there’s no ini_set()
    functions in your script that are overriding the php.ini file.

    However, if you get results like the ones above, check the permissions
    on /etc/php.ini:

    # ls -al /etc/php.ini
    -rw-------  1 root root 27 Jun  6 18:39 /etc/php.ini
    

    As you can see, php.ini is only readable to root, which prevents PHP’s
    command line parser from accessing your custom memory_limit directive
    in the php.ini. PHP’s general default is 8M for a memory limit if
    nothing is specified anywhere else, and that’s why normal users cannot
    get the higher memory limit that’s set in your php.ini file.

    Simply set the permissions on the file to 644 and you should be set to
    go:

    # chmod 644 /etc/php.ini
    # ls -al /etc/php.ini
    -rw-r--r--  1 root root 45022 Jun  6 23:00 /etc/php.ini
    

    Source: https://major.io/2007/06/14/php-cli-memory-limit-is-different-between-users-and-root/

    Because you run through with apache (I think) than maybe that user can’t access the php.ini file. Also please note, for cli and for apache (script run) there’s two different ini files. Also some distributions splited up the ini files, in those case the last variable set will be applied (if the same value was set in multiple files which are loaded).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search