PHP - Unable to change memory_limit on php-fpm - php

It might seem a stupid question, but I'm already trying since one hour to figure it out without success.
As stated in title, I'm actually unable to change memory_limit directive in PHP 7.1.15 but only for php-fpm. In fact if I execute php -i | grep memory_limit in terminal I can see the correct value memory_limit => -1 => -1.
What I tried so far:
Setting memory_limit in php.ini
Using ini_set('memory_limit', -1); directly in PHP script
I also tried to disable all loaded extensions cause I thought maybe one of them is overwriting that setting, but didn't work.
Also consider that edited php.ini file is the correct one since I was able to change max_execution_time without any problem.
So how this code
ini_set('memory_limit', -1);
echo ini_get('memory_limit');
exit;
can return 128M as output?

To change the memory limit for PHP-FPM, add the following line to your php-fpm.conf file:
php_admin_value[memory_limit] = -1

from fpm-config
; php_value/php_flag - you can set classic ini defines which can
; be overwritten from PHP call 'ini_set'.
; php_admin_value/php_admin_flag - these directives won't be overwritten by
; PHP call 'ini_set'
You must chose better solution for you. I'm prefer set lower memory limit in global config, and in places (wo I can't rewrite better) I'm add ini_set('memory_limit', xxx);

Related

Comment résoudre ce problème lié à l'installation de glpi sur wampserver? [duplicate]

