TOP

CentOS7上にApache2.4+HHVMを入れて高速化をした

2015-10-13 17:01:47

高速化の必要性に迫られて入れてみた

環境はGoogle Compute Engine上のVMインスタンス(CentOS7,Apache2.4,PHP5.4)です。

EPELリポジトリの追加

$ sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm

必要な物を入れる

$ sudo yum install -y cpp gcc-c++ cmake git psmisc {binutils,boost,jemalloc}-devel {ImageMagick,sqlite,tbb,bzip2,openldap,readline,elfutils-libelf,gmp,lz4,pcre}-devel lib{xslt,event,yaml,vpx,png,zip,icu,mcrypt,memcached,cap,dwarf}-devel {unixODBC,expat,mariadb}-devel lib{edit,curl,xml2,xslt}-devel glog-devel oniguruma-devel ocaml gperf enca libjpeg-turbo-devel openssl-devel ccache make --enablerepo=epel

MariaDBを起動

$ sudo systemctl mariadb start

ソースをcloneしてくる

サイズがでかいので割と時間かかります

$ cd /tmp
$ git clone https://github.com/facebook/hhvm -b master hhvm --recursive
$ cd ./hhvm/

権限を変更

そのままだと、ユーザ権限でUNIXドメインソケットで動作させるのに不都合があるので、1箇所変更する(760→766)

$ vim hphp/runtime/server/fastcgi/fastcgi-server.cpp

if (m_socketConfig.bindAddress.getFamily() == AF_UNIX) {
    auto path = m_socketConfig.bindAddress.getPath();
    chmod(path.c_str(), 0766); /* ※←ここ */
}

こちらを参考にさせていただきました

ビルドする

PATHは好きな所に置き換えて下さい(今回はこちらに倣って/opt/hhvm/) あと僕の環境では、makeの途中でext_zend_compatからエラーでコケたので、-DENABLE_ZEND_COMPAT=ONを付けてます

$ sudo cmake -DCMAKE_INSTALL_PREFIX:PATH=/opt/hhvm -DCMAKE_C_FLAGS="-march=native" -DCMAKE_CXX_FLAGS="-march=native" -DENABLE_ZEND_COMPAT=ON -DLIBMAGICKWAND_INCLUDE_DIRS="/usr/include/ImageMagick" -DLIBMAGICKCORE_LIBRARIES="/usr/lib64/libMagickCore.so" -DLIBMAGICKWAND_LIBRARIES="/usr/lib64/libMagickWand.so" .
$ sudo make -j$(($(nproc)))
$ sudo make install

Imagickからエラー [2015-10-20 追記]

公式のサンプルと同様に、echo $imagick_objとしていた箇所からエラー。

Catchable fatal error: Object of class Imagick could not be converted to string

無理矢理、echo strval($imagick_obj)としてみる

Fatal error: Resource did not provide a name

ダメぽい。

結果的に、Imagick::getImagesBlobを使うようにして解決

echo $imagick_obj->getImagesBlob();

Zend Engineの時は出なかったエラーなんですが…ちなPHPは5.4です

マジックメソッド:__toString()

計測してみる

手元のサイト(CakePHP)で150回(50回*3)ほどアクセスした結果、以下のようになりました(使用ツールはSiege)

Zend Engine
$ sudo siege -c 1 -r 50 http://hogehoge.com/
** SIEGE 3.0.8
** Preparing 1 concurrent users for battle.
The server is now under siege..      done.

Transactions:                 50 hits
Availability:             100.00 %
Elapsed time:             123.82 secs
Data transferred:           0.28 MB
Response time:              2.00 secs
Transaction rate:           0.40 trans/sec
Throughput:             0.00 MB/sec
Concurrency:                0.81
Successful transactions:          50
Failed transactions:               0
Longest transaction:            2.65
Shortest transaction:           1.34

$ sudo siege -c 1 -r 50 http://hogehoge.com/

Transactions:                 50 hits
Availability:             100.00 %
Elapsed time:             123.48 secs
Data transferred:           0.28 MB
Response time:              1.97 secs
Transaction rate:           0.40 trans/sec
Throughput:             0.00 MB/sec
Concurrency:                0.80
Successful transactions:          50
Failed transactions:               0
Longest transaction:            2.44
Shortest transaction:           1.36
$ sudo siege -c 1 -r 50 http://hogehoge.com/

Transactions:                 50 hits
Availability:             100.00 %
Elapsed time:             124.48 secs
Data transferred:           0.28 MB
Response time:              1.95 secs
Transaction rate:           0.40 trans/sec
Throughput:             0.00 MB/sec
Concurrency:                0.78
Successful transactions:          50
Failed transactions:               0
Longest transaction:            2.52
Shortest transaction:           1.48
HHVM
$ sudo siege -c 1 -r 50 http://hogehoge.com/
** SIEGE 3.0.8
** Preparing 1 concurrent users for battle.
The server is now under siege..      done.

Transactions:                 50 hits
Availability:             100.00 %
Elapsed time:              62.05 secs
Data transferred:           0.27 MB
Response time:              0.76 secs
Transaction rate:           0.81 trans/sec
Throughput:             0.00 MB/sec
Concurrency:                0.61
Successful transactions:          50
Failed transactions:               0
Longest transaction:            1.21
Shortest transaction:           0.40

$ sudo siege -c 1 -r 50 http://hogehoge.com/

Transactions:                 50 hits
Availability:             100.00 %
Elapsed time:              54.66 secs
Data transferred:           0.27 MB
Response time:              0.69 secs
Transaction rate:           0.91 trans/sec
Throughput:             0.00 MB/sec
Concurrency:                0.63
Successful transactions:          50
Failed transactions:               0
Longest transaction:            1.42
Shortest transaction:           0.39
$ sudo siege -c 1 -r 50 http://hogehoge.com/

Transactions:                 50 hits
Availability:             100.00 %
Elapsed time:              63.59 secs
Data transferred:           0.27 MB
Response time:              0.69 secs
Transaction rate:           0.79 trans/sec
Throughput:             0.00 MB/sec
Concurrency:                0.54
Successful transactions:          50
Failed transactions:               0
Longest transaction:            0.95
Shortest transaction:           0.46