Ruby On Rails4 の開発を始められるまで その1
posted at 2014/01/14 by chikkun@コワーキングスペース町田
Ruby On Rails4の基本を、自分のメモも兼ねて、3回シリーズでご報告します。
今日はその第1回目(2回目はdeviseを使った認証機能の追加。3回目はRspecやCucumberを利用したテストの予定です)。
Railsを使って開発する直前までをまとめます。ただ一応、環境と目標とするのは以下のようです。OSの違いや、仮に同じOSでも微妙にバージョンで違ったりする場合もあるのでご注意下さい。
- OSはCentOS6.5とする(MarvericksにParallelsを利用して、SDカードにインストールしたもの—ほとんど何もインストールされていない)。
- アプリ名は「comachi」。
- Apache2+Passengerを利用し、標準で付いているWEBrickは利用しない。
- Railsは4とする。
- DBはPostgreSQLを利用する。
- 外見はTwitter Bootstrapを使用する。
rbenvのインストール
CentOS6ですとRubyが2系ではないので、まずはRuby2をインストールします。というのもRails4はRuby2を推奨しているからです。
ただ今後1.9等も必要になることが予想されるので、便利なrbenvを使ってRubyのバージョンを変えられるようにします。ただrbenvをインストールする前に、RubyやPassenger等で必要になりそうなものを事前にyumでインストールします。
$ yum -y install yum-plugin-priorities zlib zlib-devel readline readline-devel openssl openssl-devel libxml2 libxml2-devel libxslt libxslt-devel httpd httpd-devel curl-devel bison gcc gcc-c++ git emacs
- ※↑は1行です。
- ※最後のemacsは、好みでvimなどでもw
- ※上記のいくつかは、gitやapache、Passengerのインストールに必要なもの等も入っています。Passengerの場合は必要なものをインストール時に教えてくれるので、仮に足らなくても大丈夫です。
- ※CentOSのバージョンによっては「–enablerepo=remi,epel」オプションを入れる必要があるかもしれません(6.5では必要はありませんでした)。
これで準備が出来たので、早速rbenvのインストールです。
# 必要に応じてsuなりsudoをして下さい
$ cd /usr/local
$ git clone git://github.com/sstephenson/rbenv.git rbenv
$ groupadd rbenv
$ chgrp -R rbenv rbenv
$ chmod -R g+rwxXs rbenv
$ cd rbenv
$ mkdir plugins
$ cd plugins
$ git clone git://github.com/sstephenson/ruby-build.git ruby-build
$ chgrp -R rbenv ruby-build
$ chmod -R g+rwxs ruby-build
環境変数の設定
rbenvのインストールは終わりましたが、環境変数を設定しないと使えません。
そこで環境変数を/etc/profileに設定します(「/etc/profile」を直接編集したくない場合は「/etc/profile.d/rbenv.sh」等に書くのも手です)。
$ emacs /etc/profile #viでも
で、一番下に以下のように環境変数を設定します。
export RBENV_ROOT="/usr/local/rbenv"
export PATH="$RBENV_ROOT/bin:$PATH"
eval "$(rbenv init -)"
これを書き込んで、それら環境変数を読み込みます。
$ source /etc/profile
Autoconfのバージョンアップ
最新のRuby2.2-devのインストールでは、autoconfのバージョンが低いと怒られるので、m4とAutoconfの上書きバージョンアップを行います。
$ wget ftp://ftp.gnu.org/gnu/m4/m4-1.4.17.tar.gz
$ tar xvzf m4-1.4.17.tar.gz
$ cd m4-1.4.17
$ ./configure --prefix=/usr
$ make
$ make install
$ cd ..
$ wget ftp://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
$ tar xvzf autoconf-2.69.tar.gz
$ cd autoconf-2.69
$ ./configure --prefix=/usr
$ make
$ make install
インストール可能なRubyの確認
インストール可能なRubyのパッケージを確認する。
$ rbenv install -l
...
1.9.3-rc1
2.0.0-dev
2.0.0-p0
2.0.0-p195
2.0.0-p247
2.0.0-p353
2.0.0-preview1
2.0.0-preview2
2.0.0-rc1
2.0.0-rc2
2.1.0
2.1.0-dev
2.1.0-preview1
2.1.0-preview2
2.1.0-rc1
2.2.0-dev
...
バージョンはお好みで。とりあえずここでは「2.2.0-dev」をインストールします。
$ rbenv install 2.2.0-dev #時間はそこそこかかります
$ rbenv rehash # これをやる必要があります
$ rbenv global 2.2.0-dev
インストールできたか確認
$ ruby -v
ruby 2.2.0dev (2014-01-12 trunk 44563) [x86_64-linux]
OKのようですw
PostgreSQLのインストール
yum.postgresql.orgに行って「pgdg-centos93-9.3-1.noarch.rpm」を持ってきて、rpmを実行します。
wget http://yum.postgresql.org/9.3/redhat/rhel-6-x86_64/pgdg-centos93-9.3-1.noarch.rpm
rpm -ivh pgdg-centos93-9.3-1.noarch.rpm
すなわち、wgetでダウンロードし、rpmでrepositoryのファイルをインストールします。そして次にyumでPostgreSQLをインストールします。
yum install -y postgresql93-server postgresql93-devel postgresql93-contrib
次にお決まりの「initdb」をし、サーバーを立ち上げます。またOSを再起動しても自動で立ち上げるためにchkconfigし、ついでにapacheも自動起動するようにします。
さらに、postgresユーザのパスワードを作成し、新しいユーザ(chikkun)、データベース(comachi)を作成します。
$ /etc/init.d/postgresql-9.3 initdb
$ /etc/init.d/postgresql-9.3 start
$ chkconfig --level 345 postgresql-9.3 on
$ chkconfig --level 345 httpd on
$ su postgres
$ psql
% alter role postgres with password 'hogehoge'; #postgresユーザにパスワードを設定
% \q
$ createuser -P -s chikkun
Enter password for new role:
Enter it again:
createdb comachi
$ exit
それから、/var/lib/pgsql/9.3/data/pg_hba.confを少々書き換えます。
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 ident
# IPv6 local connections:
host all all ::1/128 ident
のpeer、identをmd5にします。
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
そして再起動。
/etc/init.d/postgresql-9.3 restart
たぶん、PostgreSQLのRuby用のモジュールでインストールで失敗するので、先にオプションを付けて、インストールしていまいます。
gem install pg -- --with-pg_config=/usr/pgsql-9.3/bin/pg_config
- ※上の「–」が余計に見えますが、これがないと「そんなオプションはないよ」と言われます(というか、言われ続けました・涙)。
Railsのインストール
さてさて、何はなくともRailsをインストールします。
$ gem install rails
$ rbenv rehash
$ rails -v
Rails 4.0.2
どうやら大丈夫そう。
passengerのインストール
次に、gemを使ってpassengerをインストールします。
gem install passenger
passenger-install-apache2-module
先回りして、必要なものをインストールしているので全く同じ環境ならば大丈夫ですが、環境が違っていたりすると、足らないものが出たりします。
しかし、そのような場合は、以下のようなメッセージで、必要なもののリストをyumコマンド付きで教えてくれるので、インストールしたら再度「passenger-install-apache2-module」を実行します。
Installation instructions for required software
* To install C++ compiler:
Please install it with yum install gcc-c++
無事コンパイルが始まって、終了すると以下のようなメッセージが出ます。このうち、最初の頃のLoadModuleの部分と、最後の辺りにあるVirtualHostの部分が必要になります。
The Apache 2 module was successfully installed.
Please edit your Apache configuration file, and add these lines:
LoadModule passenger_module /usr/local/rbenv/versions/2.2.0-dev/lib/ruby/gems/2.2.0/gems/passenger-4.0.33/buildout/apache2/mod_passenger.so
PassengerRoot /usr/local/rbenv/versions/2.2.0-dev/lib/ruby/gems/2.2.0/gems/passenger-4.0.33
PassengerDefaultRuby /usr/local/rbenv/versions/2.2.0-dev/bin/ruby
After you restart Apache, you are ready to deploy any number of web
applications on Apache, with a minimum amount of configuration!
Press ENTER to continue.
--------------------------------------------
Deploying a web application: an example
Suppose you have a web application in /somewhere. Add a virtual host to your
Apache configuration file and set its DocumentRoot to /somewhere/public:
<VirtualHost *:80>
ServerName www.yourhost.com
# !!! Be sure to point DocumentRoot to 'public'!
DocumentRoot /somewhere/public
<Directory /somewhere/public>
# This relaxes Apache security settings.
AllowOverride all
# MultiViews must be turned off.
Options -MultiViews
</Directory>
</VirtualHost>
And that's it! You may also want to check the Users Guide for security and
optimization tips, troubleshooting and other useful information:
/usr/local/rbenv/versions/2.2.0-dev/lib/ruby/gems/2.2.0/gems/passenger-4.0.33/doc/Users guide Apache.html
http://www.modrails.com/documentation/Users%20guide%20Apache.html
Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl)
https://www.phusionpassenger.com
Phusion Passenger is a trademark of Hongli Lai & Ninh Bui.
すなわち
LoadModule passenger_module /usr/local/rbenv/versions/2.2.0-dev/lib/ruby/gems/2.2.0/gems/passenger-4.0.33/buildout/apache2/mod_passenger.so
PassengerRoot /usr/local/rbenv/versions/2.2.0-dev/lib/ruby/gems/2.2.0/gems/passenger-4.0.33
PassengerDefaultRuby /usr/local/rbenv/versions/2.2.0-dev/bin/ruby
と
<VirtualHost *:80>
ServerName www.yourhost.com
# !!! Be sure to point DocumentRoot to 'public'!
DocumentRoot /somewhere/public
<Directory /somewhere/public>
# This relaxes Apache security settings.
AllowOverride all
# MultiViews must be turned off.
Options -MultiViews
</Directory>
</VirtualHost>
が必要になります。もっと具体的には、上のものはそのままhttpd.confにコピペ、下のものは自分の環境に合わせて書き換えた後にhttpd.comfに追記します。
実際には私の場合以下のようにしました。
<VirtualHost *:80>
ServerAdmin hoge@fuga.com
DocumentRoot /var/www/html/rails/comachi/public
ServerName www.comachi.com
ErrorLog logs/comachi.error_log
CustomLog logs/comachi.access_log common
RackEnv development
<Directory /var/www/html/rails/comachi/public>
Options -MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
という感じです。
- ※実際にの「DocumentRoot」は違っていて、CentOS自体がSDカードに入っていて、そのSDカード自体にRailsアプリを置いてあるので、実際には/media/psf/UBUNTU/rails/publicで「UBUNTU」はSDカードのボリューム名です。
そこで、/etc/httpd/conf/httpd.confのLoadModuleを記載している最後に(今回の例では217行あたり)上記の3行を追加します。
そして、VirutualHostの設定はhttpd.confの最後に(ディストリビューションによっては別ファイルのことも)上記のを参考に追記します。
ここではrailsのアプリの場所が「/var/www/html/rails/comachi」としていますが、適宜置き換えて下さい。また、Rails的にはdevelopmentにしてあります。
もう1つ。SELinuxが悪さしてApacheが立ち上がらないので(というか必要なことなんでしょうが、開発ではいらないので)「/etc/selinux/config」を
SELINUX=disabled
とし、これは再起動後もdisabledにするものなので、コマンドラインでも
setenforce 0
として、現在も無効にします。そして
$ /etc/init.d/httpd restart
します。
最後にVirtualHostを使っているので、「/etc/hosts」の最初の行のローカルホストの部分に「www.comachi.com」を追記します。すなわち
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 www.comachi.com
さてさてあとは、railsのアプリを作成するだけです。
comachiアプリを作成
とりあえず今回のアプリ名を「comachi」と名付け、まずは何はなくともrailsコマンドのnewを実行します(comachiを作成する親ディレクトリにcdしてから)。
cd /var/www/html/rails
$ rails new comachi -d postgresql -T
※-Tはunit testを作成しないオプションで、Rspec等を使う場合には必要ないので付けました。
DBはsqliteがデフォルトなので、「-d postgresql」を付けてPostgreSQLを利用するようにします。ここで「Javascriptライブラリがない云々」とか怒られます。「therbyracer」を入れないとダメで、後で必要になるかもしれない「execjs」も含めて、GemFileに以下の2行を書き込みます。
gem 'therubyracer', platforms: :ruby
gem 'execjs'
そして
bundle install
を実行します。
外見はTwitter Bootstrap
Twitter Bootstrapを使ってデザインするので、Gemfileに以下を書き込みます(と思ったですが、後にこれは利用するBootstrapが2で、それ以外にgemなどで3が簡単に使えるようにならないか探したのですが、結局良さげなのがないのと「stackoverflow」であっさり、「ダウンロードしてvendor/assetに置くだけなんだから、自分でやれ」みたいなのを見て、全くその通りと思い、この方法をやめます)。
gem "twitter-bootstrap-rails"
gem "less-rails"
なので、↑はless-railsだけ残して、まずはBootstrapのダウンロードに行って、最新版をダウンロードして、解凍すると、
dist/
css/
bootstrap-theme.css
bootstrap-theme.min.css
bootstrap.css
bootstrap.min.css ※
fonts/
glyphicons-halflings-regular.eot
glyphicons-halflings-regular.svg
glyphicons-halflings-regular.ttf
glyphicons-halflings-regular.woff
js/
bootstrap.js
bootstrap.min.js ※
というようなファイルが出てくるので、↑の※のファイルを(必要に応じて、追加します)
vendor/
assets:
javascripts/
bootstrap.min.js
stylesheets/
bootstrap.min.css
というように、コピーします(vendorでなくても、「app/assets」でも大丈夫です)。
そして「app/assets/stylesheets/application.css」に
*= require bootstrap.min
を書き加えます。
また「app/assets/javascripts/application.js」に
//= require bootstrap.min
を書き込みます。
これで終了wtwitter-bootstrap-railsは使わないので、
gem "less-rails"
そして、以下を実行。
bundle install
最後にconfig/database.ymlの編集
データベースのinitdb で、作成したユーザやデータベース名に併せて、今回はdevelopment環境なので以下のように書き換えます。
development:
adapter: postgresql
encoding: utf8
database: comachi
pool: 5
username: chikkun
password: hogehoge
これで、ブラウザで http://www.comachi.com で次のような画面が見られたらOKです。
これで開発が始められそうですw