{"id":151,"date":"2023-08-25T17:44:22","date_gmt":"2023-08-25T09:44:22","guid":{"rendered":"http:\/\/8.130.131.184\/wordpress\/?p=151"},"modified":"2023-08-25T17:44:22","modified_gmt":"2023-08-25T09:44:22","slug":"nginxfastcgi","status":"publish","type":"post","link":"https:\/\/xianyijitan.top\/?p=151","title":{"rendered":"Nginx+FastCGI"},"content":{"rendered":"<h2>FastCGI<\/h2>\n<h3>FastCGI\u7b80\u4ecb<\/h3>\n<p>\u5feb\u901f\u901a\u7528\u7f51\u5173\u63a5\u53e3\uff08Fast Common Gateway Interface\uff0cFastCGI\uff09\u662f\u901a\u7528\u7f51\u5173\u63a5\u53e3\uff08CGI\uff09\u7684\u6539\u8fdb\uff0c\u63cf\u8ff0\u4e86\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u7a0b\u5e8f\u4e4b\u95f4\u4f20\u8f93\u6570\u636e\u7684\u4e00\u79cd\u6807\u51c6\u3002FastCGI \u53ef\u4ee5\u4f7f\u670d\u52a1\u5668\u540c\u65f6\u5904\u7406\u66f4\u591a\u7684 Web \u8bf7\u6c42\u3002<\/p>\n<p>PHP\u81ea\u5e26FPM\uff08FastCGI Process Manager\uff0cFastCGI\u8fdb\u7a0b\u7ba1\u7406\u5668\uff09\u6a21\u5757\uff0c\u7528\u6765\u5bf9PHP\u89e3\u6790\u5b9e\u4f8b\u8fdb\u884c\u7ba1\u7406\u3001\u4f18\u5316\u89e3\u6790\u6548\u7387\u3002FastCGI\u5c06\u9759\u6001\u8bf7\u6c42\u548c\u52a8\u6001\u811a\u672c\u8bed\u8a00\u5206\u79bb\u5f00\uff0cNginx\u4e13\u95e8\u5904\u7406\u9759\u6001\u8bf7\u6c42\uff0c\u8f6c\u53d1\u52a8\u6001\u8bf7\u6c42\u7ed9PHP_FPM\u3002<\/p>\n<p>FastCGI\u662f\u591a\u8fdb\u7a0b\uff0c\u6240\u4ee5\u8fdb\u7a0b\u6d88\u8017\u66f4\u591a\u7684\u670d\u52a1\u5668\u5185\u5b58\uff0cPHP-CGI\u89e3\u91ca\u5668\u6bcf\u8fdb\u7a0b\u6d88\u80177\u81f325\u5146\u5185\u5b58\uff0c\u5c06\u8fd9\u4e2a\u6570\u5b57\u4e58\u4ee550\u6216100\u5c31\u662f\u5f88\u5927\u7684\u5185\u5b58\u6570\u3002<\/p>\n<p>Nginx+PHP(FastCGI)\u670d\u52a1\u5668\u57283\u4e07\u5e76\u53d1\u8fde\u63a5\u4e0b\uff1a<\/p>\n<ul>\n<li>\u5f0010\u4e2aNginx\u8fdb\u7a0b\u6d88\u8017150M\u5185\u5b58\uff0810*15M\uff09<\/li>\n<li>\u5f0064\u4e2aphp-cgi\u8fdb\u7a0b\u6d88\u80171280M\u5185\u5b58\uff0820M*64\uff09<\/li>\n<\/ul>\n<h3>\u914d\u7f6eFastCGI<\/h3>\n<pre><code class=\"language-shell\">[root@localhost ~]# vim \/etc\/php-fpm.d\/www.conf\n[www]\nlisten = 127.0.0.1:9000                                  \n\u6216\nlisten = \/run\/php-fpm\/www.sock<\/code><\/pre>\n<p>\u914d\u7f6eNginx\u6587\u4ef6\uff0c\u4e3b\u914d\u7f6e\u6587\u4ef6\u4e3a\u5b89\u88c5\u76ee\u5f55\u4e0b\u7684conf\/nginx.conf\uff0c\u914d\u7f6eFast-CGI\u652f\u6301PHP\u7f51\u9875\u89e3\u6790<\/p>\n<pre><code class=\"language-shell\">location \/ { \n            root   html;\n            index index.php index.html index.htm;\n        }   \n\n        #error_page  404              \/404.html;\n\n        # redirect server error pages to the static page \/50x.html\n        #   \n        error_page   500 502 503 504  \/50x.html;\n        location = \/50x.html {\n            root   html;\n        }   \n\n        # proxy the PHP scripts to Apache listening on 127.0.0.1:80\n        #   \n        #location ~ \\.php$ {\n        #    proxy_pass   http:\/\/127.0.0.1;\n        #}  \n\n        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000\n        #   \n        location ~ \\.php$ {\n            root           html;\n            fastcgi_pass   unix:\/run\/php-fpm\/www.sock;\n            fastcgi_index  index.php;\n           # fastcgi_param  SCRIPT_FILENAME  \/scripts$fastcgi_script_name;\n            include        fastcgi.conf;\n        }<\/code><\/pre>\n<p>\u6d4b\u8bd5\uff1a\u521b\u5efaPHP\u9996\u9875index.php\uff0c\u6d4b\u8bd5LNMP\u67b6\u6784\u80fd\u5426\u89e3\u6790PHP\u9875\u9762<\/p>\n<pre><code class=\"language-shell\">[root@zabbixserver conf]# \/usr\/local\/nginx\/sbin\/nginx -s reload\n[root@zabbixserver conf]# systemctl restart php-fpm\n[root@zabbixserver conf]# vim \/usr\/local\/nginx\/html\/test.php\n&lt;?php\n$i=33;\necho $i;\n?&gt;<\/code><\/pre>\n<p>\u4f7f\u7528\u6d4f\u89c8\u5668\u8bbf\u95eetest.php \u53ef\u4ee5\u770b\u5230\u9875\u9762\u5185\u5bb9:<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/8.130.131.184\/wp-blog\/typora-user-images\/nginx-fastcgi\/image-20230825173254516.png?w=960\" alt=\"image-20230825173254516\" \/><\/p>\n<p>\u521b\u5efaphp\u9875\u9762\uff0c\u6d4b\u8bd5\u8fde\u63a5\u6570\u636e\u5e93\u7684php\u9875\u9762\uff1a<\/p>\n<pre><code class=\"language-shell\">[root@zabbixserver conf]# vim \/usr\/local\/nginx\/html\/test.php\n&lt;?php\n$mysqli = @new mysqli(&#039;localhost&#039;, &#039;root&#039;, &#039;&#039;, &#039;mysql&#039;);\n\/\/\u4e3b\u673a\u540d\uff0c\u7528\u6237\u540d\uff0c\u5bc6\u7801\uff0c\u6570\u636e\u5e93\nif ($mysqli-&gt;connect_errno) {\n    die(&#039;Connect Error: &#039; . $mysqli-&gt;connect_errno);\n}\nelse{\n    echo &quot;link db OK!!!&quot;;\n}\n?&gt;<\/code><\/pre>\n<p>\u4f7f\u7528\u6d4f\u89c8\u5668\u8bbf\u95eetest.php \u53ef\u4ee5\u770b\u5230\u9875\u9762\u5185\u5bb9:<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/8.130.131.184\/wp-blog\/typora-user-images\/nginx-fastcgi\/image-20230825173510456.png?w=960\" alt=\"image-20230825173510456\" \/><\/p>\n<p>\u4fee\u6539php\u9875\u9762\uff0c\u6d4b\u8bd5\u6570\u636e\u5e93\u8fde\u63a5\u6548\u679c\uff1a<\/p>\n<pre><code class=\"language-shell\">[root@zabbixserver conf]# vim \/usr\/local\/nginx\/html\/test.php\n&lt;?php\n$mysqli = new mysqli(&#039;localhost&#039;,&#039;root&#039;,&#039;&#039;,&#039;mysql&#039;);\nif (mysqli_connect_errno()){\n    die(&#039;Unable to connect!&#039;). mysqli_connect_error();\n}\n$sql = &quot;select * from user&quot;;\n$result = $mysqli-&gt;query($sql);\nwhile($row = $result-&gt;fetch_array()){\n    printf(&quot;Host:%s&quot;,$row[0]);\n    printf(&quot;&lt;\/br&gt;&quot;);\n    printf(&quot;Name:%s&quot;,$row[1]);\n    printf(&quot;&lt;\/br&gt;&quot;);\n}\n?&gt;<\/code><\/pre>\n<p>\u4f7f\u7528\u6d4f\u89c8\u5668\u8bbf\u95eetest.php \u53ef\u4ee5\u770b\u5230\u9875\u9762\u5185\u5bb9:<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/8.130.131.184\/wp-blog\/typora-user-images\/nginx-fastcgi\/image-20230825173803574.png?w=960\" alt=\"image-20230825173803574\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>FastCGI FastCGI\u7b80\u4ecb \u5feb\u901f\u901a\u7528\u7f51\u5173\u63a5\u53e3\uff08Fast Common Gateway Interface\uff0cFastCGI\uff09\u662f\u901a\u7528\u7f51\u5173\u63a5\u53e3\uff08CGI\uff09\u7684\u6539\u8fdb\uff0c\u63cf\u8ff0\u4e86\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u7a0b\u5e8f\u4e4b\u95f4\u4f20\u8f93\u6570\u636e\u7684\u4e00\u79cd\u6807\u51c6\u3002FastCGI \u53ef\u4ee5\u4f7f\u670d\u52a1\u5668\u540c\u65f6\u5904\u7406\u66f4\u591a\u7684 Web \u8bf7\u6c42\u3002 PHP\u81ea\u5e26FPM\uff08FastCGI Process Manager\uff0cFastCGI\u8fdb\u7a0b\u7ba1\u7406\u5668\uff09\u6a21\u5757\uff0c\u7528\u6765\u5bf9PHP\u89e3\u6790\u5b9e\u4f8b\u8fdb\u884c\u7ba1\u7406\u3001\u4f18\u5316\u89e3\u6790\u6548\u7387\u3002FastCGI\u5c06\u9759\u6001\u8bf7\u6c42\u548c\u52a8\u6001\u811a\u672c\u8bed\u8a00\u5206\u79bb\u5f00\uff0cNginx\u4e13\u95e8\u5904\u7406\u9759\u6001\u8bf7\u6c42\uff0c\u8f6c\u53d1\u52a8\u6001\u8bf7\u6c42\u7ed9PHP_FPM\u3002 FastCGI\u662f\u591a\u8fdb\u7a0b\uff0c\u6240\u4ee5\u8fdb\u7a0b\u6d88\u8017\u66f4\u591a\u7684\u670d\u52a1\u5668\u5185\u5b58\uff0cPHP-CGI\u89e3\u91ca\u5668\u6bcf\u8fdb\u7a0b\u6d88\u80177\u81f325\u5146\u5185\u5b58\uff0c\u5c06\u8fd9\u4e2a\u6570\u5b57\u4e58\u4ee550\u6216100\u5c31\u662f\u5f88\u5927\u7684\u5185\u5b58\u6570\u3002 Nginx+PHP(FastCGI)\u670d\u52a1\u5668\u57283\u4e07\u5e76\u53d1\u8fde\u63a5\u4e0b\uff1a \u5f0010\u4e2aNginx\u8fdb\u7a0b\u6d88\u8017150M\u5185\u5b58\uff0810*15M\uff09 \u5f0064\u4e2aphp-cgi\u8fdb\u7a0b\u6d88\u80171280M\u5185\u5b58\uff0820M*64\uff09 \u914d\u7f6eFastCGI [root@localhost ~]# vim \/etc\/php-fpm.d\/www.conf [www] listen = 127.0.0.1:9000 \u6216 listen = \/run\/php-fpm\/www.sock \u914d\u7f6eNginx\u6587\u4ef6\uff0c\u4e3b\u914d\u7f6e\u6587\u4ef6\u4e3a\u5b89\u88c5\u76ee\u5f55\u4e0b\u7684conf\/nginx.conf\uff0c\u914d\u7f6eFast-CGI\u652f\u6301PHP\u7f51\u9875\u89e3\u6790 location \/ { root html; index index.php index.html index.htm; } #error_page 404 \/404.html; # redirect server error pages to the static page \/50x.html # error_page 500 502 503 504 \/50x.html; location = \/50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \\.php$ { # proxy_pass http:\/\/127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \\.php$ { root html; fastcgi_pass unix:\/run\/php-fpm\/www.sock; fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME \/scripts$fastcgi_script_name; include fastcgi.conf; } \u6d4b\u8bd5\uff1a\u521b\u5efaPHP\u9996\u9875index.php\uff0c\u6d4b\u8bd5LNMP\u67b6\u6784\u80fd\u5426\u89e3\u6790PHP\u9875\u9762 [root@zabbixserver conf]# \/usr\/local\/nginx\/sbin\/nginx -s reload [root@zabbixserver conf]# systemctl restart php-fpm [root@zabbixserver conf]# vim \/usr\/local\/nginx\/html\/test.php &lt;?php $i=33; echo $i; ?&gt; \u4f7f\u7528\u6d4f\u89c8\u5668\u8bbf\u95eetest.php \u53ef\u4ee5\u770b\u5230\u9875\u9762\u5185\u5bb9: \u521b\u5efaphp\u9875\u9762\uff0c\u6d4b\u8bd5\u8fde\u63a5\u6570\u636e\u5e93\u7684php\u9875\u9762\uff1a [root@zabbixserver conf]# vim \/usr\/local\/nginx\/html\/test.php &lt;?php $mysqli = @new mysqli(&#039;localhost&#039;, &#039;root&#039;, &#039;&#039;, &#039;mysql&#039;); \/\/\u4e3b\u673a\u540d\uff0c\u7528\u6237\u540d\uff0c\u5bc6\u7801\uff0c\u6570\u636e\u5e93 if ($mysqli-&gt;connect_errno) { die(&#039;Connect Error: &#039; . $mysqli-&gt;connect_errno); } else{ echo &quot;link db OK!!!&quot;; } ?&gt; \u4f7f\u7528\u6d4f\u89c8\u5668\u8bbf\u95eetest.php \u53ef\u4ee5\u770b\u5230\u9875\u9762\u5185\u5bb9: \u4fee\u6539php\u9875\u9762\uff0c\u6d4b\u8bd5\u6570\u636e\u5e93\u8fde\u63a5\u6548\u679c\uff1a [root@zabbixserver conf]# vim \/usr\/local\/nginx\/html\/test.php &lt;?php $mysqli = new mysqli(&#039;localhost&#039;,&#039;root&#039;,&#038;#039&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-151","post","type-post","status-publish","format-standard","hentry","category-lixx"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts\/151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=151"}],"version-history":[{"count":1,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":152,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=\/wp\/v2\/posts\/151\/revisions\/152"}],"wp:attachment":[{"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xianyijitan.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}