编译安装

Swoole扩展是按照PHP标准扩展构建的。使用phpize来生成编译检测脚本,./configure来做编译配置检测,make进行编译,make install进行安装。

  • 请下载releases版本的swoole,直接从github主干上拉取最新代码可能会编译不过
  • 如无特殊需求, 请务必编译安装swoole的最新版本
  • 如果当前用户不是root,可能没有PHP安装目录的写权限,安装时需要sudo或者su
  • 如果是在git分支上直接git pull更新代码,重新编译前务必要执行make clean

安装准备

安装swoole前必须保证系统已经安装了下列软件

php-5.3.10 或更高版本
gcc-4.4 或更高版本
make
autoconf
pcre (centos系统可以执行命令:yum install pcre-devel)

下载地址

下载源代码包后,在终端进入源码目录,执行下面的命令进行编译和安装

新手编译示例

cd swoole
sudo phpize (ubuntu 没有安装phpize可执行命令:sudo apt-get install php-dev来安装phpize)
sudo ./configure
sudo make 
sudo make install

进阶完整编译示例

初次接触swoole的开发者请先尝试上方的简单编译

以下脚本会下载并编译master分支的swoole源码, 需保证你已安装所有依赖, 否则会遇到各种依赖错误

mkdir -p ~/build && \
cd ~/build && \
rm -rf ./swoole-src && \
curl -o ./tmp/swoole.tar.gz https://github.com/swoole/swoole-src/archive/master.tar.gz -L && \
tar zxvf ./tmp/swoole.tar.gz && \
mv swoole-src* swoole-src && \
cd swoole-src && \
phpize && \
./configure \
--enable-coroutine \
--enable-openssl  \
--enable-http2  \
--enable-async-redis \
--enable-sockets \
--enable-mysqlnd && \
make clean && make && sudo make install

PECL

swoole项目已收录到PHP官方扩展库,除了手工下载编译外,还可以通过PHP官方提供的pecl命令,一键下载安装swoole

pecl install swoole

配置php.ini

编译安装成功后,修改php.ini加入

extension=swoole.so

