swoole源码安装与配置数详解

一、编译安装swoole

1、下载源代码

源代码下载地址:

  • 官网(也是一个索引,注明下载地址,不提供下载服务)
  • github(最新的版本都在github)
  • 码云(国内镜像)
  1. wget https://github.com/swoole/swoole-src/archive/v2.1.3.tar.gz
2、解压源码包
  1. tar -zxvf v2.1.3.tar.gz
3、配置
  1. phpize
  2. ./configure --with-php-config=/usr/local/php/bin/php-config --enable-swoole-debug --enable-trace-log --enable-sockets --enable-async-redis --enable-openssl --enable-http2 --enable-coroutine --enable-coroutine-postgresql --enable-mysqlnd

注:

  • phpize 用于生成configure配置脚本文件(如果phpize不在环境变量PATH中,需要通过别名(alias)、软链接(ln -s)、全路径(如usr/local/php/bin/phpize)或者将php的bin目录加入到环境变量PATH中
  • 通过/configure --help以查看其配置参数
    • 功能(特性)开关配置参数:以--enable--disable开头的配置参数
    • 指定第三方模块或类库的路径的配置参数:以--with--without开头
(1)主要参数详解
  • --enable-swoole-debug : 是否启用swoole的调试(建议在开发环境启用,生产环境禁用)
  • --enable-trace-log :打开调试日志,开启此选项后swoole将打印各类细节的调试日志(建议在开发环境启用,生产环境禁用)
  • --enable-sockets : 增加对sockets资源的支持。开启此参数,swoole_event_add就可以添加sockets扩展创建的连接到swoole的事件循环中。另外Server和Client的getSocket()方法也需要依赖此编译参数(需要安装php的socket扩展才能启用)
  • --enable-async-redis : 是否启用异步redis模块(需要安装hiredis类库)
  • --enable-openssl:启用SSL支持(如HTTPS的支持),一般来说,都是通过nginx转发请求,因此,在大多数场景下,不需要开启该选项(需要系统有libssl类库)
  • --enable-http2:是否启用HTTP2(该特性需要系统已有nghttp2类库)
  • --enable-coroutine:是否启用swoole协程
  • --enable-coroutine-postgresql : 是否启用协程Postgresql客户端(依赖libpq的类包)
  • --enable-timewheel:启用时间轮算法,优化心跳检测性能(该特性目前是测试阶段,请不要在生产环境使用)
  • --enable-mysqlnd:启用mysqlnd支持,启用swoole_mysql::escapse方法。启用此参数后,PHP必须有mysqlnd模块,否则会导致swoole无法运行(php必须启用mysqlnd,在编译php时指定)
  • --enable-ringbuffer:开启RingBuffer内存池(该特性还处于测试阶段,请不要在生产环境使用)
  • --enable-thread:是否启用多线程(该特性处于测试阶段,不要在生产环境使用)
  • --with-libdir:指定类库包搜索的默认路径
  • --with-openssl-dir:指定openssl的安装路径(在启用openssl特性的时候使用)
  • --with-libpq-dir:指定postgresql类库的路径(在启用pg协程客户端时使用)
(2)相关模块依赖包编译(安装)
  • 异步redis依赖类包hiredis的安装
    第一步:下载编译安装hiredis包
    1. wget https://github.com/redis/hiredis/archive/v0.13.3.tar.gz
    2. tar -zxvf v0.13.3.tar.gz
    3. cd hiredis-0.13.3
    4. make
    5. make install
    6. ldconfig
    第二步:移动相关头文件和共享库文件

它将头文件放于/usr/local/include中,将共享库文件放于/usr/local/lib中

  1. cd hiredis-0.13.3
  2. mv *.h /usr/local/include
  3. mv *.so /usr/local/lib
  4. ldconfig
  • openssl依赖包libssl包编译
    • 第一种方式:通过yum或apt-get安装
      针对ubuntu

      1. sudo apt-get install libssl-dev

      针对CentOS

      1. yum install openssl-devel
    • 第二种方式:通过指定openssl源码目录

      1. wget https://www.openssl.org/source/openssl-1.1.0h.tar.gz
      2. tar -zxvf openssl-1.1.0h.tar.gz
      3. cd openssl-1.1.0h
      4. ./configure --with-php-config=/usr/local/php/bin/php-config --enable-swoole-debug --enable-trace-log --enable-sockets --enable-async-redis --enable-openssl --enable-http2 --enable-coroutine --enable-coroutine-postgresql --enable-mysqlnd --with-openssl-dir=/usr/local/src/openssl-1.1.0h
  • http2的依赖包nghttp2包编译
    第一步: 编译安装nghttp2类库,生成共享库
  1. wget https://github.com/nghttp2/nghttp2/releases/download/v1.30.0/nghttp2-1.30.0.tar.gz
  2. tar -zxvf nghttp2-1.30.0.tar.gz
  3. cd nghttp2-1.30.0
  4. ./configure
  5. make
  6. make install
  7. ldconfig

  第二步: 一般它将libnghttp2.so共享库文件放于/usr/local/lib上,需要我们手动将它加入系统自动扫描共享库的目录中,在/etc/ld.so.conf中加入一行:/usr/local/lib

  第三步:再通过ldconfig重新加载即可

  • postgresql协程客户端依赖包libpq的编译
    • 第一种方式:通过yum或apt-get install来安装

    ubuntu系统

    1. sudo apt-get install libpq-dev

     centOS等

    1. yum install postgresql-devel
    • 第二种方式:下载libpq源码,在配置Swoole中指定libpq的位置(libpq这个包在postgresql的源码包中的src/interfaces/libpq目录下)
    1. wget https://ftp.postgresql.org/pub/source/v10.3/postgresql-10.3.tar.gz
    2. tar -zxvf postgresql-10.3.tar.gz
    3. cd postgresql-10.3
    4. ls -al src/interfaces/libpq
    5. ./configure --with-php-config=/usr/local/php/bin/php-config --enable-swoole-debug --enable-trace-log --enable-sockets --enable-async-redis --enable-openssl --enable-http2 --enable-coroutine --enable-coroutine-postgresql --enable-mysqlnd --with-libpq-dir=/usr/local/src/postgresql-10.3/src/interfaces/libpq

4、编译安装

  1. make
  2. make install

5、修改php配置文件启用swoole

首先在确定php.ini文件的位置:

  1. php --ini

6、查看swoole扩展是否安装成功

  1. php -m | grep "swoole"

二、swoole配置参数(在php.ini中)说明

参数名称 含义 数据类型 默认值
swoole.aio_thread_num 设置AIO异步文件读写的线程数量 int 2
swoole.display_errors 是否展示Swoole错误信息 string On
swoole.unixsock_buffer_size 设置进程间通信的UnixSocket缓存区尺寸 int 8388608
swoole.fast_serialize 否使用swoole_serialize对异步任务数据序列化 string off
swoole.use_namespace 使用命名空间类风格 int off
swoole.use_shortname 是否启用短命名(如使用Co) String on