Ruby on Rails インストールまとめ
直近2件ほど、Railsのセッティングのお仕事があったので自分的まとめとして。
まず、ざっとおさらい、Ruby on Railsとその仲間たち。
- ruby オブジェクト指向スクリプト言語 - rbenv rubyのバージョンを簡単に切り替えるツール - gem 1. rubyのサードパーティ製のライブラリの形式 2. rubyに付属しているRubyGemsというパッケージングシステムでライブラリの作成・公開・インストールが出来るもの - bundler gemライブラリの種類やバージョンを管理するツール - Ruby on Rails (単にRailsとも) Webシステム向けのフルスタック・フレームワーク - unicorn Railsサーバー、CPUやメモリの消費が小さく高速に動くんだとか 名前がかっこいい - nginx 単なるウェブサーバー
環境
CentOS 6.6
まずはrbenv、そしてrubyをインストール
gitが無ければgitを入れる $ sudo yum install git rbenvをclone @ホームディレクトリ $ git clone git://github.com/sstephenson/rbenv.git .rbenv Path通す $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile rbenv initを呼び出すように設定 $ echo 'eval "$(rbenv init -)"'>>~/.bash_profile rubyをインストールする為にruby-buildをclone $ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build rubyをインストール 今の最新が2.2.0なので最新を入れる $ rbenv install 2.2.0 エラー。。。 (snip) Last 10 log lines: collect2: ld はステータス 1 で終了しました make[2]: *** [../../.ext/x86_64-linux/fiddle.so] エラー 1 make[2]: ディレクトリ `/tmp/ruby-build.20150112005133.14091/ruby-2.2.0/ext/fiddle' から出ます make[1]: *** [ext/fiddle/all] エラー 2 make[1]: *** 未完了のジョブを待っています.... installing default console libraries linking shared-object io/console.so make[2]: ディレクトリ `/tmp/ruby-build.20150112005133.14091/ruby-2.2.0/ext/io/console' から出ます make[1]: ディレクトリ `/tmp/ruby-build.20150112005133.14091/ruby-2.2.0' から出ます make: *** [build-ext] エラー 2 (snip) libffi-develを入れ直してリトライ! $ sudo yum install libffi-devel 成功。 $ rbenv versions 2.2.0 ついでに、2.1.5を入れた時にもエラーが出たのでメモ (snip) installing rdoc: /home/<ユーザー名>/.rbenv/versions/2.1.5/share/ri/2.1.0/system installing capi-docs: /home/<ユーザー名>/.rbenv/versions/2.1.5/share/doc/ruby The Ruby openssl extension was not compiled. Missing the OpenSSL lib? Configure options used: --prefix=/home/<ユーザー名>/.rbenv/versions/2.1.5 LDFLAGS=-L/home/<ユーザー名>/.rbenv/versions/2.1.5/lib CPPFLAGS=-I/home/<ユーザー名>/.rbenv/versions/2.1.5/include (snip) 足りないと言われている物を入れましょう。 $ sudo yum install openssl-devel 完了 $ rbenv versions 2.1.5 2.2.0 バージョンはglobalコマンドで切り替えます $ rbenv global 2.2.0
railsのインストール
$ gem install rails --no-ri --no-rdoc (--no-ri --no-rdocはドキュメントとか省く為。インストールが早くなる) 簡単。 適当に、サンプルのプロジェクトを作ってみる。 $ rails new sample
unicornインストール
$ vi Gemfile これを追記 gem 'unicorn' $ bundle install はい、エラー (snip) Gem::Ext::BuildError: ERROR: Failed to build gem native extension. /home/<ユーザー名>/.rbenv/versions/2.2.0/bin/ruby -r ./siteconf20150112-25247-k3fueb.rb extconf.rb checking for sqlite3.h... no sqlite3.h is missing. Try 'port install sqlite3 +universal', 'yum install sqlite-devel' or 'apt-get install libsqlite3-dev' and check your shared library search path (the location where your sqlite3 shared library is located). *** 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. (snip) 入れろと言われている物をいれる $ sudo yum install sqlite-devel 再実行、成功 $ bundle install config/unicorn.rbを作る。これは適当にググって拾ってきた。 -- config/unicorn.rb -- worker_processes 2 #working_directory /home/www/rails/charag listen File.expand_path("tmp/unicorn.sock", ENV['RAILS_ROOT']) pid File.expand_path("tmp/unicorn.pid", ENV['RAILS_ROOT']) timeout 60 preload_app true # ダウンタイムをなくす stdout_path File.expand_path("log/unicorn.stdout.log", ENV['RAILS_ROOT']) stderr_path File.expand_path("log/unicorn.stderr.log", ENV['RAILS_ROOT']) GC.respond_to?(:copy_on_write_friendly=) and GC.copy_on_write_friendly = true before_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! old_pid = "#{server.config[:pid]}.oldbin" if old_pid != server.pid begin sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU Process.kill(sig, File.read(old_pid).to_i) rescue Errno::ENOENT, Errno::ESRCH end end sleep 1 end after_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end ---- 起動! $ bundle exec unicorn -E production -c config/unicorn.rb -D エラー! (snip) /home/<ユーザー名>/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/execjs-2.2.2/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable) (snip) GemFileのこの行のコメント外す gem 'therubyracer', platforms: :ruby $ bundle install $ bundle exec unicorn -E production -c config/unicorn.rb -D 無事起動。
nginxをいれる
通常通りインストール $ sudo yum install nginx configを設定 -- /etc/nginx/conf.d/sample.conf -- upstream unicorn { server unix:/home/<ユーザー名>/sample/tmp/unicorn.sock; } server { listen 80; server_name <サーバー名>; root /home/<ユーザー名>/sample/public; access_log /var/log/<ユーザー名>/sample_access.log; error_log /var/log/<ユーザー名>/sample_error.log; client_max_body_size 100m; error_page 500 502 503 504 /500.html; try_files $uri/index.html $uri @unicorn; location @unicorn { 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; } } ---- 起動して終わり * sudo /etc/init.d/nginx start
アクセスしてみる
こんなエラーが (snip) E, [2015-01-12T01:57:24.801138 #25915] ERROR -- : app error: Missing `secret_token` and `secret_key_base` for 'production' environment, set these values in `config/secrets.yml` (RuntimeError) (snip) config/secrets.ymlを見ると -- production: secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> -- こんな風に書いてるから、環境変数にセットしないと駄目らしい。セキュリティ的なあれっぽい。 どうしても書きたかったら、レポジトリに登録はしないように注意(.gitignoreに追加するとか)。 rake secretを実行する為に、以下をGemfileに追加 gem 'rb-readline' $ bundle install $ bundle exec rake secret 出てきた文字列をbash_profileあたりに突っ込んでおきましょう。 export SECRET_KEY_BASE="XXXXXXXXXXXXXXXXXXX"