下載 swoole
方法1:首先下載swoole的源碼包
wget -c https://github.com/swoole/swoole-src/archive/v2.0.6.tar.gz
解壓:
tar -zxvf v2.0.6.tar.gz
方法2:
從 https://github.com/swoole/swoole-src/releases 下載
編譯&安裝
cd swoole-src-2.0.6/
1.使用phpize來生成php編譯配置
2.使用./configure 來做編譯配置檢測
3.make進行編譯,make install進行安裝
命令執(zhí)行如下:
(使用#find / -name phpize,查出phpize的路徑)
[root@php7 swoole-src-2.0.6]# /usr/local/php/bin/phpize
(使用#find / -name php-config,查出php-config的路徑)
[root@php7 swoole-src-2.0.6]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@php7 swoole-src-2.0.6]# make && make install
4.make install后,如果正確,會出現(xiàn)以下內(nèi)容
[root@php7 swoole-src-2.0.6]# make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20160303/
修改配置文件
要能夠使用該模塊,還需要在 php.ini 文件中添加該模塊。
extension=swoole.so
重啟服務
新增php模塊后,需要重新啟動nginx和php-fpm服務,才會生效。
最后通過php -m或phpinfo()來查看是否成功加載了swoole,
重啟nginx服務器
service nginx restart
重啟php-fpm
service php-fpm restart
查看swoole擴展
php -m
注意:
1.swoole_server只能用于php-cli環(huán)境,否則會拋出致命錯誤。
2.因此引入類/函數(shù)的php文件時必須要使用include_once或require_once,否會發(fā)生cannot redeclare function/class 的致命錯誤。
3.不要用exit()/die(),會導致worker進程退出。
4.PHP代碼中如果有異常拋出,必須在回調(diào)函數(shù)中進行try/catch捕獲異常,否則會導致工作進程退