下載 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 來做編譯配置檢測(cè)
3.make進(jìn)行編譯嵌器,make install進(jìn)行安裝
命令執(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后逃延,如果正確,會(huì)出現(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
重啟服務(wù)
新增php模塊后,需要重新啟動(dòng)nginx和php-fpm服務(wù),才會(huì)生效檩互。
最后通過php -m或phpinfo()來查看是否成功加載了swoole,
重啟nginx服務(wù)器
service nginx restart
重啟php-fpm
service php-fpm restart
查看swoole擴(kuò)展
php -m
注意:
1.swoole_server只能用于php-cli環(huán)境,否則會(huì)拋出致命錯(cuò)誤咨演。
2.因此引入類/函數(shù)的php文件時(shí)必須要使用include_once或require_once闸昨,否會(huì)發(fā)生cannot redeclare function/class 的致命錯(cuò)誤。
3.不要用exit()/die(),會(huì)導(dǎo)致worker進(jìn)程退出薄风。
4.PHP代碼中如果有異常拋出饵较,必須在回調(diào)函數(shù)中進(jìn)行try/catch捕獲異常,否則會(huì)導(dǎo)致工作進(jìn)程退