さくらのVPSプラン乗り換え計画(4)

MySQL のインストール。

takatoh@tk2-254-36564:~$ sudo apt install mysql-server

途中で root のパスワードを訊かれるので入力(2回)。
バージョンの確認。

takatoh@tk2-254-36564:~$ mysql --version
mysql  Ver 14.14 Distrib 5.7.18, for Linux (x86_64) using  EditLine wrapper

設定ファイルは /etc/mysql/my.cnf。

takatoh@tk2-254-36564:~$ cd /etc/mysql
takatoh@tk2-254-36564:/etc/mysql$ cat my.cnf
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

…と思ったら、何やら追加の設定は別のファイルに書くらしい。/etc/mysql/conf.d と /etc/mysql/mysql.conf.d の下に .cnf ファイルを置けば読み込んでくれるってことでいいのかな。2つのディレクトリの下にはファイルが用意されている。

takatoh@tk2-254-36564:/etc/mysql$ ls conf.d
mysql.cnf  mysqldump.cnf
takatoh@tk2-254-36564:/etc/mysql$ ls mysql.conf.d
mysqld.cnf  mysqld_safe_syslog.cnf

日本語を扱えるようにデフォルトの charset を utf-8 にするため、それぞれのファイルに次のように設定してみた(色を付けたところが追記したところ)。

[mysql]
default-character-set=utf8
[/crayon]
[crayon title="etc/mysql/conf.d/mysqldump.cnf" mark="5"]
[mysqldump]
quick
quote-names
max_allowed_packet = 16M
default-character-set=utf8
(前略)
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
character-set-server=utf8
skip-character-set-client-handshake
default-storage-engin=InnoDB
(後略)

これでいいはず。
MySQL を再起動してみよう。

takatoh@tk2-254-36564:/etc/mysql$ sudo systemctl restart mysqld
Failed to restart mysqld.service: Unit mysqld.service not found.

あれ?だめだ。Unit ファイルがないってか?

takatoh@tk2-254-36564:/etc/mysql$ sudo /etc/init.d/mysql restart
[....] Restarting mysql (via systemctl): mysql.serviceJob for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.
 failed!

/etc/init.d/mysql を直接叩いてもダメだ。メッセージに従って詳細を見てみよう。

takatoh@tk2-254-36564:/etc/mysql$ systemctl status mysql.service
* mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: activating (start-post) (Result: exit-code) since Thu 2017-05-04 12:59:31 JST; 21s ago
  Process: 8060 ExecStart=/usr/sbin/mysqld (code=exited, status=1/FAILURE)
  Process: 8051 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
 Main PID: 8060 (code=exited, status=1/FAILURE);         : 8061 (mysql-systemd-s)
   CGroup: /system.slice/mysql.service
           `-control
             |-8061 /bin/bash /usr/share/mysql/mysql-systemd-start post
             `-8125 sleep 1

May 04 12:59:01 tk2-254-36564 systemd[1]: Starting MySQL Community Server...
May 04 12:59:04 tk2-254-36564 systemd[1]: mysql.service: Main process exited, code=exited, status=1/FAILURE

なんかエラー?

takatoh@tk2-254-36564:/etc/mysql$ journalctl -xe

こちらの出力は長くて載せられないけど、やっぱりエラーになってるらしい。
mysql.servece ファイルが無いとの事なので調べげ見ると、/etc/systemd/system にはないけど、/lib/systemd/system にはあることがわかった。てことは、リンクを張ればいいのかな。

takatoh@tk2-254-36564:/etc/mysql$ cd
takatoh@tk2-254-36564:~$ sudo ln -s /lib/systemd/system/mysql.service /etc/systemd/system/mysql.service
takatoh@tk2-254-36564:~$ ls -l /etc/systemd/system
total 32
drwxr-xr-x 2 root root 4096 May  4 10:01 default.target.wants
drwxr-xr-x 2 root root 4096 May  4 10:00 getty.target.wants
drwxr-xr-x 2 root root 4096 May  4 10:20 graphical.target.wants
drwxr-xr-x 2 root root 4096 May  4 11:47 multi-user.target.wants
lrwxrwxrwx 1 root root   33 May  4 13:08 mysql.service -> /lib/systemd/system/mysql.service
drwxr-xr-x 2 root root 4096 May  4 10:01 network-online.target.wants
drwxr-xr-x 2 root root 4096 May  4 10:22 sockets.target.wants
lrwxrwxrwx 1 root root   31 May  4 10:23 sshd.service -> /lib/systemd/system/ssh.service
drwxr-xr-x 2 root root 4096 May  4 10:23 sysinit.target.wants
lrwxrwxrwx 1 root root   35 May  4 10:01 syslog.service -> /lib/systemd/system/rsyslog.service
drwxr-xr-x 2 root root 4096 May  4 10:01 timers.target.wants

