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

Rails アプリを unicorn で動かす。

unicornのインストールと設定

Gemfile に unicorn を追加。

インストール。

[root@tk2-254-36564 lathercraft]# bundle install --path vendor/bundle

unicorn の設定ファイルはつぎのようにした。

/var/run/unicorn ディレクトリを作成。

[root@tk2-254-36564 lathercraft]# mkdir /var/run/unicorn

Nginxの設定

ログファイル用のディレクトリを作成。

[root@tk2-254-36564 lathercraft]# mkdir /var/log/nginx/www.lathercraft.net

Nginxの再起動とunicornの起動

Nginx を再起動。

[root@tk2-254-36564 lathercraft]# service nginx restart
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]

unicorn を起動。

[root@tk2-254-36564 lathercraft]# export SECRET_KEY_BASE=bundle exec rails secret
[root@tk2-254-36564 lathercraft]# bundle exec unicorn_rails -c unicorn.rb -E production
I, [2017-05-05T11:58:43.619697 #13435]  INFO -- : Refreshing Gem list
DEPRECATION WARNING: The configuration option config.serve_static_assets has been renamed to config.serve_static_files to clarify its role (it merely enables serving everything in the public folder and is unrelated to the asset pipeline). The serve_static_assets alias will be removed in Rails 5.0. Please migrate your configuration files accordingly. (called from block in  at /var/www/lathercraft/config/environments/production.rb:23)
I, [2017-05-05T11:58:46.475292 #13435]  INFO -- : listening on addr=/var/run/unicorn/unicorn_lathercraft.sock fd=10
I, [2017-05-05T11:58:46.475781 #13435]  INFO -- : worker=0 spawning...
I, [2017-05-05T11:58:46.479485 #13435]  INFO -- : worker=1 spawning...
I, [2017-05-05T11:58:46.481100 #13438]  INFO -- : worker=0 spawned pid=13438
I, [2017-05-05T11:58:46.481436 #13435]  INFO -- : master process ready
I, [2017-05-05T11:58:46.481630 #13438]  INFO -- : worker=0 ready
I, [2017-05-05T11:58:46.482935 #13441]  INFO -- : worker=1 spawned pid=13441
I, [2017-05-05T11:58:46.483183 #13441]  INFO -- : worker=1 ready

さて、これでいいはず。

名前解決

ちゃんと動いているはずだけど、確認するには www2.lathercraft.net の名前解決化できないといけない。これはさくらインターネットのゾーン編集をする。

……結果、無事動いていることを確認。

unicornの自動起動

最後に、OS起動時に unicorn が自動起動するようにしておく。/etc/init.d/unicorn_lathercraft をつぎのようにした。

これでいいはず。スタートしてみよう。

[root@tk2-254-36564 init.d]# chmod 755 unicorn_lathercraft
[root@tk2-254-36564 init.d]# service unicorn_lathercraft start

OK。
自動起動するように設定。

[root@tk2-254-36564 init.d]# chkconfig unicorn_lathercraft on
[root@tk2-254-36564 init.d]# chkconfig --list unicorn_lathercraft
unicorn_lathercraft	0:off	1:off	2:on	3:on	4:on	5:on	6:off

これで完了。

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

新しいプランのサーバで Rails アプリが一応動くようになったので、旧プランのサーバからデータを移行する。

まずは mysqldump コマンドで旧プランのデータベースを取り出す。

[takatoh@www2465uo ~]$ mysqldump -u lathercraft -p lathercraft_production > lcp-20170505.sql
[takatoh@www2465uo ~]$ tar czf lcp-20170505.sql.tgz lcp-20170505.sql

ファイルをローカルにダウンロード。

takatoh@envelopes $ scp -P xxxxx takatoh@www.lathercraft.net:lcp-20170505.sql.tgz .
lcp-20170505.sql.tgz                          100%   10MB  10.1MB/s   00:01

ダウンロードしたファイルを新プランのサーバにアップロード。

takatoh@envelopes $ scp -P 60000 lcp-20170505.sql.tgz takatoh@160.16.224.68:
lcp-20170505.sql.tgz                          100%   10MB  10.1MB/s   00:01

新プランのサーバにログインして、.tgz ファイルを展開。

takatoh@envelopes $ ssh -p 60000 takatoh@160.16.224.68
Last login: Fri May  5 11:06:46 2017 from fntsitm001026.sitm.fnt.ngn.ppp.infoweb.ne.jp

SAKURA Internet [Virtual Private Server SERVICE]

[takatoh@tk2-254-36564 ~]$ ls
lcp-20170505.sql.tgz
[takatoh@tk2-254-36564 ~]$ tar xzf lcp-20170505.sql.tgz
[takatoh@tk2-254-36564 ~]$ ls
lcp-20170505.sql  lcp-20170505.sql.tgz

データベースにリストアする。

[takatoh@tk2-254-36564 ~]$ mysql -u lathercraft -p lathercraft_production < lcp-20170505.sql
Enter password:

アプリを立ち上げて確認。

[root@tk2-254-36564 lathercraft]# export SECRET_KEY_BASE=bundle exec rake secret
[root@tk2-254-36564 lathercraft]# bundle exec rails s -e production -b 0.0.0.0
=> Booting WEBrick
=> Rails 4.2.8 application starting in production on http://0.0.0.0:3000
=> Run rails server -h for more startup options
=> Ctrl-C to shutdown server
DEPRECATION WARNING: The configuration option config.serve_static_assets has been renamed to config.serve_static_files to clarify its role (it merely enables serving everything in the public folder and is unrelated to the asset pipeline). The serve_static_assets alias will be removed in Rails 5.0. Please migrate your configuration files accordingly. (called from block in  at /var/lathercraft/config/environments/production.rb:23)
[2017-05-05 11:18:51] INFO  WEBrick 1.3.1
[2017-05-05 11:18:51] INFO  ruby 2.3.3 (2016-11-21) [x86_64-linux]
[2017-05-05 11:18:51] INFO  WEBrick::HTTPServer#start: pid=12500 port=3000

ブラウザでも確認出来てOK。

ところでうえで気がついたんだけど、アプリを /var/www/lathercraft 以下に置くつもりが /var/lathercraft 以下においてしまっている。なので移動する。

[root@tk2-254-36564 var]# cp -r -a lathercraft www/lathercraft
[root@tk2-254-36564 var]# cd www/lathercraft
[root@tk2-254-36564 lathercraft]# ls
Gemfile       README.rdoc  app  config     db   log     test  vendor
Gemfile.lock  Rakefile     bin  config.ru  lib  public  tmp

再度アプリを立ち上げて確認。

[root@tk2-254-36564 lathercraft]# bundle exec rails s -e production -b 0.0.0.0

OK。

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

昨日、MySQL と Ruby のインストールが完了したので、今日は Rails アプリを動作させるところまでが目標。と言っても、現状(旧プランの VPS で)動いているアプリを真似ればいいはず。

development でテスト

いきなり本番でやるのではなくまずはテスト。
/var/www に移動して BitBucket からクローン。

[takatoh@tk2-254-36564 ~]$ cd /var
[takatoh@tk2-254-36564 var]$ sudo mkdir www
[takatoh@tk2-254-36564 var]$ sudo -s
[root@tk2-254-36564 var]# git clone https://takatoh@bitbucket.org/takatoh/lathercraft.git

ライブラリのインストール。

[root@tk2-254-36564 lathercraft]# bundle install --path vendor/bundle

設定ファイルを編集。

[root@tk2-254-36564 lathercraft]# cp config/secrets.yml.example config/secrets.yml
[root@tk2-254-36564 lathercraft]# cp config/site_config.yml.example config/site_config.yml
[root@tk2-254-36564 lathercraft]# vi config/site_config.yml

データベースのマイグレーション。

[root@tk2-254-36564 lathercraft]# bundle exec rake db:migrate

とりあえずこれでいいはず。iptables でポート 3000 番を開放して、アプリを立ち上げてみる。

[root@tk2-254-36564 lathercraft]# bundle exec rails s -b 0.0.0.0
=> Booting WEBrick
=> Rails 4.2.8 application starting in development on http://0.0.0.0:3000
=> Run rails server -h for more startup options
=> Ctrl-C to shutdown server
[2017-05-05 09:53:11] INFO  WEBrick 1.3.1
[2017-05-05 09:53:11] INFO  ruby 2.3.3 (2016-11-21) [x86_64-linux]
[2017-05-05 09:53:11] INFO  WEBrick::HTTPServer#start: pid=10211 port=3000

OK。うまく動いた。

データベースの下準備

本番では、データベースに Sqlite3 じゃなく MySQL を使うので、データベースユーザを作っておく。

[root@tk2-254-36564 lathercraft]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE USER 'lathercraft'@'localhost' IDENTIFIED BY 'xxxxxxxx';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

設定ファイルの編集とデータベースのマイグレーション

本番用に設定ファイルを編集。

[root@tk2-254-36564 lathercraft]# vi config/site_config.yml
[root@tk2-254-36564 lathercraft]# vi config/database.yml

config/database.yml はこうした。

MySQL 用のライブラリがインストールされていないのでインストール。

[root@tk2-254-36564 lathercraft]# gem install mysql2
Fetching: mysql2-0.4.6.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing mysql2:
	ERROR: Failed to build gem native extension.

    current directory: /usr/local/rvm/gems/ruby-2.3.3/gems/mysql2-0.4.6/ext/mysql2
/usr/local/rvm/rubies/ruby-2.3.3/bin/ruby -r ./siteconf20170505-10249-1r6ezbc.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for rb_big_cmp()... yes
-----
Using mysql_config at /usr/bin/mysql_config
-----
checking for mysql.h... no
checking for mysql/mysql.h... no
-----
mysql.h is missing. You may need to 'apt-get install libmysqlclient-dev' or 'yum install mysql-devel', and try again.
-----
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
	--with-opt-dir
	--without-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/usr/local/rvm/rubies/ruby-2.3.3/bin/$(RUBY_BASE_NAME)
	--with-mysql-dir
	--without-mysql-dir
	--with-mysql-include
	--without-mysql-include=${mysql-dir}/include
	--with-mysql-lib
	--without-mysql-lib=${mysql-dir}/lib
	--with-mysql-config
	--without-mysql-config

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /usr/local/rvm/gems/ruby-2.3.3/extensions/x86_64-linux/2.3.0/mysql2-0.4.6/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /usr/local/rvm/gems/ruby-2.3.3/gems/mysql2-0.4.6 for inspection.
Results logged to /usr/local/rvm/gems/ruby-2.3.3/extensions/x86_64-linux/2.3.0/mysql2-0.4.6/gem_make.out

あれ、エラーだ。yum install mysql-devel しろってか。

[root@tk2-254-36564 lathercraft]# yum install mysql-devel

もう一度。

[root@tk2-254-36564 lathercraft]# gem install mysql2
Building native extensions.  This could take a while...
Successfully installed mysql2-0.4.6
Parsing documentation for mysql2-0.4.6
Installing ri documentation for mysql2-0.4.6
Done installing documentation for mysql2 after 1 seconds
1 gem installed

OK。
じゃ、データベースの作成とマイグレーション。

[root@tk2-254-36564 lathercraft]# bundle exec rake db:create RAILS_ENV=production
Specified 'mysql2' for database adapter, but the gem is not loaded. Add gem 'mysql2' to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/connection_adapters/connection_specification.rb:177:in rescue in spec'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/connection_adapters/connection_specification.rb:174:in spec'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/connection_handling.rb:50:in establish_connection'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/tasks/mysql_database_tasks.rb:8:in establish_connection'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/tasks/mysql_database_tasks.rb:15:in create'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/tasks/database_tasks.rb:93:in create'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/tasks/database_tasks.rb:107:in block in create_current'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/tasks/database_tasks.rb:276:in block in each_current_configuration'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/tasks/database_tasks.rb:275:in each'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/tasks/database_tasks.rb:275:in each_current_configuration'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/tasks/database_tasks.rb:106:in create_current'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/railties/databases.rake:17:in block (2 levels) in '
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:250:in block in execute'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:250:in each'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:250:in execute'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:194:in block in invoke_with_call_chain'
/usr/local/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/monitor.rb:214:in mon_synchronize'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:187:in invoke_with_call_chain'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:180:in invoke'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:152:in invoke_task'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:108:in block (2 levels) in top_level'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:108:in each'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:108:in block in top_level'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:117:in run_with_threads'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:102:in top_level'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:80:in block in run'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:178:in standard_exception_handling'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:77:in run'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/exe/rake:27:in '
/var/lathercraft/vendor/bundle/ruby/2.3.0/bin/rake:23:in load'
/var/lathercraft/vendor/bundle/ruby/2.3.0/bin/rake:23:in '
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:74:in load'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:74:in kernel_load'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:27:in run'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli.rb:335:in exec'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/command.rb:27:in run'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in invoke_command'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor.rb:359:in dispatch'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli.rb:20:in dispatch'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/base.rb:440:in start'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli.rb:11:in start'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/exe/bundle:32:in block in '
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/friendly_errors.rb:121:in with_friendly_errors'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/exe/bundle:24:in '
/usr/local/rvm/gems/ruby-2.3.3/bin/bundle:23:in load'
/usr/local/rvm/gems/ruby-2.3.3/bin/bundle:23:in 
' /usr/local/rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in eval' /usr/local/rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in
' Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "database"=>"lathercraft_production", "username"=>"lathercraft", "password"=>"lathercraft", "host"=>"localhost"} rake aborted! Gem::LoadError: Specified 'mysql2' for database adapter, but the gem is not loaded. Add gem 'mysql2' to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). /var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/connection_adapters/connection_specification.rb:177:in rescue in spec' /var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/connection_adapters/connection_specification.rb:174:in spec' /var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/connection_handling.rb:50:in establish_connection' /var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/tasks/database_tasks.rb:109:in create_current' /var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/railties/databases.rake:17:in block (2 levels) in ' /var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/exe/rake:27:in ' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:74:in load' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:74:in kernel_load' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:27:in run' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli.rb:335:in exec' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/command.rb:27:in run' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in invoke_command' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor.rb:359:in dispatch' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli.rb:20:in dispatch' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/base.rb:440:in start' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli.rb:11:in start' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/exe/bundle:32:in block in ' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/friendly_errors.rb:121:in with_friendly_errors' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/exe/bundle:24:in ' /usr/local/rvm/gems/ruby-2.3.3/bin/bundle:23:in load' /usr/local/rvm/gems/ruby-2.3.3/bin/bundle:23:in
' /usr/local/rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in eval' /usr/local/rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in
' Gem::LoadError: mysql2 is not part of the bundle. Add it to Gemfile. /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/rubygems_integration.rb:351:in block (2 levels) in replace_gem' /var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/connection_adapters/mysql2_adapter.rb:3:in ' /var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:274:in require' /var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:274:in block in require' /var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:240:in load_dependency' /var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:274:in require' /var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/connection_adapters/connection_specification.rb:175:in spec' /var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/connection_handling.rb:50:in establish_connection' /var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/tasks/database_tasks.rb:109:in create_current' /var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/railties/databases.rake:17:in block (2 levels) in ' /var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/exe/rake:27:in ' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:74:in load' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:74:in kernel_load' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:27:in run' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli.rb:335:in exec' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/command.rb:27:in run' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in invoke_command' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor.rb:359:in dispatch' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli.rb:20:in dispatch' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/base.rb:440:in start' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli.rb:11:in start' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/exe/bundle:32:in block in ' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/friendly_errors.rb:121:in with_friendly_errors' /usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/exe/bundle:24:in ' /usr/local/rvm/gems/ruby-2.3.3/bin/bundle:23:in load' /usr/local/rvm/gems/ruby-2.3.3/bin/bundle:23:in
' /usr/local/rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in eval' /usr/local/rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in
' Tasks: TOP => db:create (See full trace by running task with --trace)

あー、mysql2 を Gemfile に書いておかなきゃダメなのか。

[root@tk2-254-36564 lathercraft]# vi Gemfile
[root@tk2-254-36564 lathercraft]# bundle install --path vendor/bundle

これでどうだ。

[root@tk2-254-36564 lathercraft]# bundle exec rake db:create RAILS_ENV=production
Mysql2::Error: Access denied for user 'lathercraft'@'localhost' to database 'lathercraft_production': CREATE DATABASE lathercraft_production DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.6/lib/mysql2/client.rb:120:in _query'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.6/lib/mysql2/client.rb:120:in block in query'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.6/lib/mysql2/client.rb:119:in handle_interrupt'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.6/lib/mysql2/client.rb:119:in query'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:309:in block in execute'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:484:in block in log'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/notifications/instrumenter.rb:20:in instrument'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:478:in log'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:309:in execute'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/connection_adapters/mysql2_adapter.rb:225:in execute'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:377:in create_database'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/tasks/mysql_database_tasks.rb:16:in create'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/tasks/database_tasks.rb:93:in create'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/tasks/database_tasks.rb:107:in block in create_current'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/tasks/database_tasks.rb:276:in block in each_current_configuration'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/tasks/database_tasks.rb:275:in each'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/tasks/database_tasks.rb:275:in each_current_configuration'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/tasks/database_tasks.rb:106:in create_current'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.8/lib/active_record/railties/databases.rake:17:in block (2 levels) in '
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:250:in block in execute'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:250:in each'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:250:in execute'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:194:in block in invoke_with_call_chain'
/usr/local/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/monitor.rb:214:in mon_synchronize'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:187:in invoke_with_call_chain'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:180:in invoke'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:152:in invoke_task'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:108:in block (2 levels) in top_level'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:108:in each'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:108:in block in top_level'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:117:in run_with_threads'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:102:in top_level'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:80:in block in run'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:178:in standard_exception_handling'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:77:in run'
/var/lathercraft/vendor/bundle/ruby/2.3.0/gems/rake-12.0.0/exe/rake:27:in '
/var/lathercraft/vendor/bundle/ruby/2.3.0/bin/rake:23:in load'
/var/lathercraft/vendor/bundle/ruby/2.3.0/bin/rake:23:in '
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:74:in load'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:74:in kernel_load'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:27:in run'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli.rb:335:in exec'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/command.rb:27:in run'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in invoke_command'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor.rb:359:in dispatch'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli.rb:20:in dispatch'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/base.rb:440:in start'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/cli.rb:11:in start'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/exe/bundle:32:in block in '
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/lib/bundler/friendly_errors.rb:121:in with_friendly_errors'
/usr/local/rvm/gems/ruby-2.3.3/gems/bundler-1.14.6/exe/bundle:24:in '
/usr/local/rvm/gems/ruby-2.3.3/bin/bundle:23:in load'
/usr/local/rvm/gems/ruby-2.3.3/bin/bundle:23:in 
' /usr/local/rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in eval' /usr/local/rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in
' Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "database"=>"lathercraft_production", "username"=>"lathercraft", "password"=>"lathercraft", "host"=>"localhost"}

ダメだ。なんでだ……あ、データベースユーザに権限がないからか?

[root@tk2-254-36564 lathercraft]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE DATABASE lathercraft_production;
Query OK, 1 row affected (0.00 sec)

mysql> GRANT ALL ON lathercraft_production.* To 'lathercraaft'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

ついでに空のデータベースもつくちゃった。
ではマイグレーション。

[root@tk2-254-36564 lathercraft]# export SECRET_KEY_BASE=bundle exec rake secret
[root@tk2-254-36564 lathercraft]# bundle exec rake db:migrate RAILS_ENV=production
DEPRECATION WARNING: The configuration option config.serve_static_assets has been renamed to config.serve_static_files to clarify its role (it merely enables serving everything in the public folder and is unrelated to the asset pipeline). The serve_static_assets alias will be removed in Rails 5.0. Please migrate your configuration files accordingly. (called from block in  at /var/lathercraft/config/environments/production.rb:23)
== 20140708134805 CreateBrands: migrating =====================================
-- create_table(:brands)
   -> 0.6798s
-- add_index(:brands, :name, {:unique=>true})
   -> 0.0727s
-- add_index(:brands, :slug, {:unique=>true})
   -> 0.0407s
== 20140708134805 CreateBrands: migrated (0.7937s) ============================

== 20140708135345 CreateProducts: migrating ===================================
-- create_table(:products)
   -> 0.0093s
-- add_index(:products, [:brand_id, :slug], {:unique=>true})
   -> 0.8482s
== 20140708135345 CreateProducts: migrated (0.8579s) ==========================

== 20140708140110 CreateItems: migrating ======================================
-- create_table(:items)
   -> 0.0908s
-- add_index(:items, :md5, {:unique=>true})
   -> 0.3057s
== 20140708140110 CreateItems: migrated (0.3968s) =============================

== 20140708215016 DeviseCreateUsers: migrating ================================
-- create_table(:users)
   -> 0.0661s
-- add_index(:users, :email, {:unique=>true})
   -> 0.5320s
-- add_index(:users, :reset_password_token, {:unique=>true})
   -> 0.2549s
-- add_index(:users, :confirmation_token, {:unique=>true})
   -> 0.1549s
== 20140708215016 DeviseCreateUsers: migrated (1.0086s) =======================

== 20140719083223 CreateCategories: migrating =================================
-- create_table(:categories)
   -> 0.1566s
== 20140719083223 CreateCategories: migrated (0.1568s) ========================

== 20140719085230 AddCategoryIdToItem: migrating ==============================
-- add_column(:items, :category_id, :integer)
   -> 0.5393s
== 20140719085230 AddCategoryIdToItem: migrated (0.5394s) =====================

== 20140720065147 AddAdminToUsers: migrating ==================================
-- add_column(:users, :admin, :boolean, {:null=>false, :default=>false})
   -> 1.0957s
== 20140720065147 AddAdminToUsers: migrated (1.0958s) =========================

== 20140720134222 CreateStorages: migrating ===================================
-- create_table(:storages)
   -> 0.3241s
== 20140720134222 CreateStorages: migrated (0.3243s) ==========================

== 20140721073254 CreateDownloads: migrating ==================================
-- create_table(:downloads)
   -> 0.1023s
== 20140721073254 CreateDownloads: migrated (0.1025s) =========================

== 20140721080127 CreateStoredItems: migrating ================================
-- create_table(:stored_items)
   -> 0.1760s
== 20140721080127 CreateStoredItems: migrated (0.1762s) =======================

== 20140801123109 CreateRemovedItems: migrating ===============================
-- create_table(:removed_items)
   -> 0.2218s
== 20140801123109 CreateRemovedItems: migrated (0.2219s) ======================

== 20140801132346 AddBrandToRemovedItems: migrating ===========================
-- add_column(:removed_items, :brand, :string)
   -> 0.5079s
== 20140801132346 AddBrandToRemovedItems: migrated (0.5081s) ==================

== 20140802001131 AddNameToUsers: migrating ===================================
-- add_column(:users, :name, :string)
   -> 0.1279s
-- add_index(:users, :name, {:unique=>true})
   -> 0.4112s
== 20140802001131 AddNameToUsers: migrated (0.5393s) ==========================

== 20140802070045 CreateContacts: migrating ===================================
-- create_table(:contacts)
   -> 0.0461s
== 20140802070045 CreateContacts: migrated (0.0462s) ==========================

== 20140808134556 AddOnHoldToItems: migrating =================================
-- add_column(:items, :on_hold, :boolean)
   -> 0.2378s
== 20140808134556 AddOnHoldToItems: migrated (0.2380s) ========================

== 20141029120011 AddCopyrightToProducts: migrating ===========================
-- add_column(:products, :copyright, :string)
   -> 0.1440s
== 20141029120011 AddCopyrightToProducts: migrated (0.1441s) ==================

== 20141030114708 AddR18ToProducts: migrating =================================
-- add_column(:products, :r18, :boolean)
   -> 0.1771s
== 20141030114708 AddR18ToProducts: migrated (0.1772s) ========================

== 20141106130350 CreateAdditionalLinks: migrating ============================
-- create_table(:additional_links)
   -> 0.1317s
== 20141106130350 CreateAdditionalLinks: migrated (0.1318s) ===================

== 20141213055526 AddRemoteAddrToDownloads: migrating =========================
-- add_column(:downloads, :remote_addr, :string)
   -> 0.0770s
== 20141213055526 AddRemoteAddrToDownloads: migrated (0.0772s) ================

== 20151022105014 ChangeColumnOfItems: migrating ==============================
-- change_column(:items, :file_size, :integer, {:limit=>8})
   -> 0.2759s
== 20151022105014 ChangeColumnOfItems: migrated (0.2761s) =====================

== 20160601172945 AddReferrerToDownloads: migrating ===========================
-- add_column(:downloads, :referrer, :string)
   -> 0.1129s
== 20160601172945 AddReferrerToDownloads: migrated (0.1130s) ==================

== 20160603235736 AddDownloadsToRemovedItems: migrating =======================
-- add_column(:removed_items, :downloads, :integer)
   -> 0.0603s
== 20160603235736 AddDownloadsToRemovedItems: migrated (0.0604s) ==============

よし。

確認

アプリを production 環境で立ち上げてみよう。

[root@tk2-254-36564 lathercraft]# bundle exec rails s -e production -b 0.0.0.0
=> Booting WEBrick
=> Rails 4.2.8 application starting in production on http://0.0.0.0:3000
=> Run rails server -h for more startup options
=> Ctrl-C to shutdown server
DEPRECATION WARNING: The configuration option config.serve_static_assets has been renamed to config.serve_static_files to clarify its role (it merely enables serving everything in the public folder and is unrelated to the asset pipeline). The serve_static_assets alias will be removed in Rails 5.0. Please migrate your configuration files accordingly. (called from block in  at /var/lathercraft/config/environments/production.rb:23)
[2017-05-05 10:34:34] INFO  WEBrick 1.3.1
[2017-05-05 10:34:34] INFO  ruby 2.3.3 (2016-11-21) [x86_64-linux]
[2017-05-05 10:34:34] INFO  WEBrick::HTTPServer#start: pid=12017 port=3000

OK。ブラウザからも確認できた。
警告が出てるのはアセット関連だな。ついでに直しておこうか。

[root@tk2-254-36564 lathercraft]# cd config/environments
[root@tk2-254-36564 environments]# vi production.rb

production.rb はつぎの行を修正した(true に変更)。

アセットのプリコンパイル。

[root@tk2-254-36564 environments]# bundle exec rake assets:precompile
(in /var/lathercraft)
I, [2017-05-05T10:45:12.773119 #12052]  INFO -- : Writing /var/lathercraft/public/assets/application-9d56a9000f8aea926db88f2516d6dbf4.js
I, [2017-05-05T10:45:12.852083 #12052]  INFO -- : Writing /var/lathercraft/public/assets/application-f83f6d0d6aec02ba6b82a5bb6d9cf65a.css

もう一度アプリを立ち上げて確認。

[root@tk2-254-36564 environments]# bundle exec rails s -e production -b 0.0.0.0
=> Booting WEBrick
=> Rails 4.2.8 application starting in production on http://0.0.0.0:3000
=> Run rails server -h for more startup options
=> Ctrl-C to shutdown server
DEPRECATION WARNING: The configuration option config.serve_static_assets has been renamed to config.serve_static_files to clarify its role (it merely enables serving everything in the public folder and is unrelated to the asset pipeline). The serve_static_assets alias will be removed in Rails 5.0. Please migrate your configuration files accordingly. (called from block in  at /var/lathercraft/config/environments/production.rb:23)
[2017-05-05 10:46:02] INFO  WEBrick 1.3.1
[2017-05-05 10:46:02] INFO  ruby 2.3.3 (2016-11-21) [x86_64-linux]
[2017-05-05 10:46:02] INFO  WEBrick::HTTPServer#start: pid=12055 port=3000

OK。ちゃんとスタイルシートも適用されるようになった。
とりあえずここで休憩。

さくらの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

Rails 4.2.8にしたら外部からアクセスできなくなった

Rails を 4.1.4 から 4.2.8(4.2 系の最新版)にアップデートしたら、rails s で起動したサーバに外部(ほかの PC)からアクセスできなくなっていた。4.2 で挙動が変わったらしい。

 cf. rails sで起動したサーバにブラウザからアクセスできない – Qiita

上の参考ページにあるように、-b オプションをつければいい。

takatoh@envelopes $ bundle exec rails s -b 0.0.0.0

球面上の点を平面上にステレオ投影する

先週のさらに続き。先週は、球面上の点を平面上に投影するのに X, Y 座標をそのまま使って水平投影したけど、今度はステレオ投影というのをやってみる。
ステレオ投影は Wikipedia に詳しく載っている。
先週と同じように、下半球の点 300 個を XY 平面(赤道面)上に投影する。やってみたのが下の図。

コードは次の通り。

やっつけ仕事なので、ヘルプもないけど、先週の gss_gen コマンド(--cartesian オプション付き)の結果を入力として、投影点の座標を CSV で出力する。

多数の点を球面上に一様分布させる(2)

一昨日の続き。最初と最後の点を極から移動(位置調整)するのを実装した。ついでに、gem 化して RubyGems.org にアップしておいた。gem install gss_generator でインストールできる。
使い方は次の通り。

^o^ > gss_gen --cartesian --relocate 1.0 600 > c600r.csv

--cartesian はデカルト座標系で出力、--relocate は位置調整のオプション。結果は CSV 形式で標準出力に出力するので c600r.csv ファイルにリダイレクトしている。

結果を一昨日と同様の図にしたものを載せる。

最初の点が極(中心)からずれて、一様性が増しているのがわかる。

多数の点を球面上に一様分布させる

ちょっと面白いものを見つけた。

 cf. LEDドームのLEDの並びを決めるのに使用した計算式 – jakaladaのブログ

球面上に任意個の点を一様分布させる、座標を求めるもの。一般化螺旋集合(generalized spiral set)というものを使っている。元の資料(論文)はここ。

 cf. 多数の点を球面上に一様分布させるソフトウェアGSS Generator

上のリンク先のブログでは Python を使っているので、Ruby でやってみた。

最初のがコマンドで、あとの2つがライブラリ。引数に球の半径と配置したい点の個数を指定すると、各点の極座標を CSV 形式で出力する。

^o^ > ruby -Ilib exe/gss_gen 1.0 600

極座標じゃなく、デカルト座標(XYZ座標)がほしい時には --cartesian オプション。

^o^ > ruby -Ilib exe/gss_gen --cartesian 1.0 600

下の図は、発生させた600個の点のうち下半分の300個を XY 平面上に投影した図。

各点を順に線でつないでみると、螺旋になっているのがよくわかる。

……なんか螺旋が逆になってるな。元論文じゃ球面を下から見たと書いてあるからそのせいか?

ともあれ、それらしいのはできた。ただし、最初と最後の点の座標を調整するのはやってない。時間があったらやってみよう。

SinatraでSECURITY WARNING: No secret option provided to Rack::Session::Cookie.というワーニングが出たときの対処法

Sinatra でアプリを作ってるんだけど、セッションを利用するとタイトルのようなワーニングが出た。もっと詳しく書くと次のとおり。

SECURITY WARNING: No secret option provided to Rack::Session::Cookie.
This poses a security threat. It is strongly recommended that you
provide a secret to prevent exploits that may be possible from crafted
cookies. This will not be supported in future versions of Rack, and
future versions will even invalidate your existing user cookies.

ググってみても見つかるのは、Rails のバージョンを上げろとか、Rack のバージョンを 1.4.1 にしろとかいうのばっかり。Rails は使ってないし、Rack は 1.6.5 なんだけどなあ。そんな中で見つけたのが↓このページ。

 cf. SECURITY WARNING: No secret option provided to Rack::Session::Cookie

config.ru で :session_secret に値を設定すればいいらしい。
具体的には次のようにした。

これでめでたくワーニングが出なくなった。

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