在EC2上安装Swoole


概述

我本来对PHP异步处理感兴趣,所以在PHP会议Kansai上一团糟---我听了Speaker Deck,所以我将借此机会开始。

环境建设

安装PHP

1
yum install -y php72 php72-mbstring php72-pdo php72-devel php7-pear

安装gcc

1
yum install -y gcc gcc-c++

按照官方程序安装

1
2
3
4
5
6
cd /usr/local/src
git clone https://github.com/swoole/swoole-src.git
cd swoole-src
phpize
./configure
make && make install

添加到php.ini

1
extension=swoole.so

现在,当您运行php -i时,将看到Swoole已加载。

1
2
3
4
5
6
7
8
9
10
$ php -i | grep sw
swoole
swoole support => enabled
Author => Swoole Group[email: [email protected]]
swoole.aio_thread_num => 2 => 2
swoole.display_errors => On => On
swoole.fast_serialize => Off => Off
swoole.unixsock_buffer_size => 8388608 => 8388608
swoole.use_namespace => On => On
swoole.use_shortname => On => On

尝试移动

server.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
/**
 * SwooleでHTTPサーバを立てる
 * @link https://www.swoole.co.uk/
 */
define('SERVER_IP', 'IP');
define('PORT', 9501);
$http = new swoole_http_server(SERVER_IP, PORT);
$http->on('start', function ($server) {
    echo 'Swoole http server is started at ';
    echo 'http://' . SERVER_IP . ':' . PORT . PHP_EOL;
});
$http->on('request', function ($request, $response) {
    $response->header('Content-Type', 'text/plain');
    $response->end('Hello World<br>');
});
$http->start();
1
2
$ curl http://127.0.0.1:9501
Hello World<br>

暂时有效。
这样就完成了安装。

感谢您读完文章。

参考

  • 正式程序