これでどうだ。

takatoh@tk2-254-36564:~$ sudo systemctl restart mysql
Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.

だめか。
Unit ファイルのなかを覗いてみる。

# MySQL systemd service file

[Unit]
Description=MySQL Community Server
After=network.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql
PermissionsStartOnly=true
ExecStartPre=/usr/share/mysql/mysql-systemd-start pre
ExecStart=/usr/sbin/mysqld
ExecStartPost=/usr/share/mysql/mysql-systemd-start post
TimeoutSec=600
Restart=on-failure
RuntimeDirectory=mysqld
RuntimeDirectoryMode=755

うん、わからん。
PermissionsStartOnly=true って行が気になる。start だけってことなのかな。サーバ自体を再起動してみるか。

うーん、再起動してもダメだなあ。
とりあえずここまでにしよう。後で追記する。

[追記]

いったん MySQL サーバをアンインストールして、再インストールしたらどうかと思ってやってみたら、今度はインストールすらエラーが出るようになってしまった。
こりゃダメか。最初からやり直しだな。

さくらのVPSプラン乗り換え計画(3)

Ruby と Git をインストール。
まず Ruby。

takatoh@tk2-254-36564:~$ sudo apt install ruby ruby-dev
takatoh@tk2-254-36564:~$ ruby -v
ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu]

bundler が入っていないのでインストール。

takatoh@tk2-254-36564:~$ sudo gem install bundler

つぎは Git。

takatoh@tk2-254-36564:~$ sudo apt install git
takatoh@tk2-254-36564:~$ git --version
git version 2.7.4

さくらのVPSプラン乗り換え計画(1)

タイトルの通り、プランの乗り換えにチャレンジ。

VPS の OS は標準の CentOS じゃなくて、使い慣れた Ubuntu にしたかったので、OS の再インストールをした。ところが、用意されている「標準OS」の Ubuntu 16.04 をインストールしてみたけど、ログインができない。パスワードを間違えたのかと思って何度かやり直し(インストール自体のやり直しも含めて)やってみたけどダメ。CentOS6 をインストールすると何故か大丈夫。なんでだ?
仕方がないので「カスタムOS」の Ubuntu 16.04 をインストールしたら(インストールに時間はかかったけど)無事、ログインに成功した。ユーザー名は takatoh。

VPS のページから、コンソールを選んでサーバに接続する。
まずは ssh の設定。/etc/ssh/sshd_config ファイルを編集する。

$ sudo vi /etc/ssh/sshd_config

ポートを変更。

Port 60000

root でのログインを禁止。

PermitRootLogin no

設定を反映するために sshd を再起動。

$ sudo systemctl restart sshd

次にファイアウォールの設定。

$ sudo ufw enable
$ sudo ufw default DENY
$ sudo ufw allow 60000

これで、ローカルの PC から ssh でアクセスできるようになったはず。試してみよう。

takatoh@envelopes $ ssh -p 60000 [email protected]
[email protected]'s password: 
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-77-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
Last login: Thu May  4 10:48:04 2017
takatoh@tk2-254-36564:~$

OK。
それじゃこんどはパスワード認証をやめて、公開鍵での認証に切り替える。

takatoh@tk2-254-36564:~$ pwd
/home/takatoh
takatoh@tk2-254-36564:~$ mkdir .ssh
takatoh@tk2-254-36564:~$ chmod 700 .ssh
takatoh@tk2-254-36564:~$ cd .ssh
takatoh@tk2-254-36564:~/.ssh$ wget -O authorized_keys https://github.com/takatoh.keys
--2017-05-04 11:08:43--  https://github.com/takatoh.keys
Resolving github.com (github.com)... 192.30.255.113, 192.30.255.112
Connecting to github.com (github.com)|192.30.255.113|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/plain]
Saving to: 'authorized_keys'

authorized_keys         [ <=>                ]     381  --.-KB/s    in 0.001s  

2017-05-04 11:08:43 (309 KB/s) - 'authorized_keys' saved [381]