通过php -mphpinfo()来查看是否成功加载了swoole,如果没有可能是php.ini的路径不对,可以使用php --ini来定位到php.ini的绝对路径。


  • 夜幕林

    建议对于编译时使用了--enable-swoole-debug,在初始化服务的时候可以set 是否debug。不然有时候开发的时候,打印的数据都太多。

  • 心的二分之1

    load swoole.so,重启lnmp的时候 报错了 PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/swoole.so 话说编译完成后,.so文件的位置是什么呢

  • 田雨

    php5.6,已经启用了sockets ./configure --enable-sockets --with-php-config=/usr/local/php6/bin/php-config --enable-swoole-debug -enable-async-mysql --enable-ringbuffer make的时候就会出现 Enable sockets support, But no sockets extension

  • 瞿瞿Green_yy

    谢谢提醒,不过应该是源码编译swoole的时候,不是mysql

  • 阮刚

    添加-enable-swoole-debug编译选项后报错,如下: /root/swoole-src-master/src/factory/FactoryProcess.c: 在函数‘swFactoryProcess_finish’中: /root/swoole-src-master/src/factory/FactoryProcess.c:764: 错误:‘sdata’未声明(在此函数内第一次使用) /root/swoole-src-master/src/factory/FactoryProcess.c:764: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其 /root/swoole-src-master/src/factory/FactoryProcess.c:764: 错误:所在的函数内也只报告一次。) make: *** [src/factory/FactoryProcess.lo] 错误 1

  • neil

    我也遇到这个问题了,请问如何解决?

  • 谁伴我闯荡

    真不明白为何非要搞Windows的,搞个SAMBA协议,装个Linux虚拟机,共享下开发目录,一样的本地调试!

  • lengchun

    [root@lengchun swoole-src-swoole-1.7.16-stable]# LDFLAGS="-L/opt/mysql/lib" CPPFLAGS="-I/opt/mysql/include" ./configure --with-php-config=/opt/php54/bin/php-config --enable-async-mysql=/opt/mysql/include --enable-swoole --with-swoole --enable-openssl --enable-ringbuffer --enable-sockets=/opt/php54/include/php/ext/sockets --enable-swoole-debug

  • 曹

    需要gcc4.4版本及以上

  • 陈江飞

    hecking for ext/mysqli support... PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/swoole.so'

  • shuqi888

    FreeBSD 10.2 x64版本,PHP 5.6环境下用从官网下载的源码解压安装失败,后来通过/usr/ports/devel/pecl-swoole目录make install成功

  • 石长春

    要停止正在运行的swoole服务

  • 米天大汤

    php7 php5.5两个版本共存,分别安装swoole,现在php5.5的成功,7的extensions目录下也有swoole.so,php.ini也添加了extension=swoole.so,重启了php7-fpm,php -m还是没有,swoole 1.8.4 , pecl方式也安装了,想过一样。[衰]

  • 米天大汤

    在常见错误里面找到原因和解决方案了。多谢

  • 郝多

    cygwin下编译swoole 1.8.4-stable,居然缺少pcre.h文件: make报错: /cygdrive/c/Users/modian/Downloads/swoole-src-swoole-1.8.4-stable/swoole-src-swoole-1.8.4-stable/include/swoole.h: 在函数‘swoole_strtolower’中: /cygdrive/c/Users/modian/Downloads/swoole-src-swoole-1.8.4-stable/swoole-src-swoole-1.8.4-stable/include/swoole.h:1034:9: 警告:数组下标类型为‘char’ [-Wchar-subscripts] c = tolower(c); ^ In file included from /usr/include/php/ext/spl/spl_iterators.h:27:0, from /cygdrive/c/Users/modian/Downloads/swoole-src-swoole-1.8.4-stable/swoole-src-swoole-1.8.4-stable/swoole.c:28: /usr/include/php/ext/pcre/php_pcre.h: 在文件作用域: /usr/include/php/ext/pcre/php_pcre.h:29:18: 致命错误:pcre.h:No such file or directory #include "pcre.h" ^ 编译中断。 Makefile:180: recipe for target 'swoole.lo' failed make: *** [swoole.lo] Error 1

  • 郝多

    最新: swoole-src-swoole-1.8.5-beta 版本下,cygwin可以完成make:

    libtool: install: cp ./.libs/swoole.lai /cygdrive/c/Users/modian/Downloads/swoole-src-swoole-1.8.5-beta/swoole-src-swoole-1.8.5-beta/modules/swoole.la

    Build complete. Don't forget to run 'make test'

  • 朱荣

    今天刚好解决 在centos 编译swoole 1.8.4 --enable-async-mysql 具体步骤是 yum install mysql mysql-devel ,不是前面提到的 安装mysqlclient ./configure --enable-async-mysql=/usr/include/mysql

  • jepson

    你好 我发现 1.5版本之后 configure都没有 使用--enable-async-mysql这个参数了

  • ming

    服务器没有联网,不能使用pecl upgrade swoole在线更新。请问1.8.5stable怎么通过源码编译方式升级到1.8.8stable?

  • ming

    刚才直接下载了1.8.8-stable,tar.gz,解压后通过phpize方式,安装的1.8.8,重启了php-fpm,kill掉了原来的swoole server后还是原来的版本号1.8.5,请问是哪一步没有做造成的?

  • ming

    已经OK,重新编译安装后会生成新的swoole.so文件,直接将该文件覆盖之前的swoole.so文件即可。

  • 昌维

    我也是这样 我是ubuntu14 64bit,检查了其他组件版本都没问题

  • Rango-韩天峰

    已经改为内置了,不需要这个编译参数。

  • allen

    centos PHP7 ,环境已安装成功。测试通过。总体来说,安装比较简单。[嘻嘻]

  • 彭涛

    安装完后,php -m没有swoole啊,用了两种方法安装都没有,修改php.in也是没用

  • 李军锋

    php server.php 这个事系统自带的php /Applications/XAMPP/bin/php server.php server.php 这个php有swoole扩展的

  • 休闲时间

    今天部署PHP多版本共存,遇到些问题,在编译安装swoole时,./configure需要一些参数,完整如下: ./configure --with-php-config=/usr/local/php7/bin/php-config 后边路径请替换成自己的实际路径

  • huanganxin

    debian:~/phpcode$ php -f d.php [Thu Jan 12 22:44:34 2017] Script: '/home/eric/phpcode/d.php' /home/eric/software/php-7.1.0/Zend/zend_string.h(122) : Freeing 0xb6a011b8 (20 bytes), script=/home/eric/phpcode/d.php Last leak repeated 7 times [Thu Jan 12 22:44:34 2017] Script: '/home/eric/phpcode/d.php' /home/eric/software/swoole/php7_wrapper.h(310) : Freeing 0xb6a012f8 (16 bytes), script=/home/eric/phpcode/d.php [Thu Jan 12 22:44:34 2017] Script: '/home/eric/phpcode/d.php' /home/eric/software/php-7.1.0/Zend/zend_API.c(1060) : Freeing 0xb6a57280 (44 bytes), script=/home/eric/phpcode/d.php [Thu Jan 12 22:44:34 2017] Script: '/home/eric/phpcode/d.php' /home/eric/software/php-7.1.0/Zend/zend_closures.c(463) : Freeing 0xb6a5c460 (192 bytes), script=/home/eric/phpcode/d.php [Thu Jan 12 22:44:34 2017] Script: '/home/eric/phpcode/d.php' /home/eric/software/php-7.1.0/Zend/zend_hash.c(138) : Freeing 0xb6a5e500 (224 bytes), script=/home/eric/phpcode/d.php === Total 12 memory leaks detected ===

    php版本:7.1 swoole版本:2.0.5

    d.php 内容: <?php $db = new Swoole\MySQL; $server = array( 'host' => 'localhost', 'user' => 'root', 'password' => '', 'database' => 'kfb_a', );

    $db->connect($server, function ($db, $result) { $db->query("show tables", function (Swoole\MySQL $db, $result) { if ($result === false) { var_dump($db->error, $db->errno); } elseif ($result === true) { var_dump($db->affected_rows, $db->insert_id); } else { var_dump($result); $db->close(); } }); });

  • Lenix

    cd swoole phpize ./configure --enable-sockets=/usr/include/php/20151012/ext/sockets
    make sudo make install

  • 瀚海

    完全同样的错误 我的解决方式是把make & make install 分开 就不再报错了

  • 宁静致远

    PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/swoole.so' - /usr/lib64/php/modules/swoole.so: undefined symbol: COROG in Unknown on line 0

  • 宁静致远

    安装成功,加载不成功

  • PSB

    文档中的 php -m 能看到swoole 但是在phpinfo()探针函数中看不到swoole扩展模块

  • gglinux/db

    PHP下面的安装,大家可以参考下:http://gglinux.com/2017/04/01/php7_install/

  • 夏至冰当融

    phpinfo中有swoole了,但是php-m中看不到.定位到php.ini,添加了swoole.so模块,就提示 PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/swoole.so' - /usr/lib64/php/modules/swoole.so: undefined symbol: zend_new_interned_string in Unknown on line 0

  • 问天涯

    使用pecl安装成功

  • 问天涯

    yum install php

    yum install php-pear yum install php-devel pecl install swoole vi /etc/php.ini php -m|grep swoole

  • 战神悟空

    出现错误: PHP Warning: PHP Startup: Unable to load dynamic library '/webserver/php/lib/php/extensions/no-debug-non-zts-20131226/swoole.so' - /webserver/php/lib/php/extensions/no-debug-non-zts-20131226/swoole.so: cannot open shared object file: No such file or directory in Unknown on line 0

    解决方案: 直接创建目录,重新安装即可

  • 爱在拜城_sunbea

    我没有执行这个cd swoole phpize ./configure make sudo make install 直接将swoole扩展安装好了 ,没得问题吧?

  • cjj

    安装的时候依赖 pcre, centos yum install pcre-devel

  • rhythm

    安装完毕!

  • 月半文一刀

    有个命令,php --ini能打印加载的所有ini配置文件

  • Lcoper

    make时报错 In file included from /usr/include/sys/mman.h:25:0, from /usr/local/swoole-2.1.0/include/swoole.h:65, from /usr/local/swoole-2.1.0/php_swoole.h:48, from /usr/local/swoole-2.1.0/swoole_trace.c:17: /usr/local/swoole-2.1.0/swoole_trace.c: In function ‘trace_dump’: /usr/local/swoole-2.1.0/swoole_trace.c:461:47: error: ‘zend_execute_data’ has no member named ‘function_state’ if (0 > trace_get_long(execute_data + offsetof(zend_execute_data, function_state.function), &l)) ^ /usr/local/swoole-2.1.0/swoole_trace.c:482:47: error: ‘zend_execute_data’ has no member named ‘op_array’ if (0 > trace_get_long(execute_data + offsetof(zend_execute_data, op_array), &l)) ^ make: *** [swoole_trace.lo] Error 1

    PHP版本是7.0.0的 ,版本太低?

  • 奥想瓷焅

    这样写对 swoole_timer_tick(3000, function () { echo "after 3000ms.\n"; swoole_timer_after(14000, function () { echo "after 14000ms.\n"; });

    }); 这样写不对

  • heydurex

    make时报错 /usr/local/opt/swoole/src/os/sendfile.c:23:5: error: conflicting types for 'swoole_sendfile' int swoole_sendfile(int out_fd, int in_fd, off_t *offset, off_t size) ^ /usr/local/opt/swoole/include/swoole.h:2166:5: note: previous declaration is here int swoole_sendfile(int out_fd, int in_fd, off_t *offset, size_t size); 请问是什么原因呢

  • 旧城半夏

    为什么我安装出现这样的 [root@iZmrxuum7vge1gZ ~]# pecl install swoole pecl/swoole requires PHP (version >= 7.0.0), installed version is 5.5.7 No valid packages found install failed 不是PHP最低版本php-5.3.10吗?我的是5.5.7

  • 拒绝

    php
    

  • p
    p

    In file included from /opt/remi/php72/root/usr/include/php/Zend/zend.h:328:0, from /opt/remi/php72/root/usr/include/php/main/php.h:35, from /data/apps/swoole4/swoole-src/php_swoole.h:22, from /data/apps/swoole4/swoole-src/swoole_lock.c:17: /opt/remi/php72/root/usr/include/php/Zend/zend_operators.h: In function 'fast_long_increment_function': /opt/remi/php72/root/usr/include/php/Zend/zend_operators.h:446:2: warning: implicit declaration of function '__builtin_saddl_overflow' [-Wimplicit-function-declaration] if (UNEXPECTED(__builtin_saddl_overflow(Z_LVAL_P(op1), 1, &lresult))) { ^ /opt/remi/php72/root/usr/include/php/Zend/zend_operators.h: In function 'fast_long_decrement_function': /opt/remi/php72/root/usr/include/php/Zend/zend_operators.h:500:2: warning: implicit declaration of function '__builtin_ssubl_overflow' [-Wimplicit-function-declaration] if (UNEXPECTED(__builtin_ssubl_overflow(Z_LVAL_P(op1), 1, &lresult))) { 这是什么问题啊

  • p
    p

    PHP 7.2.6 (cli) (built: May 23 2018 08:56:04) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

    In file included from /opt/remi/php72/root/usr/include/php/Zend/zend.h:328:0, from /opt/remi/php72/root/usr/include/php/main/php.h:35, from /data/apps/swoole4/swoole-src/php_swoole.h:22, from /data/apps/swoole4/swoole-src/swoole_lock.c:17: /opt/remi/php72/root/usr/include/php/Zend/zend_operators.h: In function 'fast_long_increment_function': /opt/remi/php72/root/usr/include/php/Zend/zend_operators.h:446:2: warning: implicit declaration of function '__builtin_saddl_overflow' [-Wimplicit-function-declaration] if (UNEXPECTED(__builtin_saddl_overflow(Z_LVAL_P(op1), 1, &lresult))) { ^ /opt/remi/php72/root/usr/include/php/Zend/zend_operators.h: In function 'fast_long_decrement_function': /opt/remi/php72/root/usr/include/php/Zend/zend_operators.h:500:2: warning: implicit declaration of function '__builtin_ssubl_overflow' [-Wimplicit-function-declaration] if (UNEXPECTED(__builtin_ssubl_overflow(Z_LVAL_P(op1), 1, &lresult))) { 这是什么问题啊

  • 国庆七天乐
  • 国庆七天乐

    在centos7和树莓派(类似ubuntu server)都完整编译安装了swoole,有需要的或没安装成功的可以参考一下

  • 曾四逗

    In file included from ./php_swoole.h:38: ./include/swoole.h:70:5: error: conflicting types for 'clock_gettime' int clock_gettime(clock_id_t which_clock, struct timespec *t); 有谁遇到这种报错吗

  • 将军

    用pecl install swoole 安装后,修改了php.ini。php -m没有swoole,nable to load dynamic library 'swoole.so' (tried: /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/swoole.so (libnghttp2.so.14: cannot open shared object file: No such file or directory), /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/swoole.so.so (/usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/swoole.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0

  • Aaron-森

    使用 pecl install swoole 安装swoole 可能会出现 Unable to load dynamic library '/usr/lib64/php/modules/swoole.so' - /usr/lib64/php/modules/swoole.so: undefined symbol: 解决办法 产看php安装的扩展 如果安装了mysqli 先卸掉,重新安装mysqlnd 第二步查看这个地址 https://blog.csdn.net/qq292913477/article/details/81985824 按照这个流程基本上就能解决

  • Breeze

    安装成功了 php.ini 也打开了扩展 但是 php -m 可以看到 phpinfo找不到

  • 命运的阳光

    如果出现 libhiredis.so.0.13: cannot open shared object file: No such file or directory 说明hiredis动态链接库没找到,保证已经安装了hiredis之后执行以下命令: mkdir /usr/lib/hiredis cp libhiredis.so /usr/lib/hiredis mkdir /usr/include/hiredis cp hiredis.h /usr/include/hiredis echo '/usr/local/lib' >>/etc/ld.so.conf ldconfig

    centos7,php7.2,hiredis0.13.3,测试通过

  • 佘航

    $ uname -r