LoginSignup
4
1

More than 5 years have passed since last update.

EC2にSwooleをインストールする

Posted at

概要

元々PHPの非同期処理に興味があり、PHPカンファレンス関西でハチャメチャに叩く - Speaker Deckを聞いて来たので、これを機会に始めていきます。

環境構築

PHPをインストール

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

gccをインストール

yum install -y gcc gcc-c++

公式の手順に従ってインストール

cd /usr/local/src
git clone https://github.com/swoole/swoole-src.git
cd swoole-src
phpize
./configure
make && make install

php.iniに追記

extension=swoole.so

これで php -i を実行すると Swoole が読み込まれているのが分かります。

$ php -i | grep sw
swoole
swoole support => enabled
Author => Swoole Group[email: team@swoole.com]
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
<?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();
$ curl http://127.0.0.1:9501
Hello World<br>

一先ず動きました。
以上でインストールは完了です。

最後まで読んで頂きありがとうございます。

参考

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1