takatoh@tk2-254-36564:~/.ssh$ ls -al
total 12
drwx------ 2 takatoh takatoh 4096 May  4 11:08 .
drwxr-xr-x 4 takatoh takatoh 4096 May  4 11:08 ..
-rw-rw-r-- 1 takatoh takatoh  381 May  4 11:08 authorized_keys
takatoh@tk2-254-36564:~/.ssh$ chmod 600 authorized_keys
takatoh@tk2-254-36564:~/.ssh$ ls -al
total 12
drwx------ 2 takatoh takatoh 4096 May  4 11:08 .
drwxr-xr-x 4 takatoh takatoh 4096 May  4 11:08 ..
-rw------- 1 takatoh takatoh  381 May  4 11:08 authorized_keys

ここでいったんログアウトして、鍵認証でログインできるか試してみる。

takatoh@envelopes $ ssh -p 60000 [email protected]
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-77-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
Last login: Thu May  4 11:05:11 2017 from 58.1.225.90

OK。それじゃもう一度 /etc/ssh/sshd_config を編集して、パスワード認証をしないことにする。

PasswordAuthentication no

sshd の再起動。

takatoh@tk2-254-36564:~$ sudo systemctl restart sshd

とりあえずここまで。

結局PCを買い換えた

先週の金曜日に起動しなくなってしまったメインマシン(apostrophe)、土曜日に DELL のサポートに電話してやり取りした結果、DELL に送って修理が必要ということになった。で、どのくらいかかるのかときいたら、期間にして2週間、費用にして3〜4万円(サポート期間が過ぎているので有償)という回答だった。
少し考えたけど、そんなにかかるなら買ってしまったほうがいい。DELL の即納モデルなら1週間もすれば届くだろうし、ちょうど春の割引キャンペーンをやっているので本体だけなら4万円をちょっと超えるくらいだ。
というわけで、一昨日届いた。昨日は時間がなかったので、今日セットアップ。プリインストールの Windows 10 のリカバリメディア(何故か USB フラッシュメモリしか選べないので急いで買ってきた)を作っておいて、Windows はさっくり削除して Ubuntu 16.04 LTS をインストールした。とりあえずは最低限のセットアップが終わったところ。あとは時間を見て環境を整えていこう。
あ、ホスト名は envelopes にした。
それにしても出費が続くなぁ。

メインマシン(Ubuntu)が起動しなくなった

今朝のこと。Ubuntu 16.04 のメインマシン(apostrophe)を見るとソフトウェアのアップデートが表示されていたので、アップデートした。すると、アップデートを完了するにはマシンの再起動が必要だというので、再起動したところ、起動しなくなってしまった。DELL のロゴマークが表示された状態で止まっていて、それっきり動きがない。
ともかく朝で時間がないので、そのまま様子を見ることにして(つまりほうっておいて)出かけたわけだ。
で、今帰ってきてみたら今朝と同じ状態のまま。マシンの電源を切ったり入れたりしてみるも変化がない。どういうわけだ。明日明後日と用事があってまとまった時間が取れそうにないんだけど、最悪再インストールかも。どうしたもんかね。

[追記]

Ubuntu のインストールディスクを入れて起動してみたけどダメだった。というか、DELL のロゴマークが表示されている状態で止まってるってここは、OS 以前の BIOS の途中で止まってるってことじゃなかろうか。こりゃダメかな。

Ubuntu 16.04にNokogiriがインストールできない

rubygems の Nokogiri のインストールで躓いた。環境は:

  • Ubuntu 16.04 LTS
  • Ruby 2.3.1
  • Gem 2.5.1

いろいろ試した結果、次のパッケージを sudo apt install したらインストールできた。

  • ruby-dev
  • libxml2
  • libxml2-dev
  • zlib1g-dev

これらをインストールしたうえで、Nokogiri をインストール。

takatoh@apostrophe $ sudo gem install nokogiri
Building native extensions.  This could take a while...
Successfully installed nokogiri-1.7.0.1
Parsing documentation for nokogiri-1.7.0.1
Installing ri documentation for nokogiri-1.7.0.1
Done installing documentation for nokogiri after 8 seconds
1 gem installed

Ubuntu 16.04をクリーンインストール

apostrophe で動いていた WEB アプリ2つの wplj への引っ越しが終わったので、apostrophe の方も Ubuntu 14.04 LTS から 16.04 LTS にアップグレードした。というか、これまでは Ubuntu と Windows 7 のデュアルブート体制だったのを、Windows はさっぱりと消して Ubuntu をクリーンインストールした。
詳細は wplj へのインストールの時と同じなので割愛。あとは少しずつ環境を整えていこう。

Railsアプリの引っ越し(4)デーモンとして動かす編

起動スクリプトを、これも 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 ファイルを書き換えるだけだ。

Railsアプリの引っ越し(3)Unicornで動かす編

