起動スクリプトを、これも apostrophe からコピー。
tonzlr@wplj:/etc/init.d$ cd tonzlr@wplj:~$ cd /etc/init.d tonzlr@wplj:/etc/init.d$ sudo scp tonzlr@apostrophe:/etc/init.d/tonzlr . sudo tonzlr のパスワード: tonzlr@apostrophe's password: tonzlr 100% 455 0.4KB/s 00:00
これも変更しないでいいんじゃないかな。
#!/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin:/sbin:PATH
TONZLR_ROOT=/home/tonzlr/tonzlr
case "$1" in
start)
echo "Starting Tonzlr"
cd ${TONZLR_ROOT}
export SECRET_KEY_BASE=`rake secret`
bundle exec unicorn_rails -c unicorn.conf -E production -D
;;
stop)
echo "Stoping Tonzlr"
PID=`cat ${TONZLR_ROOT}/unicorn.pid`
kill -QUIT ${PID}
;;
*)
echo "Usage: tonzlr {start|stop}" >&2
exit 1
;;
esac
exit 0
起動確認。
tonzlr@wplj:~/tonzlr$ sudo /etc/init.d/tonzlr start Starting Tonzlr
OK。
Unit ファイルを書く。
[Unit] Description=Tonzlr service After=network.target [Service] ExecStart=/etc/init.d/tonzlr start ExecStop=/etc/init.d/tonzlr stop Restart=always Type=forking PIDFile=/home/tonzlr/tonzlr/unicorn.pid [Install] WantedBy=multi-user.target
そして、/etc/systemd/system/tonzlr.service にリンクを張る。
tonzlr@wplj:~/tonzlr$ sudo ln -s /lib/systemd/system/tonzlr.service /etc/systemd/system/tonzlr.service
起動確認。
tonzlr@wplj:~/tonzlr$ sudo systemctl start tonzlr
OK。start したり stop したりして確認できた。
Ubuntu の起動時に自動起動するようにする。
tonzlr@wplj:~/tonzlr$ sudo systemctl enable tonzlr Synchronizing state of tonzlr.service with SysV init with /lib/systemd/systemd-sysv-install... Executing /lib/systemd/systemd-sysv-install enable tonzlr insserv: warning: script 'K01bruschetta' missing LSB tags and overrides insserv: warning: script 'tonzlr' missing LSB tags and overrides insserv: warning: script 'bruschetta' missing LSB tags and overrides update-rc.d: error: tonzlr Default-Start contains no runlevels, aborting.
むぅ、またエラーが出てる。だけど、この間の Flask アプリではこれでもちゃんと自動起動できるようになっていたので、今回もそれを期待してリブートしてみる。
tonzlr@wplj:~/tonzlr$ sudo reboot
……はたしてリブートした結果……おお、ちゃんと自動起動されている!
よし、最後に、Nginx のヴァーチャルホスト tonzlr を設定する。
upstream unicorn-tonzlr {
server 127.0.0.1:9009;
}
server {
# port
listen 80;
# server name
server_name tonzlr;
# document root
root /home/tonzlr/tonzlr;
# index
#index index.php index.html index.htm;
# log files
access_log /var/log/nginx/tonzlr/access.log combined;
error_log /var/log/nginx/tonzlr/error.log warn;
keepalive_timeout 60;
proxy_connect_timeout 60;
proxy_read_timeout 60;
proxy_send_timeout 60;
location / {
#root /home/tonzlr/tonzlr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://unicorn-tonzlr;
}
}
sites-enabled/tonzlr にリンクを張って:
tonzlr@wplj:/etc/nginx$ sudo ln -s /etc/nginx/sites-available/tonzlr /etc/nginx/sites-enabled/tonzlr
ログファイル用のディレクトリを作る。
tonzlr@wplj:/etc/nginx$ sudo mkdir /var/log/nginx/tonzlr
Nginx をリロード。
tonzlr@wplj:/etc/nginx$ sudo systemctl reload nginx
hosts ファイルに tonzlr を追加して、http://tonzlr/ にブラウザでアクセスしてみる。
OK!!!!!!!!!
あとは、他のマシンの hosts ファイルを書き換えるだけだ。