{"id":270448,"date":"2024-05-16T12:30:00","date_gmt":"2024-05-16T04:30:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/270448.html"},"modified":"2024-05-16T12:30:09","modified_gmt":"2024-05-16T04:30:09","slug":"nginx-%e5%a6%82%e4%bd%95%e9%83%a8%e7%bd%b2-php7-%e9%a1%b9%e7%9b%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/270448.html","title":{"rendered":"nginx \u5982\u4f55\u90e8\u7f72 php7 \u9879\u76ee"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183937\/e72a4b86-c251-48db-9f2b-04b7e44b4e4e.webp\" alt=\"nginx \u5982\u4f55\u90e8\u7f72 php7 \u9879\u76ee\" \/><\/p>\n<p><p>Nginx \u5982\u4f55\u90e8\u7f72 PHP7 \u9879\u76ee\u6d89\u53ca\u5230<strong>\u914d\u7f6eNginx\u670d\u52a1\u5668<\/strong>\u3001<strong>\u5b89\u88c5PHP7<\/strong>\u4ee5\u53ca\u914d\u7f6ePHP\u5904\u7406\u5668(FastCGI)\u4e09\u4e2a\u5173\u952e\u8fc7\u7a0b\u3002\u914d\u7f6eNginx\u670d\u52a1\u5668\u5305\u62ec\u4e86\u5bf9Nginx\u8fdb\u884c\u76f8\u5e94\u7684\u5b89\u88c5\u548c\u914d\u7f6e\u4ee5\u4f7f\u5176\u80fd\u591f\u5904\u7406PHP\u6587\u4ef6\u7684\u8bf7\u6c42\u3002<strong>\u5176\u4e2d\uff0c\u5b89\u88c5PHP7\u662f\u90e8\u7f72\u8fc7\u7a0b\u4e2d\u7684\u4e00\u4e2a\u6838\u5fc3\u6b65\u9aa4<\/strong>\uff0c\u56e0\u4e3a\u5b83\u76f4\u63a5\u5173\u7cfb\u5230PHP\u7a0b\u5e8f\u662f\u5426\u80fd\u5728\u670d\u52a1\u5668\u4e0a\u6b63\u5e38\u8fd0\u884c\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u4e0d\u4ec5\u6d89\u53ca\u5230PHP7\u7684\u5b89\u88c5\uff0c\u8fd8\u9700\u8981\u914d\u7f6ePHP7\u4e0eNginx\u7684\u4ea4\u4e92\uff0c\u786e\u4fddNginx\u80fd\u591f\u901a\u8fc7PHP FastCGI\u5904\u7406\u5668\u6b63\u786e\u5904\u7406PHP\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8ba8\u8bba\u5982\u4f55\u5b89\u88c5PHP7\u65f6\uff0c\u9700\u6ce8\u610fPHP7\u7684\u5b89\u88c5\u5305\u62ec\u4ece\u5b98\u65b9\u6e90\u4e0b\u8f7d\u5408\u9002\u7248\u672c\u7684PHP\u5e76\u5b89\u88c5\u5728\u670d\u52a1\u5668\u4e0a\u3002\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u9700\u8981\u6839\u636e\u670d\u52a1\u5668\u7684\u64cd\u4f5c\u7cfb\u7edf\u548cPHP\u7a0b\u5e8f\u7684\u9700\u6c42\uff0c\u9009\u62e9\u5b89\u88c5\u989d\u5916\u7684PHP\u6a21\u5757\uff0c\u6bd4\u5982MySQLi\u3001GD\u56fe\u5f62\u5904\u7406\u5e93\u7b49\u3002\u6b63\u786e\u5b89\u88c5\u5e76\u914d\u7f6ePHP7\u662f\u6210\u529f\u90e8\u7f72PHP7\u9879\u76ee\u7684\u57fa\u7840\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610fPHP\u7248\u672c\u4e0e\u9879\u76ee\u7684\u517c\u5bb9\u6027\uff0c\u4ee5\u53ca\u5b89\u5168\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u914d\u7f6eNginx\u670d\u52a1\u5668<\/h3>\n<\/p>\n<p><p>Nginx\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Web\u670d\u52a1\u5668\/\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\u53ca\u7535\u5b50\u90ae\u4ef6(IMAP\/POP3)\u4ee3\u7406\u670d\u52a1\u5668\uff0c\u5e76\u5728\u4e00\u4e2aBSD-like\u534f\u8bae\u4e0b\u53d1\u884c\u3002\u5728\u90e8\u7f72PHP7\u9879\u76ee\u4e4b\u524d\uff0c\u9700\u786e\u4fddNginx\u670d\u52a1\u5668\u5df2\u88ab\u6b63\u786e\u5b89\u88c5\u5e76\u8fd0\u884c\u5728\u670d\u52a1\u5668\u4e0a\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Nginx\u3002\u53ef\u4ee5\u901a\u8fc7\u670d\u52a1\u5668\u7684\u5305\u7ba1\u7406\u5668\u5b89\u88c5Nginx\uff0c\u5982Ubuntu\u4e2d\u4f7f\u7528<code>apt-get install nginx<\/code>\uff0cCentOS\u4e2d\u4f7f\u7528<code>yum install nginx<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u670d\u52a1\u5668\u7684\u516c\u5f00IP\u5730\u5740\u6765\u9a8c\u8bc1Nginx\u662f\u5426\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u914d\u7f6eNginx\u4ee5\u89e3\u6790PHP\u6587\u4ef6\u3002\u8fd9\u6d89\u53ca\u4fee\u6539Nginx\u7684\u914d\u7f6e\u6587\u4ef6\u3002\u901a\u5e38\uff0c\u8be5\u914d\u7f6e\u6587\u4ef6\u4f4d\u4e8e<code>\/etc\/nginx\/sites-av<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>lable\/<\/code>\u76ee\u5f55\u4e0b\u3002\u4f60\u9700\u8981\u4e3a\u4f60\u7684\u7ad9\u70b9\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u914d\u7f6e\u6587\u4ef6\u6216\u7f16\u8f91\u9ed8\u8ba4\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u8bbe\u7f6e<code>server<\/code>\u5757\uff0c\u4ee5\u6307\u5411\u4f60\u7684PHP\u9879\u76ee\u7684\u6839\u76ee\u5f55\uff0c\u5e76\u914d\u7f6e<code>location ~ \\.php$<\/code>\u6307\u4ee4\uff0c\u4ee5\u4fbfNginx\u80fd\u5c06PHP\u8bf7\u6c42\u8f6c\u53d1\u7ed9PHP\u5904\u7406\u5668(FastCGI)\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b89\u88c5PHP7<\/h3>\n<\/p>\n<p><p>PHP7\u63d0\u4f9b\u4e86\u6bd4\u5176\u524d\u7248\u672c\u66f4\u597d\u7684\u6027\u80fd\u548c\u66f4\u4f4e\u7684\u8d44\u6e90\u6d88\u8017\u3002\u5728\u5b89\u88c5PHP7\u4e4b\u524d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u6dfb\u52a0PHP\u7684\u5b98\u65b9\u4ed3\u5e93\u6765\u786e\u4fdd\u5b89\u88c5\u6700\u65b0\u7248\u672c\u7684PHP\u3002Ubuntu\u7528\u6237\u53ef\u4ee5\u4f7f\u7528<code>add-apt-repository ppa:ondrej\/php<\/code>\u547d\u4ee4\u6765\u6dfb\u52a0PHP\u4ed3\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5PHP7\u65f6\uff0c\u4e0d\u8981\u5fd8\u8bb0\u5b89\u88c5\u5fc5\u8981\u7684PHP\u6a21\u5757\uff0c\u8fd9\u4e9b\u6a21\u5757\u5c06\u5e2e\u52a9PHP7\u9ad8\u6548\u5730\u8fd0\u884c\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u4f7f\u7528\u547d\u4ee4<code>apt-get install php7.0 php7.0-fpm php7.0-mysql<\/code>\uff08\u4ee5Ubuntu\u4e3a\u4f8b\uff09\u53ef\u4ee5\u5b89\u88c5PHP7\u53ca\u5176\u5e38\u7528\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5fc5\u987b\u542f\u52a8PHP7.0-FPM\u670d\u52a1\u5e76\u786e\u8ba4\u5176\u8fd0\u884c\u72b6\u6001\u3002\u53ef\u4ee5\u4f7f\u7528<code>systemctl start php7.0-fpm<\/code>\u5f00\u542f\u670d\u52a1\uff0c\u5e76\u7528<code>systemctl status php7.0-fpm<\/code>\u68c0\u67e5\u670d\u52a1\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u914d\u7f6ePHP\u5904\u7406\u5668(FastCGI)<\/h3>\n<\/p>\n<p><p>\u5728Nginx\u670d\u52a1\u4e2d\uff0cPHP\u6587\u4ef6\u7684\u5904\u7406\u4f9d\u8d56\u4e8eFastCGI\u8fdb\u7a0b\u7ba1\u7406\u5668(fpm)\u3002\u56e0\u6b64\uff0c\u9700\u8981\u786e\u4fddPHP\u7684fpm\u670d\u52a1\u8fd0\u884c\u5e76\u88abNginx\u670d\u52a1\u5668\u6b63\u786e\u5f15\u7528\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u4fee\u6539<code>\/etc\/php\/7.0\/fpm\/pool.d\/www.conf<\/code>\u6587\u4ef6\uff08\u8def\u5f84\u53ef\u80fd\u56e0\u5b89\u88c5\u4f4d\u7f6e\u4e0d\u540c\u800c\u5f02\uff09\uff0c\u786e\u4fddfpm\u76d1\u542c\u6b63\u786e\u7684socket\u6216\u7aef\u53e3\u3002\u901a\u5e38\uff0cfpm\u914d\u7f6e\u4e3a\u76d1\u542c<code>\/var\/run\/php\/php7.0-fpm.sock<\/code>\uff0c\u4f46\u4e5f\u53ef\u4ee5\u914d\u7f6e\u4e3a\u76d1\u542cTCP\u7aef\u53e3\u3002<\/p>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728Nginx\u7684\u7ad9\u70b9\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0\u6216\u4fee\u6539<code>location ~ \\.php$<\/code>\u4ee3\u7801\u5757\uff0c\u8bbe\u7f6e<code>fastcgi_pass<\/code>\u6307\u5411PHP fpm\u76d1\u542c\u7684socket\u6216\u7aef\u53e3\uff0c<code>fastcgi_index<\/code>\u6307\u5411\u9ed8\u8ba4\u5904\u7406\u811a\u672c\u6587\u4ef6\uff0c\u901a\u5e38\u662f<code>index.php<\/code>\u3002\u540c\u65f6\uff0c\u786e\u4fdd<code>fastcgi_param<\/code>\u6307\u4ee4\u6b63\u786e\u5f15\u7528<code>SCRIPT_FILENAME<\/code>\uff0c\u4ee5\u4fbffpm\u80fd\u627e\u5230\u5e76\u5904\u7406PHP\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u914d\u7f6e\u5b8c\u6210\u540e\uff0c\u91cd\u542fNginx\u548cPHP7.0-FPM\u670d\u52a1\u4ee5\u5e94\u7528\u66f4\u6539\u3002\u4f7f\u7528<code>systemctl restart nginx<\/code>\u548c<code>systemctl restart php7.0-fpm<\/code>\u547d\u4ee4\u6765\u91cd\u542f\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bed<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0cNginx\u670d\u52a1\u5668\u5c31\u80fd\u591f\u6b63\u786e\u90e8\u7f72\u548c\u8fd0\u884cPHP7\u9879\u76ee\u4e86\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u8981\u6c42\u4ed4\u7ec6\u914d\u7f6eNginx\u548cPHP\u7684\u76f8\u5173\u8bbe\u7f6e\uff0c\u5e76\u786e\u4fdd\u4e24\u8005\u6b63\u786e\u4ea4\u4e92\u3002\u6b63\u786e\u914d\u7f6e\u540e\uff0c\u4f60\u7684PHP7\u9879\u76ee\u5c06\u80fd\u591f\u5728\u9ad8\u6027\u80fd\u7684Nginx\u670d\u52a1\u5668\u4e0a\u9ad8\u6548\u8fd0\u884c\uff0c\u4ece\u800c\u7ed9\u7528\u6237\u5e26\u6765\u66f4\u5feb\u7684\u8bbf\u95ee\u901f\u5ea6\u548c\u66f4\u597d\u7684\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: \u5982\u4f55\u5728nginx\u4e0a\u90e8\u7f72\u4e00\u4e2aphp7\u9879\u76ee\uff1f<\/strong><\/p>\n<p>A: 1. \u9996\u5148\uff0c\u786e\u4fdd\u60a8\u7684\u670d\u52a1\u5668\u5df2\u7ecf\u5b89\u88c5\u4e86nginx\u548cphp7\u3002<br \/>\n   2. \u5728nginx\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u627e\u5230server\u5757\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4e00\u6bb5php\u5904\u7406\u4ee3\u7801\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>```\nlocation ~ \\.php$ {\n    fastcgi_pass unix:\/var\/run\/php\/php7.0-fpm.sock;  #php7\u7684fastcgi\u5730\u5740\n    fastcgi_index index.php;\n    fastcgi_param SCRIPT_FILENAME \/path\/to\/your\/project$fastcgi_script_name;\n    include fastcgi_params;\n}\n```\n\n3. \u91cd\u542fnginx\u670d\u52a1\uff1a`sudo service nginx restart`\n4. \u786e\u4fdd\u60a8\u7684php\u9879\u76ee\u4f4d\u4e8e\u6307\u5b9a\u7684\u8def\u5f84\uff08\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u7684`\/path\/to\/your\/project`\uff09\n5. \u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee\u60a8\u7684\u7f51\u7ad9\uff0c\u5e94\u8be5\u80fd\u591f\u770b\u5230php\u9879\u76ee\u7684\u9875\u9762\u3002\n<\/code><\/pre>\n<p><strong>Q: \u6211\u5982\u4f55\u5728nginx\u670d\u52a1\u5668\u4e0a\u90e8\u7f72\u548c\u914d\u7f6ephp7\u9879\u76ee\uff1f<\/strong><\/p>\n<p>A: 1. \u9996\u5148\uff0c\u786e\u4fdd\u60a8\u7684\u670d\u52a1\u5668\u4e0a\u5b89\u88c5\u4e86nginx\u548cphp7\u3002<br \/>\n   2. \u6253\u5f00nginx\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u5e76\u627e\u5230<code>server<\/code>\u5757\u3002<br \/>\n   3. \u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u6bb5\u6765\u5904\u7406php\u6587\u4ef6\uff1a<\/p>\n<pre><code>```\nlocation ~ \\.php$ {\n    fastcgi_pass unix:\/var\/run\/php\/php7.0-fpm.sock;\n    fastcgi_index index.php;\n    fastcgi_param SCRIPT_FILENAME \/path\/to\/your\/project$fastcgi_script_name;\n    include fastcgi_params;\n}\n```\n<\/code><\/pre>\n<ol start=\"4\">\n<li>\u5c06<code>\/path\/to\/your\/project<\/code>\u66ff\u6362\u4e3a\u60a8\u5b9e\u9645php\u9879\u76ee\u6240\u5728\u7684\u8def\u5f84\u3002<\/li>\n<li>\u4fdd\u5b58\u914d\u7f6e\u6587\u4ef6\u5e76\u91cd\u542fnginx\u670d\u52a1\uff1a<code>sudo service nginx restart<\/code>\u3002<\/li>\n<li>\u8bbf\u95ee\u60a8\u7684\u7f51\u7ad9\uff0c\u5e94\u8be5\u80fd\u591f\u6210\u529f\u90e8\u7f72\u548c\u914d\u7f6ephp7\u9879\u76ee\u3002<\/li>\n<\/ol>\n<p><strong>Q: \u5927\u5bb6\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u6559\u6211\u5982\u4f55\u5728nginx\u4e0a\u90e8\u7f72\u5305\u542bphp7\u7684\u9879\u76ee\uff1f<\/strong><\/p>\n<p>A: 1. \u5728\u60a8\u7684\u670d\u52a1\u5668\u4e0a\uff0c\u786e\u4fddnginx\u548cphp7\u5df2\u7ecf\u6b63\u786e\u5b89\u88c5\u3002<br \/>\n   2. \u627e\u5230nginx\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u5e76\u8fdb\u5165<code>server<\/code>\u5757\u3002<br \/>\n   3. \u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u6bb5\u6765\u5904\u7406php\u6587\u4ef6\uff1a<\/p>\n<pre><code>```\nlocation ~ \\.php$ {\n    fastcgi_pass unix:\/var\/run\/php\/php7.0-fpm.sock;\n    fastcgi_index index.php;\n    fastcgi_param SCRIPT_FILENAME \/path\/to\/your\/project$fastcgi_script_name;\n    include fastcgi_params;\n}\n```\n<\/code><\/pre>\n<ol start=\"4\">\n<li>\u66ff\u6362<code>\/path\/to\/your\/project<\/code>\u4e3a\u5b9e\u9645php\u9879\u76ee\u7684\u8def\u5f84\u3002<\/li>\n<li>\u4fdd\u5b58\u914d\u7f6e\u6587\u4ef6\u5e76\u91cd\u542fnginx\u670d\u52a1\uff1a<code>sudo service nginx restart<\/code>\u3002<\/li>\n<li>\u786e\u4fdd\u60a8\u7684php7\u9879\u76ee\u4f4d\u4e8e\u6307\u5b9a\u7684\u8def\u5f84\u4e0a\u3002<\/li>\n<li>\u6253\u5f00\u6d4f\u89c8\u5668\u5e76\u8bbf\u95ee\u60a8\u7684\u7f51\u7ad9\uff0cphp7\u9879\u76ee\u5e94\u8be5\u5df2\u7ecf\u90e8\u7f72\u6210\u529f\u4e86\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"Nginx \u5982\u4f55\u90e8\u7f72 PHP7 \u9879\u76ee\u6d89\u53ca\u5230\u914d\u7f6eNginx\u670d\u52a1\u5668\u3001\u5b89\u88c5PHP7\u4ee5\u53ca\u914d\u7f6ePHP\u5904\u7406\u5668(FastCG [&hellip;]","protected":false},"author":3,"featured_media":270459,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/270448"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=270448"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/270448\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/270459"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=270448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=270448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=270448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}