{"id":689,"date":"2010-03-16T21:14:57","date_gmt":"2010-03-16T13:14:57","guid":{"rendered":"http:\/\/pony.xin\/?p=689"},"modified":"2021-03-12T20:53:45","modified_gmt":"2021-03-12T12:53:45","slug":"nginx-php-fpm%e8%b0%83%e4%bc%98","status":"publish","type":"post","link":"https:\/\/lnmp.ivan.xin\/?p=689","title":{"rendered":"nginx php-fpm\u8c03\u4f18"},"content":{"rendered":"<p>\u4e8eNginx\u5bf9\u9ad8\u5e76\u53d1\u7684\u4f18\u826f\u6027\u80fd\uff0c\u6545\u914d\u4e86\u4e2aNginx+php-fpm\u6765\u8dd1\u5728\u7ebf\u4ee3\u7406\u7a0b\u5e8f\uff0c\u662f\u6309\u7167\u5f20\u5bb4\u6587\u7ae0\u914d\u7684\uff0c\u521a\u914d\u7f6e\u597d\u65f6\u8fd0\u884c\u6b63\u5e38\uff0c\u4f46\u8fd0\u884c\u4e00\u6bb5\u65f6\u95f4<br \/>\n\u540e\uff0c\u7f51\u7ad9\u6253\u5f00\u5f88\u6162\uff0c\u6253\u5f00\u7f51\u7ad9\u540e\uff0c\u5728\u8f93\u5165\u6846\u8f93\u5165\u8981\u8bbf\u95ee\u7684\u7f51\u7ad9\uff0c\u4e5f\u6162\u5f97\u4e0d\u884c\u3002\u5728\u7f51\u7ad9\u6253\u5f00\u6162\u65f6\uff0c\u5728SSH\u7ec8\u7aef\u4e0a\u8f93\u5165\u547d\u4ee4\u4e5f\u6162\uff0c\u6000\u7591\u662f\u673a\u623f\u7f51\u901f\u95ee\u9898\uff0c\u4f46\u5728ssh\u4e0a<br \/>\n \u8f93\u5165<\/p>\n<p>w3m www.example.com<\/p>\n<p>\u8fd9\u4e2a\u6253\u5f00\u4e5f\u6162\uff0c\u57fa\u672c\u53ef\u4ee5\u6392\u9664\u673a\u623f\u7684\u7f51\u901f\u95ee\u9898\u3002<\/p>\n<p>\u5f53\u6253\u5f00\u7f51\u7ad9\u6162\u65f6\uff0c\u628a\u670d\u52a1\u5668\u91cd\u542f\u540e\uff0c\u5c31\u4f1a\u5feb\u8d77\u6765\uff0c\u540e\u6765\u53d1\u73b0\uff0c\u7528<\/p>\n<p>\/usr\/local\/webserver\/php\/sbin\/php-fpm restart<\/p>\n<p>\u628afastcgi\u91cd\u542f\u4e0b\u4e5f\u4f1a\u5feb\u8d77\u6765\uff0c\u6700\u628a\u5b83\u52a0\u5165\u8ba1\u5212\u4efb\u52a1\uff0c\u6bcf\u5c0f\u65f6\u91cd\u542f\u4e0b\uff0c\u57fa\u672c\u4fdd\u8bc1\u7f51\u7ad9\u4e0d\u4f1a\u6162\uff0c\u4f46\u7ec8\u7a76\u4e0d\u662f\u529e\u6cd5\u3002<\/p>\n<p>\u67e5\u770b\u4e86nginx.log\u548cphp-fpm.log\uff0c\u6839\u636e\u91cc\u9762\u7684\u9519\u8bef\uff0c\u627e\u4e86\u4ee5\u4e0a\u8f6c\u8f7d\u7684\u51e0\u7bc7\u6587\u7ae0\uff0c\u603b\u7b97\u662f\u628a\u95ee\u9898\u89e3\u51b3\u4e86\uff0c\u4e3b\u8981\u4fee\u6539\u4e86\u4e24\u4e2a\u5730\u65b9<br \/>\n1\u3001<br \/>\n\u95ee\u9898\uff1a<br \/>\n\u53d1\u73b0\/usr\/local\/webserver\/php\/etc\/php-fpm.conf\u6587\u4ef6\u91cc\u5b9a\u4e49\u7684\u6253\u5f00\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u9650\u5236\u6570\u91cf\u662f<br \/>\n&lt;value name=\"rlimit_files\"&gt;51200&lt;\/value&gt;<br \/>\n\u4f46\u7528 \u547d\u4ee4ulimit -n\u67e5\u770b\uff0c\u53d1\u73b0\u53ea\u67091024<\/p>\n<p>\u6211\u5df2\u5728\/etc\/rc.local\u91cc\u6dfb\u52a0\u4e86<br \/>\nulimit -SHn 51200<\/p>\n<p>\u7adf\u7136\u6ca1\u751f\u6548<\/p>\n<p>\u89e3\u51b3\uff1a<br \/>\nvi&nbsp; \/etc\/security\/limits.conf<\/p>\n<p>\u6587\u4ef6\u6700\u540e\u52a0\u4e0a<br \/>\n*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; soft&nbsp;&nbsp;&nbsp; nofile&nbsp; 51200<br \/>\n*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hard&nbsp;&nbsp;&nbsp; nofile&nbsp; 51200<\/p>\n<p>2\u3001<br \/>\n\u95ee\u9898\uff1a<br \/>\n\u7528\u547d\u4ee4<\/p>\n<p>netstat -np | grep 127.0.0.1:9000 |wc -l<\/p>\n<p>\u53d1\u73b0\u53ea\u6709100\u591a<\/p>\n<p>\u89e3\u51b3\uff1a<br \/>\n\u6839\u636e\u670d\u52a1\u5668\u5185\u5b58\u60c5\u51b5\uff0c\u53ef\u4ee5\u628aPHP FastCGI\u5b50\u8fdb\u7a0b\u6570\u8c03\u5230100\u6216\u4ee5\u4e0a\uff0c\u57284G\u5185\u5b58\u7684\u670d\u52a1\u5668\u4e0a200\u5c31\u53ef\u4ee5<br \/>\n\u670d\u52a1\u5668\u4e0a\u5185\u5b58\u4e3a8G\uff0c\u6211\u628aPHP FastCGI\u5b50\u8fdb\u7a0b\u6570\u8c03\u6574\u5230300<\/p>\n<p>vi \/usr\/local\/webserver\/php\/etc\/php-fpm.conf<br \/>\n\u5c06max_children\u4fee\u6539\u4e3a300<br \/>\n&lt;value name=\"max_children\"&gt;300&lt;\/value&gt;<\/p>\n<p>\u91cd\u542f\u670d\u52a1\u5668\uff0c\u8fd9\u6837\uff0c\u7f51\u7ad9\u6253\u5f00\u901f\u5ea6\u5feb\uff0c\u800c\u4e14\u7a33\u5b9a\u4e86\u3002<\/p>\n<\/p>\n<p><span>When you running a highload website with <span>PHP-FPM<\/span> via FastCGI, the following tips may be<br \/>\nuseful to you : )<\/span> <br \/>\n<span>\u5982\u679c\u60a8\u9ad8\u8d1f\u8f7d\u7f51\u7ad9\u4f7f\u7528<span>PHP-FPM<\/span>\u7ba1<br \/>\n\u7406FastCGI\uff0c\u8fd9\u4e9b\u6280\u5de7\u4e5f\u8bb8\u5bf9\u60a8\u6709\u7528\uff1a)<\/span> <\/p>\n<p><span>1. Compile PHP\u2019s modules as less as possible,<br \/>\n the simple the best (fast);<\/span> <br \/>\n<span>1.\u5c3d\u91cf\u5c11\u5b89\u88c5PHP\u6a21\u5757\uff0c\u6700\u7b80\u5355\u662f\u6700\u597d\uff08\u5feb\uff09\u7684<\/span> <\/p>\n<p><span>2. Increas PHP FastCGI child number to 100<br \/>\nand even more. Sometime, 200 is OK! ( On 4GB memory server);<\/span> <br \/>\n<span>2.\u628a\u60a8\u7684PHP<br \/>\nFastCGI\u5b50\u8fdb\u7a0b\u6570\u8c03\u5230100\u6216\u4ee5\u4e0a\uff0c\u57284G\u5185\u5b58\u7684\u670d\u52a1\u5668\u4e0a200\u5c31\u53ef\u4ee5<\/span> <br \/>\n<span>\u6ce8\uff1a\u6211\u76841g\u6d4b\u8bd5\u673a\uff0c\u5f0064\u4e2a\u662f\u6700\u597d\u7684\uff0c\u5efa\u8bae\u4f7f\u7528\u538b\u529b\u6d4b\u8bd5\u83b7\u53d6\u6700\u4f73\u503c<\/span> <\/p>\n<p><span>3. Using SOCKET PHP FastCGI, and put into<br \/>\n\/dev\/shm on Linux;<\/span> <br \/>\n<span>3.\u4f7f\u7528socket\u8fde\u63a5FastCGI\uff0clinux\u64cd\u4f5c\u7cfb\u7edf\u53ef\u4ee5\u653e\u5728<br \/>\n\/dev\/shm\u4e2d<\/span> <br \/>\n<span>\u6ce8\uff1a\u5728<span>php-fpm<\/span>.cnf<br \/>\n\u91cc\u8bbe\u7f6e&lt;value<br \/>\nname=\"listen_address\"&gt;\/tmp\/nginx.socket&lt;\/value&gt;\u5c31\u53ef\u4ee5\u901a\u8fc7socket\u8fde\u63a5<br \/>\nFastCGI\u4e86\uff0c\/dev\/shm\u662f\u5185\u5b58\u6587\u4ef6\u7cfb\u7edf\uff0c\u653e\u5728\u5185\u5b58\u4e2d\u80af\u5b9a\u4f1a\u5feb\u4e86<\/span>&nbsp; \uff08\u8fd9\u4e00\u6b65\u5f85\u786e\u8ba4\uff09<\/p>\n<p><span>4. Increase Linux \u201cmax open files\u201d, using the<br \/>\n following command (must be root): <br \/>\n# echo \u2018ulimit -HSn 65536\u2032 &gt;&gt; \/etc\/profile <br \/>\n# echo \u2018ulimit -HSn 65536 &gt;&gt; \/etc\/rc.local <br \/>\n# source \/etc\/profile <\/span> <br \/>\n<span>4.\u8c03\u9ad8linux\u5185\u6838\u6253\u5f00\u6587\u4ef6\u6570\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u547d\u4ee4(\u5fc5\u987b\u662froot\u5e10\u53f7) <br \/>\necho 'ulimit -HSn 65536' &gt;&gt; \/etc\/profile  <br \/>\necho 'ulimit -HSn 65536' &gt;&gt; \/etc\/rc.local <br \/>\nsource \/etc\/profile <\/span> <br \/>\n<span>\u6ce8\uff1a\u6211\u662f\u4fee\u6539\/etc\/rc.local\uff0c\u52a0\u5165ulimit -SHn 51200\u7684<\/span><\/p>\n<p><span>5. Increase <span>PHP-FPM<\/span><br \/>\n open file description rlimit: <br \/>\n# vi \/path\/to\/<span>php-fpm<\/span>.conf <br \/>\nFind \u201c&lt;value name=\u201drlimit_files\u201d&gt;1024&lt;\/value&gt;\u201d <br \/>\nChange 1024 to 4096 or higher number. <br \/>\nRestart <span>PHP-FPM<\/span>.<\/span> <br \/>\n<span>5. \u589e\u52a0 <span>PHP-FPM<\/span><br \/>\n \u6253\u5f00\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u9650\u5236: <br \/>\n# vi \/path\/to\/<span>php-fpm<\/span>.conf <br \/>\n\u627e\u5230\u201c&lt;value name=\"rlimit_files\"&gt;1024&lt;\/value&gt;\u201d <br \/>\n\u628a1024 \u66f4\u6539\u4e3a 4096 \u6216\u8005\u66f4\u9ad8. <br \/>\n\u91cd\u542f <span>PHP-FPM<\/span>.<\/span> <\/p>\n<p>\n<span>6. Using PHP code accelerator, e.g<br \/>\neAccelerator, XCache. And set \u201ccache_dir\u201d to \/dev\/shm on Linux.<\/span> <br \/>\n<span>6.\u4f7f\u7528php\u4ee3\u7801\u52a0\u901f\u5668\uff0c\u4f8b\u5982 eAccelerator,<br \/>\nXCache.\u5728linux\u5e73\u53f0\u4e0a\u53ef\u4ee5\u628a`cache_dir`\u6307\u5411 \/dev\/shm<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e8eNginx\u5bf9\u9ad8\u5e76\u53d1\u7684\u4f18\u826f\u6027\u80fd\uff0c\u6545\u914d\u4e86\u4e2aNginx+php-fpm\u6765\u8dd1\u5728\u7ebf\u4ee3\u7406\u7a0b\u5e8f\uff0c\u662f\u6309\u7167\u5f20\u5bb4\u6587\u7ae0\u914d\u7684\uff0c\u521a\u914d\u7f6e...<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15329],"tags":[],"class_list":["post-689","post","type-post","status-publish","format-standard","hentry","category-nginx"],"_links":{"self":[{"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=\/wp\/v2\/posts\/689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=689"}],"version-history":[{"count":1,"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=\/wp\/v2\/posts\/689\/revisions"}],"predecessor-version":[{"id":690,"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=\/wp\/v2\/posts\/689\/revisions\/690"}],"wp:attachment":[{"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lnmp.ivan.xin\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}