homestead(多PHP版本)安装swoole

看了网上的各种教程,编译安装还是Perl安装,不管哪种办法,都是各种报错。。。
我的环境是homestead,各种琢磨,总结一下,mmp

我的homestead下有n多个php版本

vagrant@homestead:/etc/alternatives$ ls /usr/bin/php
php            php7.0         php7.2         php-config5.6  php-config7.1  phpize         phpize7.0      phpize7.2      
php5.6         php7.1         php-config     php-config7.0  php-config7.2  phpize5.6      phpize7.1      

是的,有这么多,我自己都怕
看一下当前版本,php -v

PHP 7.1.10-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Sep 29 2017 17:04:25) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.10-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies
    with blackfire v1.18.0~linux-x64-non_zts71, https://blackfire.io, by SensioLabs

当前是7.1的,开始安装swoole,直接用Perl 安装,去他妈的编译安装

$ sudo pecl channel-update pecl.php.net
$ sudo pecl install swoole

安装的时候会问你yes/no之类的选项,我全部enter下去,经过漫长的等待。。。

Build process completed successfully
Installing '/usr/include/php/20170718/ext/swoole/config.h'
Installing '/usr/lib/php/20170718/swoole.so'
install ok: channel://pecl.php.net/swoole-4.2.1
configuration option "php_ini" is not set to php.ini location
You should add "extension=swoole.so" to php.ini

看到有success我就放心了,上面提示了,你应该添加 extension=swoole.so 到php.ini文件,php.ini文件在哪呢,用这条命令就可以了php -i|grep php.ini

vagrant@homestead:~$ php -i|grep php.ini
Configuration File (php.ini) Path => /etc/php/7.1/cli
Loaded Configuration File => /etc/php/7.1/cli/php.ini

vim编辑一下 /etc/php/7.1/cli/php.ini,差不多在这个位置

;extension=xsl
extension=swoole.so  //  加到这里哦亲 

;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;

然后,重启一下php,fpm有php7.1-fpm php7.2-fpm还有其他等等,要哪个呢,php -v看下版本,就用对应的版本就可以了,我当前是7.1,就用php7.1-fpm,好了,跑起来

sudo service php7.1-fpm reload

然后,看一下效果,php -m看看有没有

php -m |grep swoole

没有就算了,还多了个报错。。。

PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib/php/20160303/swoole.so' - /usr/lib/php/20160303/swoole.so: cannot open shared object file: No such file or directory in Unknown on line 0

实在是很气,提示找不到文件,看一下这个扩展extension的路径好像有点不对,回到刚才swoole安装完打印的消息

Installing '/usr/include/php/20170718/ext/swoole/config.h'
Installing '/usr/lib/php/20170718/swoole.so'

20160303跟20170718,这特么明显对不上啊,swoole怎么会安装到哪里去了咧,看一下php extension目录,执行php-config或者直接php-config –extension-dir

vagrant@homestead:~$ php-config --extension-dir
/usr/lib/php/20170718

那20160303是怎么来的呢,奇了个怪,再看php -i|grep extension

vagrant@homestead:~$ php -i| grep extension
extension_dir => /usr/lib/php/20160303 => /usr/lib/php/20160303
mbstring extension makes use of "streamable kanji code filter and converter", which is distributed under the GNU Lesser General Public License version 2.1.
extension Version => 2.0.2
sqlite3.extension_dir => no value => no value

原来如此,怎么这两个extension目录不一样呢,看一下/usr/lib/php/目录有哪些

vagrant@homestead:~$ ls /usr/lib/php/
20131226  20160303  5.6  7.1  php5.6-fpm-reopenlogs  php7.1-fpm-reopenlogs  php-helper              sessionclean
20151012  20170718  7.0  7.2  php7.0-fpm-reopenlogs  php7.2-fpm-reopenlogs  php-maintscript-helper

我猜7.1是对应20160303,7.2是对应20170718的,那想办法把7.1换成7.2试一下,切换php版本,参考了这位网友的回答
请教一下 vagrant laravel Homestead 怎么切换 PHP 版本呀?

cd /etc/alternatives
sudo mv php php.bak
sudo ln -s /usr/bin/php7.2 php

然后再看一眼版本,php -v

vagrant@homestead:~$ php -v
PHP 7.2.0RC3 (cli) (built: Sep 28 2017 16:28:14) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0-dev, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.2.0RC3, Copyright (c) 1999-2017, by Zend Technologies
    with blackfire v1.18.0~linux-x64-non_zts72, https://blackfire.io, by SensioLabs

顺利改过来了,并不简单,再执行

// 重启一下
sudo service php7.2-fpm reload
php -m |grep swoole

还是上面的报错。。。。这里还少了一步,再看一眼php -i|grep php.ini

vagrant@homestead:~$ php -i |grep php.ini
Configuration File (php.ini) Path => /etc/php/7.2/cli
Loaded Configuration File => /etc/php/7.2/cli/php.ini

此时的ini已经不是彼时的ini,刚才是7.1目录下的,现在切换版本变成了7.2目录下的,vim编辑 /etc/php/7.2/cli/php.ini 把刚才的extension=swoole.so再加上去,加完了别忘了重启

sudo service php7.2-fpm reload

重启完了,再 php -m |grep swoole 这下子终于出来了

vagrant@homestead:~$ php -m |grep swoole
swoole

一把辛酸泪,折腾了大半天终于整出来了

    原文作者:一阵风fly
    原文地址: https://www.jianshu.com/p/b868a247a1a9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