{"id":7,"date":"2025-02-10T06:30:23","date_gmt":"2025-02-10T06:30:23","guid":{"rendered":"https:\/\/notes.cp2.techshu.in\/?p=7"},"modified":"2025-02-10T06:30:23","modified_gmt":"2025-02-10T06:30:23","slug":"php-fpm-configuration","status":"publish","type":"post","link":"https:\/\/notes.cp2.techshu.in\/?p=7","title":{"rendered":"PHP-FPM Configuration"},"content":{"rendered":"\n<p>Thanks for the clarification! Since the <strong>database server is separate<\/strong>, we can <strong>allocate more RAM to PHP-FPM<\/strong> because MySQL\/PostgreSQL isn\u2019t consuming memory on this server.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\ud83d\udd39 Adjusted PHP-FPM Configuration for 16GB RAM &amp; 4 Cores<\/strong><\/h2>\n\n\n\n<p>Since the database is on a different server, we assume <strong>PHP-FPM, Apache\/Nginx, Redis, and OS services<\/strong> as the main consumers of memory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1\ufe0f\u20e3 Determine <code>pm.max_children<\/code><\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Formula:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>pm.max_children = (Total Available RAM - Reserved RAM) \/ Average PHP Process Size\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Available RAM<\/strong>: 16GB<\/li>\n\n\n\n<li><strong>Reserve for OS, Webserver, Cache, and Other Services<\/strong>: ~3GB<\/li>\n\n\n\n<li><strong>PHP Process Size<\/strong>: ~100MB (Check with <code>ps -ylC php-fpm8.2 --sort:rss | awk '{sum+=$8} END {print sum\/NR\/1024 \" MB\"}'<\/code>)<\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udca1 <strong>Calculation:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(16GB - 3GB) \/ 100MB = 130\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49 <strong>Recommended: <code>pm.max_children = 120<\/code> (keeping buffer)<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2\ufe0f\u20e3 Set <code>pm.start_servers<\/code><\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Formula:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>pm.start_servers = Total CPU Cores * 2\n<\/code><\/pre>\n\n\n\n<p>For <strong>4 cores<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pm.start_servers = 4 * 2 = 8\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49 If traffic is high, increase to <strong>10-12<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3\ufe0f\u20e3 Set <code>pm.min_spare_servers<\/code> &amp; <code>pm.max_spare_servers<\/code><\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Formula:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>pm.min_spare_servers = pm.start_servers \/ 2\npm.max_spare_servers = pm.start_servers * 2\n<\/code><\/pre>\n\n\n\n<p>For <strong>10 start servers<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pm.min_spare_servers = 5\npm.max_spare_servers = 20\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49 <strong>Recommended:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pm.min_spare_servers = 5\npm.max_spare_servers = 20\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4\ufe0f\u20e3 Set <code>pm.process_idle_timeout<\/code><\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>For consistent traffic:<\/strong> Keep at <strong>10s<\/strong> (default).<\/li>\n\n\n\n<li><strong>For fluctuating traffic:<\/strong> Increase to <strong>20-30s<\/strong> to avoid frequent process respawning.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\ud83d\udd25 Final PHP-FPM Configuration<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>pm = dynamic\npm.max_children = 120\npm.start_servers = 10\npm.min_spare_servers = 5\npm.max_spare_servers = 20\npm.process_idle_timeout = 10s\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\ud83d\udcca Performance Monitoring<\/strong><\/h2>\n\n\n\n<p>After applying the settings, restart PHP-FPM:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart php8.2-fpm\n<\/code><\/pre>\n\n\n\n<p>Monitor CPU\/RAM usage:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>htop\nfree -m\n<\/code><\/pre>\n\n\n\n<p>Check PHP-FPM process usage:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ps aux | grep php-fpm\n<\/code><\/pre>\n\n\n\n<p>Check logs for PHP-FPM activity:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo tail -f \/var\/log\/php8.2-fpm.log\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\ud83d\udd04 Alternative: Use <code>pm = ondemand<\/code> for Variable Load<\/strong><\/h2>\n\n\n\n<p>If your traffic is unpredictable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pm = ondemand\npm.max_children = 120\npm.process_idle_timeout = 20s\n<\/code><\/pre>\n\n\n\n<p>This ensures PHP processes <strong>only run when needed<\/strong>, saving memory.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83d\ude80 <strong>These settings should provide optimal performance for your setup! Try them and monitor for any needed tweaks. Let me know if you need further adjustments!<\/strong> \ud83c\udfaf<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Thanks for the clarification! Since the database server is separate, we can allocate more RAM to PHP-FPM because MySQL\/PostgreSQL isn\u2019t consuming memory on this server. \ud83d\udd39 Adjusted PHP-FPM Configuration for 16GB RAM &amp; 4 Cores Since the database is on a different server, we assume PHP-FPM, Apache\/Nginx, Redis, and OS services as the main consumers [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/notes.cp2.techshu.in\/index.php?rest_route=\/wp\/v2\/posts\/7","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/notes.cp2.techshu.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notes.cp2.techshu.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notes.cp2.techshu.in\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notes.cp2.techshu.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7"}],"version-history":[{"count":1,"href":"https:\/\/notes.cp2.techshu.in\/index.php?rest_route=\/wp\/v2\/posts\/7\/revisions"}],"predecessor-version":[{"id":9,"href":"https:\/\/notes.cp2.techshu.in\/index.php?rest_route=\/wp\/v2\/posts\/7\/revisions\/9"}],"wp:attachment":[{"href":"https:\/\/notes.cp2.techshu.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notes.cp2.techshu.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notes.cp2.techshu.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}