swoole 学习(一)安装

swoole 的安装与使用

环境要求

  1. 系统: Linux, FreeBSD or MacOS

  2. Linux 内核版本 >= 2.3.32

  3. PHP 版本 >= 5.3.10

  4. GCC 版本 >= 4.4 or Clang

  5. Cmake 版本 >= 2.4 (Cmake需要将libswoole编译为C / C ++库)

推荐Linux版本:Ubuntu 14,CentOS 7或更高版本

PHP版本依赖

  1. Swoole仅支持PHP版本> = 5.3.10,推荐使用PHP 5.4+
  2. Swoole不依赖于stream,sockets,pcntl,posix,sysvmsg等扩展。只需安装PHP的基本扩展。

安装

源码安装

下载 swoole 最新稳定版本

https://github.com/swoole/swoole-src/releases

cd swoole          #  进入 swoole 源码目录  
phpize             #  为PHP扩展准备编译环境
./configure        #  根据需要添加配置参数
make               #  编译
sudo make install  #  安装 swoole 到 php 扩展目录

我的测试环境编译参数 ./configure --with-php-config=PATH --enable-swoole-debug --enable-trace-log --enable-sockets --enable-openssl --enable-swoole --with-swoole --enable-coroutine --enable-thread --enable-debug

编译可选参数

 --disable-option-checking  ignore unrecognized --enable/--with options
  --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
  --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
  --without-PACKAGE       do not use PACKAGE (same as --with-PACKAGE=no)
  --with-libdir=NAME      Look for libraries in .../NAME rather than .../lib
  --with-php-config=PATH  Path to php-config php-config
  --enable-swoole-debug   Enable swoole debug
  启用swoole的调试日志。不要在生产环境中启用此配置。
  --enable-trace-log   Enable swoole trace log
  --enable-sockets        Do you have sockets extension?
  启用对 sockets 的支持。依赖 sockets
  --enable-async-redis    Do you have hiredis?
  启用对异步Redis客户端的支持。依赖 hiredis
  --enable-coroutine-postgresql    Do you install postgresql?
  启用协程 Postgresql 客户端,依赖 libpq
  --enable-openssl        Use openssl?
  启用openssl支持。依赖 libssl.so
  --enable-http2          Use http2.0?
  启用HTTP2的支持。依赖 nghttp2
  --enable-thread         Experimental: Use thread?
  启用线程支持 //实验性功能。请勿在生产环境中使用此功能
  --enable-hugepage       Experimental: Use hugepage?
  启用 hugepage //使用大内存页优化性能,具体鸟哥在他的博客中讲到。
  如果已经开启了 jemalloc,再开启hugepage 印象性能
  https://blog.digitalocean.com/transparent-huge-pages-and-alternative-memory-allocators/
  实验性功能。请勿在生产环境中使用此功能
  --enable-swoole         Enable swoole support
  --enable-swoole-static    Enable swoole static compile support
  --with-swoole           With swoole support
  --with-libpq-dir=DIR    Include libpq support (requires libpq >= 9.5)
  --with-openssl-dir=DIR    Include OpenSSL support (requires OpenSSL >= 0.9.6)
  设置openssl库的路径,例如:--with-openssl-dir=/opt/openssl/.
  --with-jemalloc-dir=DIR    Include jemalloc support
  使用 jemalloc 进行内存优化支持
  --enable-mysqlnd       Do you have mysqlnd?
  启用对 mysqlnd 的支持,依赖 mysqlnd
  --enable-coroutine      Enable coroutine (requires PHP >= 5.5)
  启用协程
  --enable-asan      Enable asan
  启用 Address-Sanitizier 内存检测工具 //只有开启debug才有效
  --enable-picohttpparser     Experimental: Do you have picohttpparser?
  启用 picohttpparser 支持 //这是一个超高性能的http解析器,实验性功能。请勿在生产环境中使用此功能
  --enable-timewheel     Experimental: Enable timewheel heartbeat?
  启用时间轮算法并优化心跳算法 //实验性功能。请勿在生产环境中使用此功能
  --enable-debug,  compile with debug symbols
  编译时加入符号表 //使用gdb调试时有用
  --enable-shared=PKGS    Build shared libraries default=yes
  --enable-static=PKGS    Build static libraries default=yes
  --enable-fast-install=PKGS
                          Optimize for fast installation default=yes
  --with-gnu-ld           Assume the C compiler uses GNU ld default=no
  --disable-libtool-lock  Avoid locking (might break parallel builds)
  --with-pic              Try to use only PIC/non-PIC objects default=use both
  --with-tags=TAGS        Include additional configurations automatic

二进制版本安装

Linux用户

pecl install swoole

安装可选参数默认 NO

enable debug/trace log support?
enable sockets supports?
enable openssl support?
enable http2 support?
enable async-redis support? 
enable mysqlnd support?
enable postgresql coroutine client support?
MacOS X(macOS)用户

推荐使用 homebrew 安装

brew install swoole

使用

安装完成后需要在 php.ini 中加入扩展 extension=swoole.so,才能使用

php -i | grep php.ini                      # 检查 php.ini 所在目录
sudo echo "extension=swoole.so" > php.ini  # 添加 extension=swoole.so 到 php.ini 到最后一行
php -m | grep swoole                       # 检查 swoole 扩展是否已经被激活

使用 PhpStorm 开发swoole

默认 PhpStorm 不支持对于 swoole 的代码提示,需要下载对应的代码提示库

https://github.com/eaglewu/swoole-ide-helper

然后在 Preferences -> Languages & Frameworks -> PHPInclude Path 中引入代码提示库

现在就可以正常使用自动补全提示了