{"id":10006841,"date":"2022-11-09T10:32:43","date_gmt":"2022-11-09T02:32:43","guid":{"rendered":"https:\/\/ityet.com\/?p=10006841"},"modified":"2022-11-09T10:32:43","modified_gmt":"2022-11-09T02:32:43","slug":"swoole-cli-5-0-1%ef%bc%9aphp-%e7%9a%84%e4%ba%8c%e8%bf%9b%e5%88%b6%e5%8f%91%e8%a1%8c%e7%89%88","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006841","title":{"rendered":"Swoole-Cli 5.0.1\uff1aPHP \u7684\u4e8c\u8fdb\u5236\u53d1\u884c\u7248"},"content":{"rendered":"<h2>Swoole-Cli \u4ecb\u7ecd<\/h2>\n<p><code>Swoole-Cli<\/code> \u662f\u4e00\u4e2a PHP \u7684\u4e8c\u8fdb\u5236\u53d1\u884c\u7248\uff0c\u96c6\u6210\u4e86 <code>swoole<\/code>\u3001<code>php \u5185\u6838<\/code>\u3001<code>php-cli<\/code>\u3001<code>php-fpm<\/code>\u4ee5\u53ca\u591a\u4e2a\u5e38\u7528\u6269\u5c55\u3002<code>Swoole-Cli<\/code> \u662f\u5168\u90e8\u9759\u6001\u7f16\u8bd1\u6253\u5305\u7684\uff0c\u4e0d\u4f9d\u8d56\u4efb\u4f55\u64cd\u4f5c\u7cfb\u7edf\u7684<code>so<\/code>\u52a8\u6001\u94fe\u63a5\u5e93\uff0c\u5177\u5907\u975e\u5e38\u597d\u7684\u79fb\u690d\u6027\uff0c\u53ef\u4ee5\u5728\u4efb\u610f <code>Linux<\/code> \u7cfb\u7edf\u4e4b\u95f4\u590d\u5236\uff0c\u4e0b\u8f7d\u5373\u53ef\u4f7f\u7528\u3002<\/p>\n<p>GitHub: <a href=\"https:\/\/link.segmentfault.com\/?enc=E4HE1UNW%2F3Od6UTWvQdpmQ%3D%3D.l9QxuzFluZ%2FL%2F%2Fpr6PDBHTdwPT%2FhQ9FFMoCRzT3OZtTS8ktFsCDTeKZb7m%2FgOyfo\" rel=\"nofollow\">https:\/\/github.com\/swoole\/swoole-cli<\/a><\/p>\n<h2>\u64cd\u4f5c\u7cfb\u7edf<\/h2>\n<p><code>Swoole-Cli<\/code> \u4f1a\u63d0\u4f9b<code>Linux<\/code>\u3001<code>macOS<\/code>\u3001<code>Windows(Cygwin)<\/code> 3\u79cd\u64cd\u4f5c\u7cfb\u7edf\u7684\u4e8c\u8fdb\u5236\u5305\u7684\u652f\u6301\u3002<\/p>\n<blockquote><p><code>Windows<\/code> \u73af\u5883\u63a8\u8350\u4f7f\u7528 <code>WSL<\/code><\/p><\/blockquote>\n<h2>\u4e3b\u8981\u66f4\u65b0<\/h2>\n<ol>\n<li><code>PHP<\/code> \u540c\u6b65\u5230 <code>8.1.12<\/code> \u7248\u672c<\/li>\n<li>\u589e\u52a0\u4e86 <code>mongodb<\/code> \u6269\u5c55<\/li>\n<li>\u96c6\u6210\u4e86 <code>cli http server<\/code> \u548c <code>php-fpm<\/code><\/li>\n<\/ol>\n<h2>\u4e0b\u8f7d\u5b89\u88c5<\/h2>\n<p>\u4e0b\u8f7d\u5730\u5740\uff1a<\/p>\n<ul>\n<li><code>linux x64<\/code>\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=3n1G25vfrVF%2BA1IGE84NRw%3D%3D.zZsd4sqpTQq8o1%2FB1YDOxe%2BJGafFV5ctX5Dd1goMgbFK7vNBqpkTgiwtOE8V1%2F365P6MUfhD%2F%2FNAupcZsvTPvO6spiXB%2FVTA4m97LYJRt%2BFZ%2BIJlfW3E3y%2BuqwsDDn7U\" rel=\"nofollow\">https:\/\/wenda-1252906962.file.myqcloud.com\/dist\/swoole-cli-v5.0.1-linux-x64.tar.xz<\/a><\/li>\n<\/ul>\n<p>\u76f4\u63a5\u4e0b\u8f7d\u540e\u653e\u7f6e\u5230 <code>\/usr\/bin<\/code> \u76ee\u5f55\uff0c\u5e76\u4e14\u8bbe\u7f6e\u4e3a\u53ef\u8fd0\u884c\uff0c<code>chmod +x \/usr\/bin\/swoole-cli<\/code> \u5373\u53ef\u3002\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\uff1a<\/p>\n<pre><code class=\"shell\">swoole-cli -v\nswoole-cli -m<\/code><\/pre>\n<h2>\u66ff\u4ee3 PHP<\/h2>\n<p>\u5c06 <code>Swoole-Cli<\/code> \u8f6f\u94fe\u63a5\u5230 <code>php<\/code>\uff0c\u66ff\u4ee3 <code>PHP<\/code><\/p>\n<pre><code class=\"shell\">ln -s \/usr\/bin\/swoole-cli \/usr\/bin\/php\nphp -v\n# \u5b89\u88c5 composer\ncurl -sS https:\/\/getcomposer.org\/installer | php<\/code><\/pre>\n<h2>\u914d\u7f6e\u6587\u4ef6<\/h2>\n<p><code>swoole-cli<\/code> \u9ed8\u8ba4\u4e0d\u52a0\u8f7d\u4efb\u4f55 <code>php.ini<\/code> \u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<p>\u53ef\u901a\u8fc7 <code>-d<\/code> \u53c2\u6570\u6765\u8bbe\u7f6e <code>PHP<\/code> \u9009\u9879\u6216\u4f7f\u7528 <code>-c<\/code> \u53c2\u6570\u6307\u5b9a\u52a0\u8f7d\u7684<code>php.ini<\/code>\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<pre><code class=\"shell\">swoole-cli -d swoole.use_shortname=off bin\/hyperf.php start\nswoole-cli -c \/tmp\/php.ini -v<\/code><\/pre>\n<h2>\u542f\u52a8 PHP-FPM<\/h2>\n<p><code>swoole-cli<\/code> \u96c6\u6210\u4e86 <code>PHP-FPM<\/code> \uff0c\u53ef\u4f7f\u7528 <code>-P<\/code> \u547d\u4ee4\u6765\u542f\u52a8 <code>fpm<\/code> \u3002<\/p>\n<pre><code class=\"shell\"># \u67e5\u770b\u5e2e\u52a9\u6587\u4ef6\nswoole-cli -P -h\n# \u8fd0\u884c FPM\nswoole-cli -P --fpm-config \/opt\/php-8.1\/etc\/php-fpm.conf -p \/opt\/php-8.1\/var\n# \u5173\u95ed\u5b88\u62a4\u8fdb\u7a0b\nswoole-cli -P --fpm-config \/opt\/php-8.1\/etc\/php-fpm.conf -p \/opt\/php-8.1\/var -F\n# \u4f7f\u7528 root \u8d26\u6237\u542f\u52a8\nswoole-cli -P --fpm-config \/opt\/php-8.1\/etc\/php-fpm.conf -p \/opt\/php-8.1\/var -F -R<\/code><\/pre>\n<h2>\u542f\u52a8 CLI Server<\/h2>\n<pre><code class=\"shell\"># \u4f7f\u7528 Laravel Artisan \u5de5\u5177\nswoole-cli artisan serve\n# \u542f\u52a8 CLI Server\nswoole-cli -S 127.0.0.1:9001<\/code><\/pre>\n<h2>\u542f\u52a8 Swoole Server<\/h2>\n<h3>server.php<\/h3>\n<pre><code class=\"php\">$http = new Swoole\/Http\/Server('127.0.0.1', 9501);\n\n$http-&gt;on('start', function ($server) {\n    echo &quot;Swoole http server is started at http:\/\/127.0.0.1:9501\/n&quot;;\n});\n\n$http-&gt;on('request', function ($request, $response) {\n    $response-&gt;header('Content-Type', 'text\/plain');\n    $response-&gt;end('Hello World');\n});\n\n$http-&gt;start();<\/code><\/pre>\n<h3>\u6267\u884c<\/h3>\n<pre><code class=\"shell\">swoole-cli server.php<\/code><\/pre>\n<h2>\u6269\u5c55\u5217\u8868<\/h2>\n<pre><code class=\"shell\"><a href=\"\/cdn-cgi\/l\/email-protection\" class=\"__cf_email__\" data-cfemail=\"b3dbc7d5f3dbc7d59ee7dbdaddd8e3d2d79ee7878483c3\">[email&#160;protected]<\/a>:~\/workspace\/cli-swoole$ .\/bin\/swoole-cli -m\n[PHP Modules]\nbcmath\nbz2\nCore\nctype\ncurl\ndate\ndom\nexif\nfileinfo\nfilter\ngd\ngmp\nhash\niconv\nimagick\nintl\njson\nlibxml\nmbstring\nmongodb\nmysqli\nmysqlnd\nopenssl\npcntl\npcre\nPDO\npdo_mysql\npdo_sqlite\nPhar\nposix\nredis\nReflection\nsession\nSimpleXML\nsoap\nsockets\nsodium\nSPL\nsqlite3\nstandard\nswoole\ntokenizer\nxml\nxmlreader\nxmlwriter\nxsl\nyaml\nzip\nzlib\n\n[Zend Modules]\n<\/code><\/pre>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042773837\">https:\/\/segmentfault.com\/a\/1190000042773837<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Swoole-Cli \u4ecb\u7ecd Swoole-Cli \u662f\u4e00\u4e2a PHP \u7684\u4e8c\u8fdb\u5236\u53d1\u884c\u7248\uff0c\u96c6\u6210\u4e86 swoole\u3001php [&hellip;]<\/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":[4],"tags":[],"class_list":["post-10006841","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006841"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006841\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}