Docker搭建Swoole环境
$ docker-machine ls
$ docker-machine start default
$ docker-machine ssh default
$ docker-machine ip
$ docker ps -a
拉取镜像
docker@default:~$ docker pull twosee/swoole
docker@default:~$ docker pull twosee/swoole-coroutine
docker@default:~$ docker pull twosee/swoole-coroutine:mysql
docker@default:~$ docker pull twosee/swoole-coroutine:redis
查看镜像
docker@default:~$ docker images|grep swoole
twosee/swoole-coroutine latest d71166ef543b 2 months ago 833MB
twosee/swoole latest 36b1fc8cfa4d 2 months ago 833MB
twosee/swoole-coroutine redis f07dcda98890 5 months ago 83.5MB
twosee/swoole-coroutine mysql 43aac2677dd2 5 months ago 486MB
启动镜像
docker@default:~$ docker run -d --name=swoole \
-v /workdir:/workdir \
-p 9501:9501 \
twosee/swoole-coroutine \
php /app/server.php start
注意-v /workdir:/workdir
是需要挂载映射的工作目录,workdir
为你自己挂载的文件路径。
$ docker run -it -d -p 9701:9701 --name=myswoole twosee/swoole-coroutine /bin/bash
挂载了还几次都不成功,更换了端口再试试!
$ docker run -it -d -p 9701:9701 -v /share/swoole:/app --name=myswoole twosee/swoole-coroutine /bin/bash
$ docker ps -a | grep myswoole
539b086d15cc twosee/swoole-coroutine "docker-php-entrypoi…" 48 seconds ago Up 48 seconds 0.0.0.0:9701->9701/tcp myswoole
打开本地项目创建测试文件
$ vim /share/swoole/server.php
注意这里的端口使用的是9701与容器启动时保持一致
<?php
$ws = new swoole_websocket_server("0.0.0.0", 9701);
$ws->on("open", function($ws, $request){
$ws->push($request->fd, "connect success");
});
$ws->on("close", function($ws, $fd){
echo "fd: {$fd} close";
});
$ws->on("message", function($ws, $frame){
$ws->push($frame->fd, "server:".$frame->data);
});
$ws->start();
测试文件编写完毕后会同步到myswoole容器中映射的app
目录下
进入容器启动服务器脚本
$ docker exec -it myswoole bash
root@539b086d15cc:/# cd app
root@539b086d15cc:/# ls
root@539b086d15cc:/# php server.php start
服务器脚本运行成功后,可编写客户端脚本对其进行访问,首先需要确定容器的IP地址,之前是可以通过docker-machine ip
获取的,这里使用的是192.168.99.100。
$ vim client.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
var ws = new WebSocket("ws://192.168.99.100:9701");
ws.onmessage = function(evt){
console.log(evt.data);
};
</script>
</body>
</html>
客户端脚本可在本地服务器上开发,然后访问客户端脚本,并查看控制台输出信息。
connect success
编译安装Swoole扩展
- 操作系统:Ubuntu
- PHP版本:PHP5
准备工作
# 安装PHP
$ sudo apt-get install php5 php5-dev
# 安装编译环境,常见三种编译器gcc/g++/clang
$ sudo apt-get install gcc autoconf
# 修改编译器
$ vi makefile
CC = clang
# 删除已编译好的文件重新编译
$ make clean && make
编译源码
phpize
是Shell脚本,主要用于调用autoconf
提供的工具,生成configure
文件。 cmake
和autoconf
功能类似,出现要比autocof
晚一些。
# 使用cmake生成lib/libswoole.so的动态链接库
$ vim CMakeLists.txt
# 生成makefile
$ cmake
启用多线程编译,加快编译速度。
# 启用多线程编译
$ make -j
低版本Linux内核不支持-j
参数。注意的是-j
是危险操作,若占用所有进程会造成系统资源耗尽而死机。
下载Swoole源代码
# 下载代码
$ git clone https://github.com/swoole/swoole-src/releases
$ cd swoole-src
编译源码
phpize
是PHP官方自带的用于生成configure
编译脚本的工具,它会根据php.m4
配置生成configure
脚本。
$ phpize
查看脚本参数
$ .configure --help
生成makefile
,makefile
是make
的配置文件,用来执行编译配置。configure
会检测操作系统的差异,Linux2.6.27分界线。
$ .configure
查看Linux系统内核版本
$ uname -a
编译,将.c
文件生成.o
文件,然后连接生成swoole.so
。
$ make
安装
$ make install
查询 php.ini
文件地址
$ php -i | grep php.ini
修改php.ini
配置文件并加入Swoole扩展
$ vim php.ini
extension=swoole.so
查看PHP扩展
$ php -m | grep swoole
测试
$ php examples/server.php
相关工具
-
ldconf
工具用于重新生成配置
$ ls /etc/ld.so.conf.d/
$ ldconfig
-
nm
工具可查看源码中定义的函数
$ nm libswoole.so
Ubuntu安装配置Swoole
操作系统:Ubuntu
查看Linux操作系统内核版本
$ uname --all
Linux junchow 4.15.0-46-generic #49-Ubuntu SMP Wed Feb 6 09:33:07 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
安装GCC编译器
$ apt install gcc
$ gcc --version
gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
安装Cmake工具
$ apt install cmake
$ cmake --version
cmake version 3.10.2
安装PHP
$ apt install php
$ php -v
PHP 7.2.15-0ubuntu0.18.04.2 (cli) (built: Mar 22 2019 17:05:14) ( NTS )
安装php-pear
$ apt install php-pear
使用pecl安装swoole
$ pecl install swoole
No releases available for package "pecl.php.net/swoole"
$ pear clear-cache
$ pear update-channels
$ pear upgrade
如果使用pecl install swoole
无法成功,可进入pecl官网搜索 swoole 包,下载后本地安装 。
- pecl官网地址 https://pecl.php.net/package/swoole
- swoole下载地址 https://pecl.php.net/get/swoole-4.3.1.tgz
通过swoole install
进行在线安装
# 安装包,包括下载编译安装
$ pecl install https://pecl.php.net/get/swoole-4.3.1.tgz
# 下载源码包
$ pecl download swoole
查看php是否已经安装 swoole 扩展
$ php --ri swoole
查看php配置文件位置
$ php -i | grep php.ini
在php配置文件中添加swoole扩展
$ vim php.ini
extension=swoole
检查swoole扩展是否已经安装成功
$ php -i | grep extension_dir
查看扩展是否安装
$ php -m | grep swoole
CentOS安装配置Swoole
安装PHP7.2+
# 卸载原先安装的PHP
$ yum remove php
# 列出所有的php相关的rpm包
$ rpm -qa|grep php
# 强制删除包
$ rpm -e xxx --nodeps
# 安装扩展包
$ yum install -y epel-release -y
# 更新系统内核
$ yum update
# 更改yum源
$ rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
# 安装PHP7.2
$ yum -y install php72w php72w-cli php72w-common php72w-devel php72w-pear php72w-embedded php72w-fpm php72w-gd php72w-imap php72w-ldap php72w-mbstring php72w-opcache php72w-pdo php72w-xml php72w-xmlrpc php72w-mysql php72w-mysqlnd php72w-odbc
# 安装php依赖组件
$ yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers gd gd2 gd-devel gd2-devel perl-CPAN pcre-devel
# 安装php依赖组件
$ yum install -y wget vim pcre pcre-devel openssl openssl-devel libicu-devel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel krb5-devel libidn libidn-devel openldap openldap-devel nss_ldap jemalloc-devel cmake boost-devel bison automake libevent libevent-devel gd gd-devel libtool* libmcrypt libmcrypt-devel mcrypt mhash libxslt libxslt-devel readline readline-devel gmp gmp-devel libcurl libcurl-devel openjpeg-devel libicu-devel
# 创建用户和组
$ groupadd www
$ useradd -g www www
# 下载php安装包解压
$ cd /tmp
$ wget http://am1.php.net/distributions/php-7.2.1.tar.gz
$ tar xvf php-7.2.1.tar.gz
$ cd php-7.2.1
PHP下载地址 https://www.php.net/downloads.php
$ cd tmp && mkdir download && cd download
$ yum install -y wget
$ wget https://www.php.net/distributions/php-7.3.3.tar.gz
$ tar -zxvf php-7.3.3.tar.gz
$ cp -frp /usr/lib64/libldap* /usr/lib/
$ ./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-mysqlnd-compression-support \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--with-mcrypt \
--with-libmbfl \
--enable-ftp \
--with-gd \
--enable-gd-jis-conv \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--enable-opcache \
--with-pear \
--enable-maintainer-zts \
--with-ldap=shared \
--without-gdbm
./configure \
--prefix=/usr/local/php \
--with-fpm-user=www \
--with-fpm-group=www \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-jpeg-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--with-bz2 \
--with-mhash \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-xml \
--enable-zip \
--enable-fpm
# 下载安装icu 然后重新编译时指定路径 --with-icu-dir=/usr/local/icu
checking for location of ICU headers and libraries... checking for icu-config... no
not found
configure: error: Unable to detect ICU prefix or no failed. Please verify ICU install prefix and make sure icu-config works.
$ yum -y install libicu-devel
checking for libzip... not found
configure: error: Please reinstall the libzip distribution
$ yum install -y libzip-devel
checking for libzip... configure: error: system libzip must be upgraded to version >= 0.11
# https://nih.at/libzip/
$ 卸载libzip
$ yum remove -y libzip
$ wget https://libzip.org/download/libzip-1.5.2.tar.gz
$
下载安装icu
重新编译时指定路径 --with-icu-dir=/usr/local/icu
http://download.icu-project.org/files
$ mkdir /usr/local/icu
$ wget http://download.icu-project.org/files/icu4c/64rc2/icu4c-64rc2-src.tgz
$ tar xf icu4c-64rc2-src.tgz
$ cd icu/source
$ ./configure --prefix=/usr/local/icu
$ make && make check
CentOS7安装PHP7
安装PHP7.3.4
$ cd /usr/local/src
$ wget https://www.php.net/distributions/php-7.3.4.tar.gz
$ yum install -y tar
$ tar -zxvf php-7.3.4.tar.gz
$ cd php-7.3.4
$ ./configure --prefix=/usr/local/php
$ make && make check
$ make test
$ make && make install
$ cp /usr/local/src/php-7.3.4/php.ini-development /usr/local/php/lib/php.ini
$ vim ~/.bash_profile
alias php=/usr/local/php/bin/php
$ source ~/.bash_profile
$ php -v
PHP 7.3.4 (cli) (built: Apr 9 2019 22:09:00) ( NTS )
$ yum install php-pear pcre-devel
$ pecl channel-update pecl.php.net
$ pecl upgrade-all
$ cd `pear config-get php_dir`
$ pwd
/usr/share/pear
卸载PHP7.3.4
$ whereis php
$ rm -rf /usr/local/php /usr/bin/php /usr/lib64/php/ /etc/php.d/ /etc/php.ini /usr/share/php /usr/share/man/man1/php
安装PHP7.2.17
$ cd /usr/local/src
$ wget https://www.php.net/distributions/php-7.2.17.tar.gz
$ tar zxvf php-7.2.17.tar.gz
$ cd php-7.2.17
$ ./configure --prefix=/usr/local/php
$ make && make install
$ cd /usr/local/src/php-7.2.14/php.ini-development /usr/local/php/lib/php.ini
$ php -i | grep php.ini
Configuration File (php.ini) Path => /usr/local/php/lib
Loaded Configuration File => /usr/local/php/lib/php.ini
$ vim ~/.bash_profile
alias php=/usr/local/php/bin/php
$ source ~/.bash_profile
$ php -v
没安装 php-devel 这个扩展包,phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块,phpize 是属于php-devel的内容。
$ phpize
Can't find PHP headers in /usr/include/php
The php-devel package is required for use of this command.
$ which phpize
$ yum search php-devel
$ yum info php72-php-devel
$ yum install -y php72-php-devel
$ yum install -y php-devel
$ yum info php72-php-pear
$ yum install -y php72-php-pear
安装GIT
$ yum install -y git
$ git --version
git version 1.8.3.1
GIT下载Swoole
$ cd /usr/local/src
$ git clone https://github.com/swoole/swoole-src.git
$ cd swoole-src
$ make clean
$ phpize
$ which php-config
/usr/bin/php-config
$ ./configure --with-php-config=php-config
$ make && make install
$ whereis php.ini
$ vim /usr/local/php/lib/php.ini
extension=swoole.so
$ php -m | grep swoole