How to increase transaction timeout? I want to upload videos, but large size of videos not uploaded?
It throws error The process *** exceeded the timeout of 60 seconds.
You need to change some settings in your php.ini:
upload_max_filesize = 2M
;or whatever size you want
max_execution_time = 60
; also, higher if you must - sets the maximum time in seconds
Where your PHP.ini is located depends on your system environment. For more information: http://php.net/manual/en/ini.list.php
You should be able to do during runtime too using
set_time_limit(100);
http://php.net/manual/en/function.set-time-limit.php
or in your vhost-config
php_admin_value max_execution_time 10000
Having a global execution time limit that is LOW is mostly a good idea for performance-reasons on not-so-reliable applications. So you might want to only allow those scripts to run longer that absolutely have to.
p.s.: Dont forget about post_max_size and upload_max_filesize (like the first answer told allready)
To complete the answer of Hannes.
You need to change some setting in your php.ini:
upload_max_filesize = 2M
;or whatever size you want
max_execution_time = 60
; also, higher if you must
If someone want put in unlimited (I don't know why but if you want), you can set the time to 0:
You need to change some setting in your php.ini:
upload_max_filesize = 0
max_execution_time = 0
And if you don't know where is your php.ini. You can do a file "name.php" in your server and put:
<?php phpinfo(); ?>
And on your website, you can see the config of your php.ini and it's marked where is it.
Edit on 9 January 2015:
If you can't access your php.ini, you have two more options.
You can set this line directly in your "name.php" file but I don't find for upload_max_filesize for this option:
set_time_limit(0);
Or in ".htaccess"
php_value upload_max_filesize 0
php_value max_execution_time 0
if what you need to do is specific only for 1 or 2 pages i suggest to use set_time_limit so it did not affect the whole application.
set_time_limit(some_values);
but ofcourse these 2 values (post_max_size & upload_max_filesize) are subject to investigate.
you either can set it via ini_set function
ini_set('post_max_size','20M');
ini_set('upload_max_filesize','2M');
or directly in php.ini file like response above by Hannes, or even set it iin .htaccess like below
php_value upload_max_filesize 2M
php_value post_max_size 20M
If you happen to be using Microsoft IIS server, in addition to the php.ini settings mentioned by others, you may need to increase the execution timeout settings for the PHP FastCGI application in the IIS Server Manager:
Step 1) Open the IIS Server Manager (usually under Server Manager in the Start Menu, then Tools / Internet Information Services (IIS) Manager).
Step 2) Click on the main connection (not specific to any particular domain).
Step 3) Under the IIS section, find FastCGI Settings (shown below).
Step 4) Therein, right-click the PHP application and select Edit....
Step 5) Check the timeouts (shown below).
In my case, the default timeouts here were 70 and 90 seconds; the former of which was causing a 500 Internal Server Error on PHP scripts that took longer than 70 seconds.
As an addition to above answers, you may use set_time_limit() function:
http://php.net/manual/en/function.set-time-limit.php
passing 0 as an argument will make your script run with no time limit.
If you cannot edit php.ini (on your server for example) you can attempt to change the php.ini parameters from within your php code. Try:
ini_set('max_execution_time', 'NUMBER OF SECONDS TO ALLOW BEFORE TIMEOUT');
If that doesn't work, try also setting 'set_time_limit' in the same way, beyond that I'd say your only option is to contact your host. These settings cannot be modified while in safe mode.
You had a typo: ini_set('max_input_time','200M') - value set needs to be an int, like ini_set('max_input_time','200')
I know you are specifically asking about the PHP timeout, but what no one else seems to have mentioned is that there can also be a timeout on the webserver and it can look very similar to the PHP timeout.
So if you have tried:
Increasing the timeout in php.ini by adding a line: max_execution_time = {number of seconds i.e. 60 for one minute}
Increasing the timeout in your script itself by adding: ini_set('max_execution_time','{number of seconds i.e. 60 for one minute}');
And you have checked with the phpinfo() function that max_execution_time has indeed be increased, then you might want to try adding this to .htaccess which will make sure Apache itself does not time out:
RewriteRule .* - [E=noabort:1]
RewriteRule .* - [E=noconntimeout:1]
More info here:
https://www.antropy.co.uk/blog/php-script-keeps-timing-out-despite-ini-set/
First check the php.ini file path by phpinfo(); and then changed PHP.INI params:
upload_max_filesize = 1000M
memory_limit = 1500M
post_max_size = 1500M
max_execution_time = 30
restarted Apache
set_time_limit(0); // safe_mode is off
ini_set('max_execution_time', 500); //500 seconds
Note: you can also use command to find php.ini in Linux
locate `php.ini`
Test if you are is safe mode - if not - set the time limit (Local Value) to what you want:
if(!ini_get('safe_mode')){
echo "safe mode off";
set_time_limit(180);// seconds
phpinfo();// see 'max_execution_time'
}
*You cannot set time limit this way if safe mode 'on'.
optional : if you set config about php.ini but still can't upload
-this is php function to check error code
$error_type = [];
$error_type[0] = "There is no error";
$error_type[1] = "The uploaded file exceeds the upload_max_filesize directive in php.ini.";
$error_type[2] = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.";
$error_type[3] = "The uploaded file was only partially uploaded.";
$error_type[4] = "No file was uploaded.";
//$error_type["5"] = "";
$error_type[6] = "Missing a temporary folder. Introduced in PHP 5.0.3.";
$error_type[7] = "Failed to write file to disk. Introduced in PHP 5.1.0.";
$error_type[8] = "A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help. Introduced in PHP 5.2.0.";
//------------------------------
//--> show msg error.
$status_code = $_FILES["uploadfile"]["error"];
if($status_code != 0){
echo $error_type[$status_code];
exit;
}
You can also set a max execution time in your .htaccess file:
php_value max_execution_time 180
To really increase the time limit i prefer to first get the current value. set_time_limit is not always increasing the time limit. If the current value (e.g. from php.ini or formerly set) is higher than the value used in current call of set_time_limit, it will decrease the time limit!
So what's with a small helper like this?
/**
* #param int $seconds Time in seconds
* #return bool
*/
function increase_time_limit(int $seconds): bool
{
return set_time_limit(max(
ini_get('max_execution_time'), $seconds
));
}
// somewhere else in your code
increase_time_limit(180);
See also: Get max_execution_time in PHP script

Trouble with ini_set("memory_limit") in php 5.3

