CentOS7.0 + HHVM3.5 + Nginx環境構築

# OS状況確認

CentOS 7 (x86_64) with Updates HVM
https://aws.amazon.com/marketplace/pp/B00O7WM7QW
※user:'centos'

# OS初期セットアップ

```shell-session
$ yum -y update
$ yum -y install wget ntp mlocate
```

## 時刻サーバ設定
```shell-session
sed -i "s/server 0.centos.pool.ntp.org iburst/server -4 ntp.nict.jp iburst/g" /etc/ntp.conf
sed -i "s/server 1.centos.pool.ntp.org iburst/server -4 ntp1.jst.mfeed.ad.jp iburst/g" /etc/ntp.conf
sed -i "s/server 2.centos.pool.ntp.org iburst/server -4 ntp2.jst.mfeed.ad.jp iburst/g" /etc/ntp.conf
sed -i "s/server 3.centos.pool.ntp.org iburst/server -4 ntp3.jst.mfeed.ad.jp iburst/g" /etc/ntp.conf
```

## NTPサーバ起動

```shell-session
$ sudo systemctl enable ntpd
$ sudo systemctl start ntpd
$ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*ntp-a3.nict.go. .NICT.           1 u   29   64  177    2.445  -12.927   7.649
+ntp1.jst.mfeed. 172.29.2.50      2 u   21   64  177    2.361   -9.481   4.574
+ntp2.jst.mfeed. 172.16.176.60    2 u   20   64  177    2.330  -10.254   4.722
-ntp3.jst.mfeed. 133.243.236.17   2 u   22   64  177    2.913   -4.273   5.107
```

#タイムゾーンを日本に書き換え

```shell-session
$ sudo timedatectl set-timezone Asia/Tokyo
$ timedatectl status
]$ timedatectl status
      Local time: Wed 2015-04-15 16:39:04 JST
  Universal time: Wed 2015-04-15 07:39:04 UTC
        Timezone: Asia/Tokyo (JST, +0900)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a
```


# HHVMインストール

## リポジトリ設定
```shell-session
sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
sudo rpm -Uvh http://yum.gleez.com/6/x86_64/gleez-repo-6-0.el6.noarch.rpm
sudo sed -i 's|baseurl=http://yum.gleez.com/6/$basearch/|baseurl=http://yum.gleez.com/7/$basearch/|g' /etc/yum.repos.d/gleez.repo
sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
```
※現在、Centos7版のリポジトリがないので、CentOS6版を持ってきて、7に書き換えている

# HHVMインストール

```shell-session
sudo yum --nogpgcheck install hhvm
```

# HHVM起動確認

```shell-session
[centos@ip-172-31-6-55 ~]$ hhvm --version
HipHop VM 3.5.0 (rel)
Compiler: tags/HHVM-3.5.0-0-ga90f4733cfa0d8fefdafc7433f758f78cdc75424
Repo schema: b88b6fee8ef78ab638506d212abcbefe74a276ac
[centos@ip-172-31-6-55 ~]$sudo mkdir /var/run/hhvm/
[centos@ip-172-31-6-55 ~]$sudo chown nginx:nginx /var/run/hhvm/
```
※ 標準の権限設定だと/var/cache/nginx/に権限がない為、付与。

参考までのその際のエラーメッセージを記載。
```shell-session:/var/log/hhvm/error.log
[Mon Apr 13 19:51:08 2015] [hphp] [12230:7f0b59546ec0:0:000005] [] mapping self...
[Mon Apr 13 19:51:08 2015] [hphp] [12230:7f0b59546ec0:0:000006] [] mapping self took 0'00" (212668 us) wall time
[Mon Apr 13 19:51:08 2015] [hphp] [12230:7f0b59546ec0:0:000007] [] loading static content...
[Mon Apr 13 19:51:08 2015] [hphp] [12230:7f0b59546ec0:0:000008] [] loading static content took 0'00" (1 us) wall time
[Mon Apr 13 19:51:08 2015] [hphp] [12230:7f0b59546ec0:0:000009] [] Failed to initialize central HHBC repository:\n  Failed to open /var/run/hhvm/hhvm.hhbc: 14 - unable to open database file\n  Failed to open /var/www/.hhvm.hhbc: 14 - unable to open database file\n
```

# HHVM自動起動設定

```shell-session
sudo systemctl enable hhvm
sudo systemctl start hhvm
sudo systemctl status hhvm
```

# ローカルPHPとして実行できるように設定
```shell-session
sudo update-alternatives --install /usr/bin/php php /usr/bin/hhvm 60
```

# Nginxインストール

```shell-session
sudo yum --nogpgcheck install nginx
sudo setenforce 0
```

# Nginx設定

```shell-session
$ sudo cat <<_EOF_ > /etc/nginx/hhvm.conf
# Note this will work with "hack" files as well as php files!
location ~ \.(hh|php)$ {
    fastcgi_keep_conn on;
    try_files      $uri =404;
    fastcgi_pass   127.0.0.1:9001;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include        fastcgi_params;
}
_EOF_
$ sudo cat <<_EOF_ > /etc/nginx/nginx.conf
user  nginx;
worker_processes  1; #　CPUの数に合わせる

#error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  warn;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;
pid        /var/run/nginx.pid;

events {
    worker_connections  2048;
}

http {
    #server_tokens off; #本番環境の場合、コメント外す

    # default log format
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    # default access log
    access_log  /var/log/nginx/access.log  main;　

    # don't rely on the kernel's sendfile() this is more efficient.
    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;

    # allow user agents to keep a connection open with us.
    keepalive_timeout         10;
    client_header_timeout     10;
    client_body_timeout       10;
    reset_timedout_connection on;
    send_timeout              10;

    limit_conn_zone $binary_remote_addr zone=addr:10m;
    limit_conn addr 100;

    include       /etc/nginx/mime.types;
    default_type  text/html;

    # deliver content faster with gzip.
    gzip              on;
    gzip_http_version 1.0;
    gzip_disable      "msie6";
    gzip_vary         on; # 本番環境の場合、off
    #gzip_proxied      any; # Proxy利用の場合、有効化
    gzip_min_length 1024;
    gzip_comp_level 6;
    gzip_types      text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript application/json;

    # cache the open file descriptors
    open_file_cache max=10000 inactive=20s;
    open_file_cache_valid    30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors   on;

    # allow 20Mb as max upload size.
    client_body_buffer_size 20M;

    include /etc/nginx/conf.d/*.conf;
}
_EOF_
```

# Nginx自動起動設定

```shell-session
sudo systemctl enable nginx
sudo systemctl start nginx
sudo systemctl status nginx
```

#関連投稿
HHVM3.5+Symfony2環境構築
http://qiita.com/nyatakasan/items/1e1b78238a8b0609259d

# その他・参考情報
Building and installing hhvm on CentOS 7.x · facebook/hhvm Wiki
https://github.com/facebook/hhvm/wiki/Building-and-installing-hhvm-on-CentOS-7.x
Prebuilt packages on Centos 6.5 · facebook/hhvm Wiki
https://github.com/facebook/hhvm/wiki/Prebuilt-packages-on-Centos-6.5
FastCGI · facebook/hhvm Wiki
https://github.com/facebook/hhvm/wiki/FastCGI
hhvm基础配置
http://www.hhvmc.com/thread-115-1-1.html
using HHVM FastCGI with NGINX
http://houwenhui.gotoip2.com/archives/2095