Swoole is a PHP asynchronous, parallel, and step-by-step extension written by cool people in China. It is a PHP high-performance network communication framework and provides an asynchronous multi-thread server in the PHP language, asynchronous TCP/UDP network client, asynchronous MySQL, database connection pool, AsyncTask, message queue, millisecond timer, asynchronous file read/write, asynchronous DNS query.
Swoole can be widely used in the Internet, mobile communications, enterprise software, online games, IoT, IoV, smart home, and other fields. Using PHP + Swoole as the network communication framework can greatly improve the efficiency of enterprise it r & D teams and focus more on developing innovative products.
Next, install the Swoole extension in the Ubuntu environment. (For more information, see the previous blog post in my Ubuntu environment)
1. Confirm the installation environment
Only the Linux operating system is supported, and the core code is compatible with FreeBSD. You need to adjust some compilation parameters and details before passing through
Linux kernel version 2.3.32 or later
PHP5.3.10 or later
Gcc4.4 or later. The core code is clang compatible and the CPU affinity setting feature must be disabled.
Use cmake
2. Installation and configuration
2. 1 install swoole: directly install swoole in pecl or download swoole for releases
2.11 compile and install swoole extension
Download from: http://pecl.php.net/package/swoole
The code is as follows: |
Copy code |
# Cd swoole # Phpize #./Configure # Make & make install |
2.12 install swoole extension in pecl
If pecl is unavailable, check that the bin directory of the php installation directory has been added to the system variable.
This article describes how to install it using pecl
The code is as follows: |
Copy code |
Zzs @ ubuntu :~ $ Sudo pecl install swoole Downloading swoole-1.7.8.tgz... Starting to download swoole-1.7.8.tgz (412,906 bytes) .... Done: 412,906 bytes 130 source files, building Running: phpize Sh: 1: phpize: not found If the command failed with 'phpize: not found' then you need to install php5-dev packageYou can do it by running 'Apt-get install php5-dev 'as a root userERROR: 'phpiz' failed Zzs @ ubuntu :~ $ Sudo apt-get install php5-dev |
Reading the package list... Complete
Analyzing the dependency tree of the software package
Reading status information... Complete
The following additional software packages will be installed:
Pkg-php-tools shtool
Recommended software packages:
Dh-make
The following [new] software packages will be installed:
Php5-dev pkg-php-tools shtool
0 software packages are upgraded, 3 new software packages are installed, 0 software packages are to be uninstalled, and 13 software packages are not upgraded.
The software package of 0 B/527 kB needs to be downloaded.
After decompression, 4,431 kB of extra space will be consumed.
Do you want to continue? [Y/n] y
...
Install OK: channel: // pecl.php.net/swoole-1.7.8
Configuration option "php_ini" is not set to php. ini location
You shoshould add "extension = swoole. so" to php. ini
Zzs @ ubuntu :~ $ Sudo vim/etc/php5/mod-available/swoole. ini
Add content
The code is as follows: |
Copy code |
; Configuration for php Swoole module Extension = swoole. so Php-m | grep swoole |
No swoole module
The code is as follows: |
Copy code |
Sudo ln-s/etc/php5/mod-available/swoole. ini/etc/php5/cli/conf. d/20-swoole.ini |
Use the following two lines as appropriate
The code is as follows: |
Copy code |
Sudo ln-s/etc/php5/mod-available/swoole. ini/etc/php5/apache2/conf. d/20-swoole.ini Sudo ln-s/etc/php5/mod-available/swoole. ini/etc/php5/fpm/conf. d/20-swoole.ini Php-m | grep swoole Swoole |
Appendix php5 configuration file description
Cd/etc/php5/
Ls
Apache2 cli fpm mod-available
There are four directories in my system.
The real extension is in the directory of the mod-available. It is advantageous for other environments to enable the extension to take effect by using soft links in ubuntu, when the real configuration file changes, you only need to change one location (modo-available.
Test extension
Server. php:
The code is as follows: |
Copy code |
<? Php $ Serv = new swoole_server ("maid", 9501 ); $ Serv-> on ('connect ', function ($ serv, $ fd ){ Echo "Client: Connect. \ n "; }); $ Serv-> on ('receive ', function ($ serv, $ fd, $ from_id, $ data ){ $ Serv-> send ($ fd, 'swoole: '. $ data ); }); $ Serv-> on ('close', function ($ serv, $ fd ){ Echo "Client: Close. \ n "; }); $ Serv-> start (); ?>
|
Client. php:
The code is as follows: |
Copy code |
<? Php $ Client = new swoole_client (SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC ); $ Client-> on ("connect", function ($ cli ){ $ Cli-> send ("hello world \ n "); }); $ Client-> on ("receive", function ($ cli, $ data ){ Echo "Receive: $ data \ n "; }); $ Client-> on ("error", function ($ cli ){ Echo "connect fail \ n "; }); $ Client-> on ("close", function ($ cli ){ Echo "close \ n "; }); $ Client-> connect ('127. 0.0.1 ', 127, 9501 ); ?> |
Finally, I would like to thank the author rango for developing such a great extension for php and contributing to the open-source business. I hope php will become more and more versatile.