1、打开网址:https://wiki.swoole.com/wiki/page/7.html ,查看环境依赖,如图1
环境依赖
仅支持Linux,FreeBSD,MacOS,3类操作系统
Linux内核版本2.3.32以上
PHP-5.3.10以上版本,包括PHP7
gcc4.4以上版本或者clang
cmake2.4+,编译为libswoole.so作为C/C++库时需要使用cmake
建议使用Ubuntu14、CentOS7或更高版本的操作系统

图1

2、在 VirtualBox 设置端口转发规则,以便于远程连接 CentOS 7 64,如图2

图2

3、查看 VirtualBox 的IP地址,如图3

图3

4、通过 Xshell 5 连接 CentOS 7 64,如图4

图4

5、ssh 连接成功,如图5

图5

6、环境依赖检查,分别查看gcc、gcc-c++、make、autoconf、pcre的安装情况,未安装的,需要安装,gcc、gcc-c++、autoconf未安装,如图6

rpm -qa gcc
rpm -qa gcc-c++
rpm -qa make
rpm -qa autoconf
rpm -qa pcre

图6

7、安装 gcc、gcc-c++、autoconf,如图7
yum install gcc gcc-c++ autoconf

图7

8、再次查看gcc、gcc-c++、autoconf的安装情况,皆已安装成功,如图8
rpm -qa gcc
rpm -qa gcc-c++
rpm -qa autoconf

图8

9、编译安装 php7 之前需要安装对应的编译工具和依赖包,运行如下命令,如图9
rpm -qa libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel
yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel

图9

10、查看安装情况,libmcrypt-devel未安装,如图10
rpm -qa libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel
rpm -qa libcurl-devel
rpm -qa libjpeg-turbo-devel

图10

11、libmcrypt-devel未安装,原因在于:没有可用软件包 libmcrypt-devel,如图11

图11

12、搜索 EPEL 相关的软件包,如图12
yum search epel

图12

13、安装 EPEL 软件包,如图13
yum install epel-release

图13

14、再次安装 libmcrypt-devel,如图14
yum -y install libmcrypt-devel

图14

15、查看安装情况,libmcrypt-devel已安装,如图15
rpm -qa libmcrypt-devel

图15

16、参考网址:http://www.shaunfreeman.name/compiling-php-7-on-centos/ ,新建目录:/usr/local/php7,如图16
mkdir /usr/local/php7

图16

17、安装 Git,如图17
yum install git-core

图17

18、进入用户主目录,如图18
cd ~

图18

19、从 PHP Github 仓库中获取源代码,如图19
git clone https://github.com/php/php-src.git

图19

20、进入目录:php-src,如图20
cd php-src/

图20

21、打开网址:https://github.com/php/php-src ,当前 7.0.* 的最新分支为 7.0.23,如图21

图21

22、切换至 7.0.23,如图22
git checkout PHP-7.0.23

图22

23、生成配置脚本,如图23
./buildconf –force

图23

24、使用如下命令编译配置,如图24
./configure –prefix=/usr/local/php7 \
–with-config-file-path=/usr/local/php7/etc \
–with-config-file-scan-dir=/usr/local/php7/etc/conf.d \
–enable-bcmath \
–with-bz2 \
–with-curl \
–enable-filter \
–enable-fpm \
–with-gd \
–enable-gd-native-ttf \
–with-freetype-dir \
–with-jpeg-dir \
–with-png-dir \
–enable-intl \
–enable-mbstring \
–with-mcrypt \
–enable-mysqlnd \
–with-mysql-sock=/var/lib/mysql/mysql.sock \
–with-mysqli=mysqlnd \
–with-pdo-mysql=mysqlnd \
–with-pdo-sqlite \
–disable-phpdbg \
–disable-phpdbg-webhelper \
–enable-opcache \
–with-openssl \
–enable-simplexml \
–with-sqlite3 \
–enable-xmlreader \
–enable-xmlwriter \
–enable-zip \
–with-zlib

图24