I am attempting to use ini_set("memory_limit", "256M"); to set the memory limit to 256M for my script, but it doesn't seem to work.
echo ini_get("memory_limit");
gives me 128M which is the default.
Using ini_set to change the max execution time does when tested the same way.
Having already found this question I checked the suhosin.ini and php.ini files. Suhosin caps it at 2048M though php.ini limits that to 512.
I am not running into either of those caps, and there is another script in the same directory running with a memory limit of 512MB, so there isn't anything in the .htaccess. Is there somewhere else I should be checking?

PHP- cannot change max_execution_time in xampp

I've tried everything to change the max_execution_time of a php crawler script so that it can run an infinite amount of time.
I have changed the php.ini file setting max_execution_time to 0 or 100000000 but with no change
I've also tried setting it from the php script itself by using ini_set('max_execution_time', 0);
All php scripts throw the same error Fatal error: Maximum execution time of 3000 seconds exceeded, what could I be missing and how can I make sure there is no max execution time limit?
php script
<?php
ini_set('MAX_EXECUTION_TIME', -1);
error_reporting(E_ALL); // turn on all errors, warnings and notices for easier debugging
//ini_set('max_execution_time', 123456);
ini_set('max_input_time', -1);
ini_set('memory_limit', '512M');
set_time_limit(0);
date_default_timezone_set('Europe/London');
/*code which scrapes websites*/
?>
phpinfo()
max_execution_time 0 0
max_input_time -1 -1
Try turning off safe mode in php and then try the below code
if( !ini_get('safe_mode') ){
set_time_limit(0); //this won't work if safe_mode is enabled.
}
This should allow you to run the script for infinite time.
In Apache you can change maximum execution time by .htaccess with this line
php_value max_execution_time 200
set_time_limit() php manual ref.
You shouldn't let your crawler run under apache, it's better to run it stand-alone via cli as part of a Gearman setup.
That way it won't hog your web server and it can run as long as you want. You can find many bindings for Gearman that you can use, including PHP of course.
use
set_time_limit(0);
at the top of the script
In WAMP there is three PHP.ini files so you might find 3 in xampp also, so just search for it with find and replace max_execution_time to what you are setting. But you must keep something small not too large as for speedy the app you running.
You could also try setting ignore_user_abort(TRUE); in your script as it might be the browser timing out rather than the script.
From the php.net manual
<?php
// Ignore user aborts and allow the script
// to run forever
ignore_user_abort(true);
set_time_limit(0);
See here for more info
http://www.php.net/manual/en/function.ignore-user-abort.php
If you are on windows, and this is a CLI run script maybe read this.
check phpinfo() from a temp script and search for max_execution_time. make sure that it has same value what you are setting. default should be 30 seconds. try to change it to a couple of different values and restart apache then check the value in phpinfo() to confirm.
if when you change the value it is reflected properly in the phpinfo() it means that there is some code in your script which is changing this value. search for two things in your code:
search your code for ini_set() and check if it is change max_execution_time
search for set_time_limit()
these functions can change maximum time limit of execution from script. otherwise you should check .htaccess from where this value may be set. but this will effect phpinfo() also.
I found the following in the xampp documentation. Maybe you are trying to edit the wrong php.ini file?
Why have changes in my php.ini no effect?
Since XAMPP 1.7.1 the "php.ini" is only in the directory "\xampp\php". Till XAMPP 1.7.0 is was in the directory "\xampp\apache\bin".
If a change in the "php.ini" have no effect, it's possible PHP is still using an other one. You can verify this with phpinfo(). Go to the URI localhost/xampp/phpinfo.php and search for "Loaded Configuration File". This value shows you the "php.ini" PHP is really using.
Info:
After changing the "php.ini" you have to restart Apache, thus Apache/PHP can read the new settings.
what you are doing is just setting the max_execution_time to whatever inside your page.
you can't change this using ini_set. you can change the memory_limit only.
see more details here...
from the php official site.
if you want them to be changed, change in php.ini.
You have to change both of these in you php.ini ( and check if that's the right php.ini by finding the location in phpinfo(); output! )
max_execution_time = 0
max_input_time = 0
And after that check if some php file is not overwriting those variables locally.
open php.ini notepad file and search or find upload_max_filesize = 1000M and you should change on Post_max_filesize = 1000M then restart your xampp and refresh the local phpmyadmin..

Which php.ini setting controls the permissions of the php_ini function? [duplicate]

I had this working before :
echo ini_get("memory_limit")."\n";
ini_set("memory_limit","256M");
echo ini_get("memory_limit")."\n";
That would input this :
32M
256M
on a php script executed by command line.
I updated from 5.2 to 5.3, and from now, this directive is not working at all : this gives me :
32M
32M
and then make my script fail with a fatal Error...
I checked the php documentation, and googled it, and I didn't find anywhere that "memory_limit" had been deprecated.
Does anyone have a solution?
Most likely your sushosin updated, which changed the default of suhosin.memory_limit from disabled to 0 (which won't allow any updates to memory_limit).
On Debian, change /etc/php5/conf.d/suhosin.ini
;suhosin.memory_limit = 0
to
suhosin.memory_limit = 2G
Or whichever value you are comfortable with. You can find the changelog of Sushosin at http://www.hardened-php.net/hphp/changelog.html, which says:
Changed the way the memory_limit protection is implemented
If you have the suhosin extension enabled, it can prevent scripts from setting the memory limit beyond what it started with or some defined cap.
http://www.hardened-php.net/suhosin/configuration.html#suhosin.memory_limit
Here's a list of things that are worth checking:
Is Suhosin installed?
How to check whether Suhosin is installed?
ini_set
The format is important
ini_set('memory_limit', '512'); // DIDN'T WORK
ini_set('memory_limit', '512MB'); // DIDN'T WORK
ini_set('memory_limit', '512M'); // OK - 512MB
ini_set('memory_limit', 512000000); // OK - 512MB
When an integer is used, the value is measured in bytes. Shorthand
notation, as described in this FAQ, may also be used.
http://php.net/manual/en/ini.core.php#ini.memory-limit
Has php_admin_value been used in .htaccess or virtualhost files?
Sets the value of the specified directive. This can not be used in
.htaccess files. Any directive type set with php_admin_value can not
be overridden by .htaccess or ini_set(). To clear a previously set
value use none as the value.
http://php.net/manual/en/configuration.changes.php
Works for me, has nothing to do with PHP 5.3. Just like many such options it cannot be overriden via ini_set() when safe_mode is enabled. Check your updated php.ini (and better yet: change the memory_limit there too).
Ubuntu 10.04 comes with the Suhosin patch only, which does not give you configuration options. But you can install php5-suhosin to solve this:
apt-get update
apt-get install php5-suhosin
Now you can edit /etc/php5/conf.d/suhosin.ini and set:
suhosin.memory_limit = 1G
Then using ini_set will work in a script:
ini_set('memory_limit', '256M');
Let's do a test with 2 examples:
<?php
$memory = (int)ini_get("memory_limit"); // Display your current value in php.ini (for example: 64M)
echo "original memory: ".$memory."<br>";
ini_set('memory_limit','128M'); // Try to override the memory limit for this script
echo "new memory:".$memory;
}
// Will display:
// original memory: 64
// new memory: 64
?>
The above example doesn't work for overriding the memory_limit value.
But This will work:
<?php
$memory = (int)ini_get("memory_limit"); // get the current value
ini_set('memory_limit','128'); // override the value
echo "original memory: ".$memory."<br>"; // echo the original value
$new_memory = (int)ini_get("memory_limit"); // get the new value
echo "new memory: ".$new_memory; // echo the new value
// Will display:
// original memory: 64
// new memory: 128
?>
You have to place the ini_set('memory_limit','128M'); at the top of the file or at least before any echo.
As for me, suhosin wasn't the solution because it doesn't even appear in my phpinfo(), but this worked:
<?php
ini_set('memory_limit','2048M'); // set at the top of the file
(...)
?>

ini_set("memory_limit") in PHP 5.3.3 is not working at all

I had this working before :
echo ini_get("memory_limit")."\n";
ini_set("memory_limit","256M");
echo ini_get("memory_limit")."\n";
That would input this :
32M
256M
on a php script executed by command line.
I updated from 5.2 to 5.3, and from now, this directive is not working at all : this gives me :
32M
32M
and then make my script fail with a fatal Error...
I checked the php documentation, and googled it, and I didn't find anywhere that "memory_limit" had been deprecated.
Does anyone have a solution?
Most likely your sushosin updated, which changed the default of suhosin.memory_limit from disabled to 0 (which won't allow any updates to memory_limit).
On Debian, change /etc/php5/conf.d/suhosin.ini
;suhosin.memory_limit = 0
to
suhosin.memory_limit = 2G
Or whichever value you are comfortable with. You can find the changelog of Sushosin at http://www.hardened-php.net/hphp/changelog.html, which says:
Changed the way the memory_limit protection is implemented
If you have the suhosin extension enabled, it can prevent scripts from setting the memory limit beyond what it started with or some defined cap.
http://www.hardened-php.net/suhosin/configuration.html#suhosin.memory_limit
Here's a list of things that are worth checking:
Is Suhosin installed?
How to check whether Suhosin is installed?
ini_set
The format is important
ini_set('memory_limit', '512'); // DIDN'T WORK
ini_set('memory_limit', '512MB'); // DIDN'T WORK
ini_set('memory_limit', '512M'); // OK - 512MB
ini_set('memory_limit', 512000000); // OK - 512MB
When an integer is used, the value is measured in bytes. Shorthand
notation, as described in this FAQ, may also be used.
http://php.net/manual/en/ini.core.php#ini.memory-limit
Has php_admin_value been used in .htaccess or virtualhost files?
Sets the value of the specified directive. This can not be used in
.htaccess files. Any directive type set with php_admin_value can not
be overridden by .htaccess or ini_set(). To clear a previously set
value use none as the value.
http://php.net/manual/en/configuration.changes.php
Works for me, has nothing to do with PHP 5.3. Just like many such options it cannot be overriden via ini_set() when safe_mode is enabled. Check your updated php.ini (and better yet: change the memory_limit there too).
Ubuntu 10.04 comes with the Suhosin patch only, which does not give you configuration options. But you can install php5-suhosin to solve this:
apt-get update
apt-get install php5-suhosin
Now you can edit /etc/php5/conf.d/suhosin.ini and set:
suhosin.memory_limit = 1G
Then using ini_set will work in a script:
ini_set('memory_limit', '256M');
Let's do a test with 2 examples:
<?php
$memory = (int)ini_get("memory_limit"); // Display your current value in php.ini (for example: 64M)
echo "original memory: ".$memory."<br>";
ini_set('memory_limit','128M'); // Try to override the memory limit for this script
echo "new memory:".$memory;
}
// Will display:
// original memory: 64
// new memory: 64
?>
The above example doesn't work for overriding the memory_limit value.
But This will work:
<?php
$memory = (int)ini_get("memory_limit"); // get the current value
ini_set('memory_limit','128'); // override the value
echo "original memory: ".$memory."<br>"; // echo the original value
$new_memory = (int)ini_get("memory_limit"); // get the new value
echo "new memory: ".$new_memory; // echo the new value
// Will display:
// original memory: 64
// new memory: 128
?>
You have to place the ini_set('memory_limit','128M'); at the top of the file or at least before any echo.
As for me, suhosin wasn't the solution because it doesn't even appear in my phpinfo(), but this worked:
<?php
ini_set('memory_limit','2048M'); // set at the top of the file
(...)
?>

Categories