Nginx and PHP-FPM Configuration and Optimizing Tips and Tricks - Comment Page: 3
I wrote before a guide Howto install Nginx/PHP-FPM on Fedora 29/28, CentOS/RHEL 7.5/6.10, but this guide is just installation guide and many cases Nginx and PHP-FPM basic configuration is good enough, but if you want to squeeze all the juice out of your VPS or web server / servers and do your maintenance work little bit easier, then this guide might be useful. These tips are based entirely on my own experience, so they may not be an absolute truth, and in some situations, a completely different configuration may work better. It's also good to remember leave resources for another services also if you run example, MySQL, PostgreSQL, MongoDB, Mail server, Name server and/or SSH server on same...
Hi there!
I’ve the following PHP file:
I’m using nginx/1.2.4 + PHP 5.3.18-1~dotdeb.0 (fpm-fcgi) with specified pools for each virtualhost by using http://www.sebdangerfield.me.uk/2012/05/nginx-and-php-fpm-bash-script-for-creating-new-vhosts-under-separate-fpm-pools/
My problem is, if I run that script nginx is loading it a bunch of time and finally stop. When I stop it running by pressing ESC it says something like an error occured. The picture isn’t that big so it should just load few seconds but it doesn’t… Any ideas?
Thank you in advance.
Regards
Jonas
Hi Jonas,
I fixed your comment little bit. Right/working syntax is <pre lang=”php”>some code</pre>.
And then your problem.
Is all other php scripts working normally?
Do you see any errors on nginx log files and/or php-fpm log files?
Hi,
I am shifting from Apache to Nginx. I am trying to find an alternative for suPHP. Configuring three different pools (site.conf, blog.conf & forum.conf ) as shown in this blog might solve my problem. Right ?
And then in my virtualhost (server tag) I can specify 127.0.0.1:9000, 127.0.0.1:9001, 127.0.0.1:9002 etc in (location – fastcgi_pass). Right ?
Hi Jigar,
Yes, you have fully understood the idea, PHP-FPM works as a replacement of suPHP and yes you can specify 127.0.0.1:9000, 127.0.0.1:9001, 127.0.0.1:9002 in fastcgi_pass.
Hi Guys,
I am getting this issue :
“2012/11/16 12:01:56 [error] 19393#0: *144134 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 123.456.789.2, server: domain.playlab.com.my, request: “GET /download.php?date_start=2012-10-15&date_end=2012-11-16 HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9000”, host: “domain.com.my”, referrer: “http://domain.playlab.com.my/report.php?page=15&date_start=2012-10-15&date_end=2012-11-16”
This issue was generated when getting a report. I was told that, I need to increase the processing time “script” Is there anyone who can advise me on this issue? If the above mention information is not enough please do let me know. Hope to hear from you guys soon. Appreciate it.
Thank you
Hi Nathaneal,
You can check more info from php-fpm logs (under /var/log/php-fpm/).
If problem is PHP max_execution_time time and you want to change it, then just open /etc/php.ini and change:
and then you can also check PHP memory_limit, if it’s too low.
This is little bit off topic, but next you should maybe look into that script why it take so long to execute, because later you will see more problems (maybe even more fatal problems) if you have more users who generate this report same time. Maybe same thing could be done faster and more efficiently, for example with a background processes, database optimization or query optimization, etc. :)
Hi Jr,
Thanks for the advice. I have just amended….will monitor to see the result.
Thanks again. :D
Best Regards
Nathaneal
Hi Nathaneal,
You are welcome and let me know, if you have more problems. :)
[…] hurt to  make a few small changes in the config files to improve the performance. Thanks to JR’s post for this […]
Hi Jr!
The tips and the comments where very clarifying for me.
I came to thank you for the time you spend posting and answering people.
I just made a few ajustments in my php-fpm and nginx based on what you tiped here.
Hi i have this php script that runs under this other web host fine but i setup nginx/php5-fpm and when i go to run it it runs fine but doesn’t finish will eventually get 502 bad gateway now it does make a lot of http requests any idea what i should change i’m using default configs for the most part on a vps with 256mem
Hi x,
Is some simple php script working normally?
This is an excellent article. . .I just spun up a VM for a client, and plan to use this article as a starting point to optimize his nginx installation. ;-)
Hi Eric,
Nice to hear that you like this article, thanks. :)
Yeah it works for the most accounts but if i select 40-50 accounts and click “skill” when it goes to equipt stuff by making massive amounts of http request it will eventully show the nginx error but if i select just 20-30 it works fine :/ but on my apache host if i select everything it also works fine and doesn’t error which i find odd
Hi again x,
You can try to increase max_execution_time and memory_limit on your /etc/php.ini file. Then remember restart your php-fpm.
Example:
Nice article, thanks for the tips!
Hi i compiled php5-fpm from source and there’s no php.ini but there is conf.d folders and i’m needing to enable curl/short open tags this is what phpinfo shows any idea’s been stumped with this for a few days then found this site.
Configuration File (php.ini) Path /etc/php5/cgi
Loaded Configuration File (none)
Scan this dir for additional .ini files /etc/php5/cgi/conf.d
Hi derp,
Could you do following (as root):
And post output of following command:
And also output of following commands:
We have an issue where RES memory being used by each php-fpm process increased in size as time went by. After a fresh restart of php-fpm the RES for each was 30-70Mb. After letting it go for 8 hours, each php-fpm process was at 1.1GB and still growing. A restart of php-fpm brings it down. However, it was causing serious perfomance issues. At this point we still have not been able to pinpoint the issue. We did lower the pm.max_requests to 1,000 from 10,000 to recycle php. It didn’t stop the RES Memory for each process from growing however.
Hi Andy,
Could you post output, if you run following php script on your server:
Could you tell more specific what you are running on this server? I just wonder, if it is something massive or small?
we have 2 servers, web and DB. APC 3.1.9, nginx/1.0.14, php 5.3.3 memcached. 24GB ram each and good dual hex core cpus. I believe the issue lies with APC and possibly magento. possible memory leak but not ruling out APC configuration. One interesting note. There is an APC php page that you can use to monitor APC. I can get the server to function properly when the value ‘Cached Files’ stops caching files.
right now, this value is stuck at 847. When the server breaks, this value will continually grow to thousands, which it should do. so, something amiss with APC most likely.
Array ( [0] => Core [1] => date [2] => ereg [3] => libxml [4] => openssl [5] => pcre [6] => sqlite3 [7] => zlib [8] => bz2 [9] => calendar [10] => ctype [11] => hash [12] => filter [13] => ftp [14] => gettext [15] => gmp [16] => session [17] => iconv [18] => Reflection [19] => standard [20] => shmop [21] => SPL [22] => SimpleXML [23] => sockets [24] => exif [25] => tokenizer [26] => xml [27] => cgi-fcgi [28] => apc [29] => curl [30] => dom [31] => fileinfo [32] => gd [33] => json [34] => mcrypt [35] => memcache [36] => mysql [37] => mysqli [38] => PDO [39] => pdo_mysql [40] => pdo_sqlite [41] => soap [42] => wddx [43] => xmlreader [44] => xmlrpc [45] => xmlwriter [46] => xsl [47] => zip [48] => ionCube Loader )
Hi Andy,
Thanks and sorry for long delay, I actually remember that I answered your comment once.
But yes I have seen very bad performance problems with all Magento setups. Magento use RAM ~100 Mb per page load and if you run heavy traffic site even with proper caching and optimizations you probably have performance problems.
I have also seen some problems with APC when there is “too much” files to cache. Do you use APC also as user cache? Or just for PHP file cache?
If you have pretty much static Magento (PHP files) then you can set following to APC config:
If apc.stat is off then APC will not check everytime are cached files changed or not. You get much better performance, but you have to remember restart web server or clear cache manually if you change some files.
I turned off APC and am using a filesystem cache. in Magento you define if you want to use APC as the caching module in the local.xml file. I removed all reference to APC, however the issue still persisted. With APC on, each php-fpm process that spawned increased in RES memory size. Even with no reference to APC. It was not until I completely turned of APC in apc.ini that this issue finally stopped. If Magento removes all references to APC, is it still possible that Magento and APC are still talking and don’t like each other? OR does this just sound like a pure APC problem?
Nice to hear that turning APC completely off solved your problem.
You can use APC as user cache, which you configure on Magento config files (local.xml) and PHP file cache for all .php files, which is used as default when you have APC running with php-fpm.
When you have turned APC off from apc.ini and removed all APC references from Magento config, then you should have php-fpm and Magento running totally without APC.
If you have this style problems with APC and still want some better caching than filesystem cache, then you can try xcache for .php file cache and Memcached as user cache. You have very powerful servers and lot of RAM, so that’s why it might wise to setup some caching using RAM.
thanks for this informational tutorial . it helped me to conrol my load around 1
Hi JR,
i am interested with your article, i have some problem with php5-fpm. U can see our top and config capture on the URL picture below :
-top :
http://grab.by/l0wo
-config php5-fpm :
pm.max_children = 16
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 8
Why php5-fpm service using high CPU load?is there any issue with my configuration? need your suggestion, please :)…
Many thanks.
Hi adm_junior,
Could you also post information which distro you use and what is your PHP version?
And then also some info what kind of site you are running, do you use some caching?
I have a 5 servers using load balancing.
One of them has 64 core cpu, 256GB memory. What’s your recommendation for this specification?
Thanks.
Hi Kevin,
Sounds awesome hardware. What specs you are looking for? Just nginx or also php-fpm on same machines?
Have to ask, that do you have some current setup and do you have some problems with it?
Sometimes it’s hard to say exactly best configuration, because it also depends on site(s) what you are running and of course traffic.
Your tips on tuning PHP-FPM really helped me out. I had my host recently migrated my service to a newer hardware and borked it. I had to rebuild a new LEMP stack once i got it all back together you really helped me resolve a zooming memory issue. Good write up. Hope you don;t mind a trackback. ;D