Unicorn のインストール。

tonzlr@wplj:~/tonzlr$ sudo apt install unicorn
[sudo] tonzlr のパスワード: 
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています                
状態情報を読み取っています... 完了
以下のパッケージが自動でインストールされましたが、もう必要とされていません:
  libpango1.0-0 libpangox-1.0-0 ubuntu-core-launcher
これを削除するには 'sudo apt autoremove' を利用してください。
以下の追加パッケージがインストールされます:
  ruby-kgio ruby-rack ruby-raindrops
以下のパッケージが新たにインストールされます:
  ruby-kgio ruby-rack ruby-raindrops unicorn
アップグレード: 0 個、新規インストール: 4 個、削除: 0 個、保留: 8 個。
269 kB のアーカイブを取得する必要があります。
この操作後に追加で 898 kB のディスク容量が消費されます。
続行しますか? [Y/n] Y
取得:1 http://jp.archive.ubuntu.com/ubuntu xenial/universe amd64 ruby-kgio amd64 2.10.0-1build1 [27.7 kB]
取得:2 http://jp.archive.ubuntu.com/ubuntu xenial/universe amd64 ruby-rack all 1.6.4-3 [81.3 kB]
取得:3 http://jp.archive.ubuntu.com/ubuntu xenial/universe amd64 ruby-raindrops amd64 0.16.0-1build1 [33.0 kB]
取得:4 http://jp.archive.ubuntu.com/ubuntu xenial/universe amd64 unicorn amd64 4.9.0-2build2 [127 kB]
269 kB を 0秒 で取得しました (1,720 kB/s)
以前に未選択のパッケージ ruby-kgio を選択しています。
(データベースを読み込んでいます ... 現在 216599 個のファイルとディレクトリがインストールされています。)
.../ruby-kgio_2.10.0-1build1_amd64.deb を展開する準備をしています ...
ruby-kgio (2.10.0-1build1) を展開しています...
以前に未選択のパッケージ ruby-rack を選択しています。
.../ruby-rack_1.6.4-3_all.deb を展開する準備をしています ...
ruby-rack (1.6.4-3) を展開しています...
以前に未選択のパッケージ ruby-raindrops を選択しています。
.../ruby-raindrops_0.16.0-1build1_amd64.deb を展開する準備をしています ...
ruby-raindrops (0.16.0-1build1) を展開しています...
以前に未選択のパッケージ unicorn を選択しています。
.../unicorn_4.9.0-2build2_amd64.deb を展開する準備をしています ...
unicorn (4.9.0-2build2) を展開しています...
man-db (2.7.5-1) のトリガを処理しています ...
systemd (229-4ubuntu16) のトリガを処理しています ...
ureadahead (0.100.0-19) のトリガを処理しています ...
ureadahead will be reprofiled on next reboot
ruby-kgio (2.10.0-1build1) を設定しています ...
ruby-rack (1.6.4-3) を設定しています ...
ruby-raindrops (0.16.0-1build1) を設定しています ...
unicorn (4.9.0-2build2) を設定しています ...
insserv: warning: script 'K01bruschetta' missing LSB tags and overrides
insserv: warning: script 'bruschetta' missing LSB tags and overrides
insserv: warning: script 'K01bruschetta' missing LSB tags and overrides
insserv: warning: current start runlevel(s) (empty) of script `unicorn' overrides LSB defaults (2 3 4 5).
insserv: warning: current stop runlevel(s) (0 1 2 3 4 5 6) of script `unicorn' overrides LSB defaults (0 1 6).
insserv: warning: script 'bruschetta' missing LSB tags and overrides
systemd (229-4ubuntu16) のトリガを処理しています ...
ureadahead (0.100.0-19) のトリガを処理しています ...

apostrophe から設定ファイル unicorn.conf をコピー。

tonzlr@wplj:~/tonzlr$ scp tonzlr@apostrophe:tonzlr/unicorn.conf .
tonzlr@apostrophe's password: 
unicorn.conf                                  100%  166     0.2KB/s   00:00

たぶん、変更しないでいいはず。

tonzlr@wplj:~/tonzlr$ cat unicorn.conf
listen "9009"
worker_processes 1
working_directory "/home/tonzlr/tonzlr"
pid "./unicorn.pid"
stdout_path "./unicorn.log"
stderr_path "./unicorn.log"
preload_app true
tonzlr@wplj:~/tonzlr$ bundle exec unicorn_rails -c unicorn.conf -E production

OK。ブラウザで http://localhost:9009/ にアクセスして確認できた。