25、报错,如图25
configure: WARNING: This bison version is not supported for regeneration of the Zend/PHP parsers (found: none, min: 204, excluded: ).
checking for re2c… no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
configure: error: bison is required to build PHP/Zend when building a GIT checkout!

图25

26、安装 bison,如图26
yum install bison

图26

27、再次编译配置,报错,如图27
checking for BZip2 support… yes
checking for BZip2 in default path… not found
configure: error: Please reinstall the BZip2 distribution

图27

28、安装 bzip2-devel,如图28
yum install bzip2-devel

图28

29、再次编译配置,报错,如图29
checking build system type… config.sub: missing argument
Try `config.sub –help’ for more information.
configure: error: /bin/sh ./config.sub failed

图29

30、检查之后,发现 –enable-zip \ 后面多了一个空格,如图30

图30

31、去掉 –enable-zip \ 后面的空格,再次编译安装PHP,报错,如图31
checking for icu-config… no
checking for location of ICU headers and libraries… not found
configure: error: Unable to detect ICU prefix or no failed. Please verify ICU install prefix and make sure icu-config works.

图31

32、安装 libicu-devel,如图32
yum install libicu-devel

图32

33、再次编译配置,正常,如图33

图33

34、正式安装,依次执行,如图34
make
make test
make install

图34

35、添加ini选项,如图35
mkdir /usr/local/php7/etc/conf.d
cp ./php.ini-development /usr/local/php7/lib/php.ini

图35

36、将PHP的可执行目录添加到环境变量中,使用 Vim 打开 ~/.bashrc,在末尾添加如下内容:,如图36
vim ~/.bashrc
export PATH=/usr/local/php7/bin:$PATH
export PATH=/usr/local/php7/sbin:$PATH

图36

37、保存完毕后,在终端中运行以下命令:如图37
source ~/.bashrc

图37

38、查看 PHP 版本,如图38
php –version

PHP 7.0.23 (cli) (built: Sep 4 2017 17:12:44) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies

图38

39、进入用户主目录,如图39
cd ~

图39

40、从 Swoole Github 仓库中获取源代码,如图40
git clone https://github.com/swoole/swoole-src.git

图40

41、进入目录:swoole-src,如图41
cd swoole-src/

图41

42、打开网址:https://github.com/swoole/swoole-src/releases ,当前最新稳定版为 v1.9.19,如图42

图42

43、切换至 v1.9.19,如图43
git checkout v1.9.19

报错:Note: checking out ‘v1.9.19’.

You are in ‘detached HEAD’ state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

git checkout -b new_branch_name

HEAD 目前位于 124c4f5… fix #1337

图43

44、切换至 v1.9.19,如图44
git checkout -b v1.9.19

图44

45、使用 phpize 来生成php编译配置,执行命令:如图45
phpize

图45

46、使用 ./configure 来做编译配置检测,swoole的./configure有很多额外参数,可以通过 ./configure –help 命令查看,这里均选择默认项,如图46
./configure

图46

47、编译安装,依次执行,如图47
make
make test
make install

图47

48、安装完成后,编辑 /usr/local/php7/etc/conf.d/modules.ini 文件,如图48
vim /usr/local/php7/etc/conf.d/modules.ini

图48

49、在其中加上如下一句,如图49
extension=swoole.so

图49

50、输入命令 php -m 查看扩展安装情况,在列出的扩展中看到了 swoole,说明安装成功,如图50
php -m

图50

51、安装增强功能,如图51

图51

52、报错,如图52
Failed to set up service vboxadd, please check the log file /var/log/VBoxGuestAdditions.log for details.
Press Return to close this window…

图52

53、查看 /var/log/VBoxGuestAdditions.log 文件,如图53
cat /var/log/VBoxGuestAdditions.log

日志:
vboxadd.sh: failed: Look at /var/log/vboxadd-install.log to find out what went wrong.
vboxadd.sh: failed: Look at /var/log/vboxadd-install.log to find out what went wrong.
vboxadd.sh: failed: modprobe vboxguest failed.

图53

54、查看 /var/log/vboxadd-install.log 文件,如图54
cat /var/log/vboxadd-install.log

日志:
/tmp/vbox.0/Makefile.include.header:112: *** Error: unable to find the sources of your current Linux kernel. Specify KERN_DIR=<directory> and run Make again。 停止。
Creating user for the Guest Additions.
Creating udev rule for the Guest Additions kernel module.

图54

55、查看kernel-headers、kernel-devel的安装情况,kernel-devel 未安装,如图55
rpm -qa kernel-headers
rpm -qa kernel-devel

图55

56、安装 kernel-devel,如图56
yum install kernel-devel

图56

57、基于 VirtualBox 重启虚拟电脑,再次安装增强功能,如图57
报错:未能加载虚拟光盘 VBoxsGuestAdditions.iso 到虚拟电脑

图57

58、将虚拟电脑中的光驱弹出,如图58

图58

59、再次安装增强功能,仍然报错,如图59

图59

60、升级所有包同时也升级软件和系统内核,如图60
yum update

图60

61、基于 CentOS 重启虚拟电脑,再次安装增强功能,安装成功,如图61

图61

62、根据安装提示,重启 Windows 系统,在 视图 – 虚拟显示屏 中,如果分辩率可选,表示增强功能成功安装,如图62

图62

63、新建共享文件夹,E:\wwwroot\CentOS-7-x86_64-DVD_share,如图63

图63

64、在 CentOS 中创建挂载文件夹,如图64
mount -t vboxsf CentOS-7-x86_64-DVD_share /mnt/share

报错:
/sbin/mount.vboxsf: mounting failed with the error: No such file or directory

图64

65、新建目录 /mnt/share,如图65
mkdir /mnt/share

图65

66、在 CentOS 中再次挂载文件夹,正常,如图66
mount -t vboxsf CentOS-7-x86_64-DVD_share /mnt/share

图66

67、在目录 E:\wwwroot\CentOS-7-x86_64-DVD_share 中,新建 phpinfo.php 文件,如图67

图67

68、查看 /mnt/share ,已经存在 phpinfo.php 文件,共享成功,如图68

图68

69、运行 php phpinfo.php,正常,如图69

图69

70、搭建Echo服务器,创建一个Server.php文件并输入如下内容:,如图70

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/5
 * Time: 15:33
 */
// Server
class Server
{
    private $serv;

    public function __construct() {
        $this->serv = new swoole_server("0.0.0.0", 9501);
        $this->serv->set(array(
            'worker_num' => 8,
            'daemonize' => false,
        ));

        $this->serv->on('Start', array($this, 'onStart'));
        $this->serv->on('Connect', array($this, 'onConnect'));
        $this->serv->on('Receive', array($this, 'onReceive'));
        $this->serv->on('Close', array($this, 'onClose'));

        $this->serv->start();
    }

    public function onStart( $serv ) {
        echo "Start\n";
    }

    public function onConnect( $serv, $fd, $from_id ) {
        $serv->send( $fd, "Hello {$fd}!" );
    }

    public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
        echo "Get Message From Client {$fd}:{$data}\n";
        $serv->send($fd, $data);
    }

    public function onClose( $serv, $fd, $from_id ) {
        echo "Client {$fd} close connection\n";
    }
}
// 启动服务器 Start the server
$server = new Server();

图70

71、创建一个Client.php文件并输入如下内容:,如图71

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/5
 * Time: 15:35
 */
class Client
{
    private $client;

    public function __construct() {
        $this->client = new swoole_client(SWOOLE_SOCK_TCP);
    }

    public function connect() {
        if( !$this->client->connect("127.0.0.1", 9501 , 1) ) {
            echo "Error: {$this->client->errMsg}[{$this->client->errCode}]\n";
        }

        fwrite(STDOUT, "请输入消息 Please input msg:");
        $msg = trim(fgets(STDIN));
        $this->client->send( $msg );

        $message = $this->client->recv();
        echo "Get Message From Server:{$message}\n";
    }
}

$client = new Client();
$client->connect();

图71

72、在一个 Xshell 终端中运行服务端,如图72
php Server.php

图72

73、在一个 Xshell 终端中运行客户端,如图73
php Client.php

图73

 

 

 

永夜