はじめに
CentOS7 / PHP7.1 環境に Nginx Unit を導入してみたので覚書。
現在はBeta版の0.1しかないのでそれを使います。
RPM使わないの?
yum使おうとしたらPHP5.4のパッケージに依存しているとか言われたので面倒くさくなりました。
こんぱいるこんぱいる。
まずインストール
Download -> 展開
cd /usr/local/src
wget https://github.com/nginx/unit/archive/0.1.tar.gz
tar xzf 0.1.tar.gz
cd unit-0.1/
必要なPHPパッケージ類のインストール
バージョンやRepoはともかく、このパッケージが入っていればOKな模様。
yum install php-devel php-embedded --enablerepo=remi-php71
Configure
Baseとなる部分とPHP用の部分のために2回行います。
./configure --bindir=/usr/bin --sbindir=/usr/sbin --modules=/etc/unit/modules --prefix=/etc/unit --log=/var/log/unit/unit.log
./configure php --module=php --config=/usr/bin/php-config --lib-path=/usr/lib64/php/modules
このように設定しておくと、インストールされるときに以下のようなSummaryになります。
お好みで変えてください。
Configuration summary:
unit pid file: "/etc/unit/unit.pid"
unit log file: "/var/log/unit/unit.log"
unit modules path: "/etc/unit/modules"
unit control API socket: "unix:/etc/unit/control.unit.sock"
non-privileged user: "nobody"
non-privileged group: ""
IPv6 support: YES
Unix domain sockets support: YES
debug logging: NO
実行スクリプトは /usr/sbin/unitd
に置かれます。
make & install
make all
make install
起動
とりあえず動かしてみる
# unitd
20ps a17/10/04 23:33:57 [info] 19804#19804 unit started
うごいた。。らしい?
# ps aux | grep unit
root 19854 0.0 0.0 16028 608 ? Ss 23:36 0:00 unit: main [unitd]
nobody 19856 0.0 0.0 16028 416 ? S 23:36 0:00 unit: controller
nobody 19857 0.0 0.0 16028 420 ? S 23:36 0:00 unit: router
# ls -l /etc/unit/
total 16
srw------- 1 root root 0 Oct 4 23:36 control.unit.sock
drwxr-xr-x 2 root root 24 Oct 4 21:36 modules
-rw-r--r-- 1 root root 309 Oct 4 21:37 unit.conf
-rw-r--r-- 1 root root 6 Oct 4 23:36 unit.pid
らしい。
動作チェック
レスポンスが得られたので良し。
現状はコンフィグファイルを読んでいないため何もでない。仕方ない。
# curl -XGET --unix-socket /etc/unit/control.unit.sock http://localhost/
{
"listeners": {},
"applications": {}
}
初期設定
コンフィグを読ませてみる
コンフィグはJSONで記載されます。jqとかで書式チェックとかもできる。
細かい説明は公式にオマカセしてとりあえずコンフィグを流してみます。
{
"listeners": {
"*:8300": {
"application": "blogs"
}
},
"applications": {
"blogs": {
"type": "php",
"workers": 20,
"root": "/www/blogs/scripts",
"index": "index.php"
}
}
}
こんな感じでファイルを作ったら、PUTします。
# curl -X PUT -d @/etc/unit/unit.conf --unix-socket /etc/unit/control.unit.sock http://localhost/
{
"success": "Reconfiguration done."
}
SUCCESS!!って出たらさっきと同じようにGETします。
# curl -XGET --unix-socket /etc/unit/control.unit.sock http://localhost/
{
"listeners": {
"*:8300": {
"application": "blogs"
}
},
"applications": {
"blogs": {
"type": "php",
"workers": 20,
"root": "/www/blogs/scripts",
"index": "index.php"
}
}
}
できました!
最後にListen port確認しておきましょ。
# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8300 0.0.0.0:* LISTEN 19857/unit: router
よきよき。
systemdに登録
適当にブラウザなどでアクセスしてみて、納得が出来たら起動環境を整えます。
[Unit]
Description=Nginx Unit
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
Alias=unitd.service
[Service]
User=root
Group=root
Type=forking
ExecStart=/usr/sbin/unitd
適当にこんな感じで用意しました。
start,stop,restartあたりで動作確認しましょう。
ただし再起動すると悲しいことにコンフィグ読み直してくれないようです。
ので、ExecStartPostにコンフィグ読ませるコマンドを入れておきます。
[Unit]
Description=Nginx Unit
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
Alias=unitd.service
[Service]
User=root
Group=root
Type=forking
ExecStart=/usr/sbin/unitd
ExecStartPost=/usr/bin/curl -X PUT -Ss -d @/etc/unit/unit.conf \
--unix-socket /etc/unit/control.unit.sock http://localhost/
これでsystemdで起動するところまでは大枠良いでしょう。