.logbook

学んだことを書き綴る、言わば航海日誌です。

Ruby on Rails上で動作するCMS LocomotiveCMSをインストールしてみた

Ruby on Railsの入門教材のサンプルとしてしばしばブログシステムが取り上げられているにも関わらず、WordPressばりの知名度を誇るCMSRuby界隈には登場していない。が、唯一挙げるとするならばLocomotiveCMSというものがそれに相当するらしい。

locomotivecms.com

Rubyファンとしてはそこそこ使えるCMSであることがわかればすぐにでも乗り換えたいくらいである。よって、試しにインストールしてみることとした。

インストール環境

以下の環境にインストールする。Ruby, Railsはすでにインストールされている。

Getting Startedから手順書を入手

LocomotiveCMSを初めて使う場合、この手順書を見ながら作業すると楽であるようだ。メールアドレスと名前を入力する必要があるが、入手したほうが良いだろう。

Learn the basics of LocomotiveCMS

Wagonのインストール

まずはこのWagonというものをインストールする必要があるらしい。インストール成功するとwagonコマンドが使えるようになった。

$ gem install locomotivecms_wagon
Successfully installed locomotivecms_wagon-1.5.5
Parsing documentation for locomotivecms_wagon-1.5.5
Done installing documentation for locomotivecms_wagon after 2 seconds
1 gem installed
$ wagon version
1.5.5

インストールできたので早速サイトを構築してみる。bundle installまで自動実行してくれるようだ。

$ wagon init acme_corp
      create  acme_corp
      create  acme_corp/Gemfile
      create  acme_corp/app/content_types
      create  acme_corp/app/views/pages/404.liquid
      create  acme_corp/app/views/pages/404.liquid.haml
      create  acme_corp/app/views/pages/index.liquid
      create  acme_corp/app/views/pages/index.liquid.haml
      create  acme_corp/app/views/snippets
      create  acme_corp/config.ru
      create  acme_corp/config/deploy.yml
      create  acme_corp/config/site.yml
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
      create  acme_corp/config/translations.yml
      create  acme_corp/data
      create  acme_corp/public/fonts
      create  acme_corp/public/images
      create  acme_corp/public/javascripts
      create  acme_corp/public/samples
      create  acme_corp/public/stylesheets
Do you prefer HAML templates? no
      remove  acme_corp/app/views/pages/index.liquid.haml
      remove  acme_corp/app/views/pages/404.liquid.haml
         run  bundle install
Fetching gem metadata from https://rubygems.org/..............
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies...
Using RedCloth 4.2.9
Using i18n 0.6.11
(省略...)
Bundle complete! 5 Gemfile dependencies, 64 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

Congratulations, your site "acme_corp" has been created successfully !
Next steps:
    cd ./acme_corp
    bundle install
    bundle exec wagon serve
    open http://0.0.0.0:3333

生成されたディレクトリへ移動してサーバを起動する。念のためbundle installも再度実行しておく。

$ cd ./acme_corp
$ bundle install
Bundle complete! 5 Gemfile dependencies, 64 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
$ bundle exec wagon serve
Thin web server (v1.6.3 codename Protein Powder)
Maximum connections set to 1024
Listening on 0.0.0.0:3333, CTRL+C to stop

サーバ起動したらアクセスしてみる。URLは「http://0.0.0.0:3333」だ。とりあえず何か表示された。

f:id:ylgbk:20150704065451p:plain

Engineのインストール

このページを参考にした。

LocomotiveCMS - Documentation

まずmongoDBをインストールする。

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
$ echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
$ sudo apt-get update
$ sudo apt-get install mongodb-10gen
(数分かかる)

次にRails3をインストールする。ここまできて今更だがRails4はまだサポートしていないらしい。

$ gem install rails --version=3.2.19

サンプルのアプリを作ってGemfileを編集する。(★)

$ rails new acme_cms --skip-active-record --skip-test-unit --skip-javascript --skip-bundle
$ cd acme_cms
$ vi Gemfile

Gemfileは以下のとおり

source 'https://rubygems.org'

gem 'rails', '3.2.19'

gem 'locomotive_cms', '~> 2.5.6', :require => 'locomotive/engine'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'compass',        '~> 0.12.7'
  gem 'compass-rails',  '~> 2.0.0'
  gem 'sass-rails',     '~> 3.2.6'
  gem 'coffee-rails',   '~> 3.2.2'
  gem 'uglifier',       '~> 2.5.1'
  gem 'therubyracer', :platforms => :ruby
end

# Use unicorn as the app server
gem 'unicorn'

Gemをインストールする

$ bundle install

LocomotiveCMSのインストールジェネレータを起動する。

$ bundle exec rails g locomotive:install

このとき、

Usage:
rails new APP_PATH [options]
(以下省略)

が表示されて、うまくいかないことがある。

下記URLを参考に、上記(★)の手順をやり直すとうまくいく。

github.com

$ rails _3.2.19_ new acme_cms --skip-active-record --skip-test-unit --skip-javascript --skip-bundle

次にデータベースを作成する。

$ mkdir mongodb
$ mongod --dbpath mongodb

最後に、config/mongoid.ymlを以下のように修正する。

development:
  sessions:
    default:
      database: acme_cms_development
      hosts:
        - localhost:27017
  options:
    identity_map_enabled: true

test:
  sessions:
    default:
      database: acme_cms_test
      hosts:
        - localhost:27017
  options:
    identity_map_enabled: true

production:
  sessions:
    default:
      database: acme_cms_production
      # heroku
      # uri: <%= ENV['MONGOHQ_URL'] %>
      hosts:
        - localhost:27017
  options:
    identity_map_enabled: true

起動

以下のコマンドで起動する。

$ bundle exec unicorn_rails

ここで、「undefined method `secret_key=' for Devise:Module (NoMethodError)」というエラーが発生する場合、vi config/initializers/devise.rbを以下のように編集する。

# Use this hook to configure devise mailer, warden hooks and so forth. The first
# four configuration values can also be set straight in your models.
Devise.setup do |config|
# ★以下、2行をコメント化
#  config.secret_key = "d9eb5171c59a4c817f68b0de27b8c1e340c2341b52cdbc60d3083d4e8958532" \
#                      "18dcc5f589cafde048faec956b61f864b9b5513ff9ce29bf9e5d58b0f234f8e3b"

  # ==> Mailer Configuration
  # Configure the e-mail address which will be shown in Devise::Mailer,
  # note that it will be overwritten if you use your own mailer class with default "from" parameter.
  config.mailer_sender = "please-change-me@config-initializers-devise.com"

ブラウザからhttp://localhost:8080へアクセスすると、アカウント生成画面が表示される。

f:id:ylgbk:20150706064029p:plain