{"id":4740,"date":"2019-09-17T06:43:20","date_gmt":"2019-09-17T06:43:20","guid":{"rendered":"http:\/\/myprojects.advchaweb.com\/?p=4740"},"modified":"2019-09-18T03:46:09","modified_gmt":"2019-09-18T03:46:09","slug":"install-swoole-on-ubuntu-16-04","status":"publish","type":"post","link":"https:\/\/myprojects.advchaweb.com\/index.php\/2019\/09\/17\/install-swoole-on-ubuntu-16-04\/","title":{"rendered":"Install Swoole on Ubuntu 16.04"},"content":{"rendered":"<p>Ref: https:\/\/github.com\/swoole\/swoole-docs\/blob\/master\/get-started\/installation.md<br \/>\nInstall from pecl<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy:~$ sudo pecl install swoole\r\n[sudo] password for teddy: \r\nWARNING: channel \"pecl.php.net\" has updated its protocols, use \"pecl channel-update pecl.php.net\" to update\r\ndownloading swoole-4.4.5.tgz ...\r\nStarting to download swoole-4.4.5.tgz (1,426,083 bytes)\r\n.........................................................................................................................................................................................................................................................................................done: 1,426,083 bytes\r\n391 source files, building\r\nrunning: phpize\r\nConfiguring for:\r\nPHP Api Version:         20170718\r\nZend Module Api No:      20170718\r\nZend Extension Api No:   320170718\r\nenable sockets supports? [no] : yes\r\nenable openssl support? [no] : yes\r\nenable http2 support? [no] : yes\r\nenable mysqlnd support? [no] : yes\r\nbuilding in \/tmp\/pear\/temp\/pear-build-rootIVbbu7\/swoole-4.4.5\r\n...\r\nBuild process completed successfully\r\nInstalling '\/usr\/include\/php\/20170718\/ext\/swoole\/config.h'\r\nInstalling '\/usr\/lib\/php\/20170718\/swoole.so'\r\ninstall ok: channel:\/\/pecl.php.net\/swoole-4.4.5\r\nconfiguration option \"php_ini\" is not set to php.ini location\r\nYou should add \"extension=swoole.so\" to php.ini<\/pre>\n<p>Then include the extension &#8220;extension=swoole.so&#8221; in php.ini file. For php-fpm, the php.ini file in &#8220;\/etc\/php\/7.2\/fpm\/php.ini&#8221;<\/p>\n<pre class=\"lang:default decode:true \">sudo gedit \/etc\/php\/7.2\/fpm\/php.ini<\/pre>\n<p>then add this new line:<\/p>\n<pre class=\"lang:default decode:true\">...\r\nextension=swoole.so\r\n...<\/pre>\n<p>Save then restart php7.2-fpm service<\/p>\n<pre class=\"lang:default decode:true \">sudo service php7.2-fpm restart<\/pre>\n<p>look it up phpinfo() result. It should show like this:<a href=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2019\/09\/phpinfo_swoole.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4744\" src=\"http:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2019\/09\/phpinfo_swoole.png\" alt=\"\" width=\"967\" height=\"648\" srcset=\"https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2019\/09\/phpinfo_swoole.png 967w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2019\/09\/phpinfo_swoole-300x201.png 300w, https:\/\/myprojects.advchaweb.com\/wp-content\/uploads\/2019\/09\/phpinfo_swoole-768x515.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a>Pls do the same thing for php cli. the php.ini file for php cli in &#8220;\/etc\/php\/7.2\/cli\/php.ini&#8221;. So edit the file and add a new line for the extension &#8220;extension=swoole.so&#8221;. Check the extension is loaded on the terminal:<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy:~$ php -m | grep swoole\r\nswoole<\/pre>\n<p>Note: I tried to do this via terminal<\/p>\n<pre class=\"lang:default decode:true \">php -i | grep php.ini                      # check the php.ini file location\r\nsudo echo \"extension=swoole.so\" &gt; php.ini  # add the extension=swoole.so to the end of php.ini\r\nphp -m | grep swoole                       # check if the swoole extension has been enabled<\/pre>\n<p>But it didn&#8217;t work. The extension is not loaded so I put it manually in the php.ini for cli.<br \/>\nNOTE: IT NEED TO DISABLE &#8216;xdebug&#8217; EXTENSION. CHECK IF xdebug IS ACTIVE\/ENABLED:<\/p>\n<pre class=\"lang:default decode:true\">teddy@teddy:~$ php -i | grep xdebug\r\n\/etc\/php\/7.2\/cli\/conf.d\/20-xdebug.ini,\r\nextension 'xdebug' detected\r\nxdebug\r\nxdebug support =&gt; enabled\r\n...<\/pre>\n<p>IF IT IS ENABLED, THEN MODIFY &#8216;\/etc\/php\/7.2\/cli\/conf.d\/20-xdebug.ini&#8217; FILE TO DISABLE xdebug BY ADDING &#8216;;&#8217; IN FRONT OF THE LINE LIKE THIS:<\/p>\n<pre class=\"lang:default decode:true \">;zend_extension=xdebug.so<\/pre>\n<p>IF xdebug IS ENABLED WHEN THE SERVER IS RUNNING, IT&#8217;D SPIT THIS WARNING:<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy:~\/Documents\/works\/swoole$ php server.php \r\nPHP Warning:  Swoole\\Server::start(): Using Xdebug in coroutines is extremely dangerous, please notice that it may lead to coredump! in \/home\/teddy\/Documents\/works\/swoole\/server.php on line 21\r\nPHP Stack trace:\r\nPHP   1. {main}() \/home\/teddy\/Documents\/works\/swoole\/server.php:0\r\nPHP   2. Swoole\\Server-&gt;start() \/home\/teddy\/Documents\/works\/swoole\/server.php:21\r\n...<\/pre>\n<p>TEST SWOOLE<br \/>\nref: https:\/\/github.com\/swoole\/swoole-docs\/blob\/master\/get-started\/examples\/tcp_server.md<br \/>\nCreate a new file server.php<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\/\/ Create the server object and listen 127.0.0.1:9501\r\n$server = new swoole_server(\"127.0.0.1\", 9501);\r\n\r\n\/\/ Register the function for the event `connect`\r\n$server-&gt;on('connect', function($server, $fd){\r\n    echo \"Client : Connect.\\n\";\r\n});\r\n\r\n\/\/ Register the function for the event `receive`\r\n$server-&gt;on('receive', function($server, $fd, $from_id, $data){\r\n    $server-&gt;send($fd, \"Server: \" . $data);\r\n});\r\n\r\n\/\/ Register the function for the event `close`\r\n$server-&gt;on('close', function($server, $fd){\r\n    echo \"Client: {$fd} close.\\n\";\r\n});\r\n\r\n\/\/ Start the server\r\n$server-&gt;start();<\/pre>\n<p>Run the php in a terminal<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy:~\/Documents\/works\/swoole$ php server.php<\/pre>\n<p>then open another terminal and type &#8216;telnet 127.0.0.1 9501&#8217;<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy:~$ telnet 127.0.0.1 9501\r\nTrying 127.0.0.1...\r\nConnected to 127.0.0.1.\r\nEscape character is '^]'.<\/pre>\n<p>The first terminal would print &#8216;Client : Connect&#8217;<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy:~\/Documents\/works\/swoole$ php server.php \r\nClient : Connect.<\/pre>\n<p>You can connect with another new terminal.<br \/>\nTo close the connection in the second terminal, press Ctrl + ] then type close in telnet&gt;<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy:~$ telnet 127.0.0.1 9501\r\nTrying 127.0.0.1...\r\nConnected to 127.0.0.1.\r\nEscape character is '^]'.\r\n\r\nServer: \r\nhello\r\nServer: hello\r\n^]\r\ntelnet&gt; close\r\nConnection closed.<\/pre>\n<p>The first terminal would give response like this:<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy:~\/Documents\/works\/swoole$ php server.php \r\nClient : Connect.\r\nClient : Connect.\r\nClient: 1 close.<\/pre>\n<p>Create web server<br \/>\nref: https:\/\/github.com\/swoole\/swoole-docs\/blob\/master\/get-started\/examples\/http_server.md<br \/>\ncreate a new file &#8216;http_server.php&#8217;<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\/\/ Create the http server object \r\n$http_server = new swoole_http_server(\"127.0.0.1\", 9503);\r\n\r\n\/\/ Register the event of request\r\n$http_server-&gt;on('request', function($request, $response){\r\n    var_dump($request-&gt;get, $request-&gt;post);\r\n    $response-&gt;header(\"Content-Type\", \"text\/html;charset=utf-8\");\r\n    $response-&gt;end(\"&lt;h1&gt;Hello Swoole \" . rand(1000, 9999) . \"&lt;\/h1&gt;\");\r\n});\r\n\r\n\/\/ Start the server\r\n$http_server-&gt;start();<\/pre>\n<p>then run the server via terminal<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy:~\/Documents\/works\/swoole$ php http_server.php<\/pre>\n<p>Open it via web browser: http:\/\/127.0.0.1:9503\/<br \/>\nIt should show this message:<\/p>\n<pre class=\"lang:default decode:true \">Hello Swoole 8267<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ref: https:\/\/github.com\/swoole\/swoole-docs\/blob\/master\/get-started\/installation.md Install from pecl teddy@teddy:~$ sudo pecl install swoole [sudo] password for teddy: WARNING: channel &#8220;pecl.php.net&#8221; has updated its protocols, use &#8220;pecl channel-update pecl.php.net&#8221; to update downloading swoole-4.4.5.tgz &#8230; Starting to download swoole-4.4.5.tgz (1,426,083 bytes) &#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..done: 1,426,083 bytes 391 source files, building running: phpize Configuring for: PHP Api Version: 20170718 Zend Module Api No: &hellip; <a href=\"https:\/\/myprojects.advchaweb.com\/index.php\/2019\/09\/17\/install-swoole-on-ubuntu-16-04\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Install Swoole on Ubuntu 16.04&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,17,78],"tags":[],"class_list":["post-4740","post","type-post","status-publish","format-standard","hentry","category-php-2","category-ubuntu","category-ubuntu-16-04"],"_links":{"self":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/4740","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/comments?post=4740"}],"version-history":[{"count":8,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/4740\/revisions"}],"predecessor-version":[{"id":4743,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/4740\/revisions\/4743"}],"wp:attachment":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/media?parent=4740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/categories?post=4740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/tags?post=4740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}