Swoole安装 (源码编译安装)
思路:先编译configure 然后make && make install
1、下载Swoole源码
# wget http://pecl.php.net/get/swoole-4.4.6.tgz
或者克隆swoole
# git clone https://gitee.com/swoole/swoole.git
2、将下载的swoole-4.4.6.tgz 文件解压并执行
# tar -zxvf swoole-v4.6.6.zip
3、修改名称
# mv swoole-v4.6.6 swoole
4、进入swoole目录下执行
# cd swoole
当我进入swoole的文件时,发现并没有我们熟悉的configure文件
其实安装这种扩展分为两种:
1)、编译php时候安装的扩展
2)、php安装完成后,外挂到php上的扩展 (这里选择第二种)
php中通过外挂扩展需要使用phpize命令,如果配置了环境变量,则可以直接使用phpize命令,出现如下效果
[root@VM-4-2-centos bin]# phpize Configuring for: PHP Api Version: 20160303 Zend Module Api No: 20160303 Zend Extension Api No: 320160303
如果没有设置环境变量则需要指定全路径执行如下
[root@VM-4-2-centos bin]# /usr/local/php/bin/phpize Configuring for: PHP Api Version: 20160303 Zend Module Api No: 20160303 Zend Extension Api No: 320160303
如果 phpize 出现的错误
[root@VM-4-2-centos bin]# /usr/local/php/bin/phpize Cannot find config.m4. Make sure that you run '/usr/local/php/bin/phpize' in the top level source directory of the module
解决方法:
[root@VM-4-2-centos openssl]# pwd /root/php-7.3.5/ext/openssl [root@VM-4-2-centos openssl]# cp /root/php-7.3.5/ext/openssl/config0.m4 /usr/local/php/bin/config.m4
如果出现错误
Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF envifonment variable. Then,retun this script.
如果报这个错的话,就是没有安装autoconf
解决方案也简单
# yum install autoconf
# 重新执行phpize命令
[root@VM-4-2-centos bin]# phpize Configuring for: PHP Api Version: 20160303 Zend Module Api No: 20160303 Zend Extension Api No: 320160303
如果正常,此时查看swoole目录下就有configure 文件了
[root@VM-4-2-centos mytools]# cd swoole/ [root@VM-4-2-centos swoole]# ll total 2568 -rw-r--r-- 1 root root 84357 Aug 10 17:11 acinclude.m4 -rw-r--r-- 1 root root 394382 Aug 10 17:11 aclocal.m4 drwxr-xr-x 2 root root 4096 Aug 10 17:11 autom4te.cache drwxr-xr-x 2 root root 4096 Aug 10 17:11 build -rwxr-xr-x 1 root root 407 Apr 22 2021 clear.sh -rw-r--r-- 1 root root 5248 Apr 22 2021 CMakeLists.txt -rwxr-xr-x 1 root root 1102 Apr 22 2021 code_format.sh -rwxr-xr-x 1 root root 81 Apr 22 2021 code_stats.sh -rw-r--r-- 1 root root 113 Apr 22 2021 CODE-STYLE.md -rwxr-xr-x 1 root root 45297 Aug 10 17:11 config.guess -rw-r--r-- 1 root root 5412 Aug 10 17:11 config.h -rw-r--r-- 1 root root 5043 Aug 10 17:11 config.h.in -rw-r--r-- 1 root root 59690 Aug 10 17:11 config.log -rw-r--r-- 1 root root 29051 Apr 22 2021 config.m4 -rwxr-xr-x 1 root root 150 Aug 10 17:11 config.nice -rwxr-xr-x 1 root root 59908 Aug 10 17:11 config.status -rwxr-xr-x 1 root root 35454 Aug 10 17:11 config.sub -rwxr-xr-x 1 root root 656247 Aug 10 17:11 configure -rw-r--r-- 1 root root 4684 Aug 10 17:11 configure.in -rw-r--r-- 1 root root 152 Apr 22 2021 CPPLINT.cfg -rw-r--r-- 1 root root 62 Apr 22 2021 CREDITS drwxr-xr-x 31 root root 4096 Apr 22 2021 examples drwxr-xr-x 3 root root 4096 Aug 10 17:12 ext-src
# 编译安装(现在在swoole目录下就会有configure了)
# ./configure --enable-openssl --enable-http2 --with-php-config=/usr/local/php/bin/php-config && make && make install
如果出现configure:error:C++ preprocessor "/lib/cpp" fails sanity chSee `config.log` for more details
出现该情况是由于c++编译器的相关package没有安装
# yum install gcc-c++
然后重新执行编译安装
# 编译安装完成后结果如下:
Build complete. Don't forget to run 'make test'. Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20180731/ Installing header files: /usr/local/php/include/php/
# 进入到扩展目录下查看是否安装了swoole.so扩展
[root@VM-4-2-centos lib]# cd /usr/local/php/lib/php/extensions/no-debug-non-zts-20180731 [root@VM-4-2-centos no-debug-non-zts-20180731]# ll total 34592 -rwxr-xr-x 1 root root 1388742 Aug 2 12:12 mysqli.a -rwxr-xr-x 1 root root 654912 Aug 2 12:12 mysqli.so -rwxr-xr-x 1 root root 4214218 Aug 2 12:12 opcache.a -rwxr-xr-x 1 root root 2279240 Aug 2 12:12 opcache.so -rwxr-xr-x 1 root root 497142 Aug 2 12:12 pdo_mysql.a -rwxr-xr-x 1 root root 229888 Aug 2 12:12 pdo_mysql.so -rwxr-xr-x 1 root root 26144112 Aug 10 17:13 swoole.so
# 需要在php.ini中开启swoole扩展
extension=swoole
注意:
安装完毕后 通过 php -m 查看是否有swoole扩展,如果发现php -v和 phpinfo 版本不一致
需要关闭不符合swoole的php版本 (php7.2以上)
# php -m swoole sysvsem tokenizer xml xmlreader xmlrpc xmlwriter zip zlib
查看swoole安装包的demo 下的echo.php
# cd /root/mytools/swoole/examples/server
# 运行echo.php
[root@VM-4-2-centos examples]# pwd /root/mytools/swoole/examples [root@VM-4-2-centos examples]# cd server/ [root@VM-4-2-centos server]# php echo.php
这是另外开一个终端,查看端口是否启动
swoole服务需要监听9051端口
[root@VM-4-2-centos ~]# lsof -i:9501 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME php 11519 root 3u IPv4 136461055 0t0 TCP *:9501 (LISTEN)
或者查看端口号9501 netstat -tunlp
[root@VM-4-2-centos ~]# netstat -tunlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:9501 0.0.0.0:* LISTEN 11924/php tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 4211/php-fpm: pool tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 19771/redis-server tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7213/nginx: master tcp 0 0 0.0.0.0:81 0.0.0.0:* LISTEN 7213/nginx: master tcp 0 0 0.0.0.0:82 0.0.0.0:* LISTEN 7213/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2045/sshd tcp6 0 0 :::3306 :::* LISTEN 4622/mysqld tcp6 0 0 :::21 :::* LISTEN 10717/vsftpd udp 0 0 0.0.0.0:68 0.0.0.0:* 924/dhclient udp 0 0 10.0.4.2:123 0.0.0.0:* 654/ntpd udp 0 0 127.0.0.1:123 0.0.0.0:* 654/ntpd udp6 0 0 fe80::5054:ff:fe37::123 :::* 654/ntpd udp6 0 0 ::1:123 :::* 654/ntpd
此时 swoole已经安装完毕!