周りの環境により中々Rails環境まで作ったことが無かったのですが、今日開発の方の希望により作ることになり、若干ハマったのでメモします。

 

環境

CentOS release 6.7 (Final)
Apache2.2
Ruby 2.2.4(rbenvで管理)
Rails 4.2.5

Rubyのインストール

パッケージのインストール

# yum install gcc gcc-c++ make autoconf openssl-devel readline-devel mysql-devel wget git
# yum install zlib zlib-devel openssl-devel sqlite-devel gcc-c++ glibc-headers libyaml-devel readline readline-devel zlib-devel libffi-devel

rbenvのインストール

# yum install openssl-devel readline-devel zlib-devel libcurl-devel
# cd /usr/local
# git clone git://github.com/sstephenson/rbenv.git rbenv
Initialized empty Git repository in /usr/local/rbenv/.git/
remote: Counting objects: 2325, done.
remote: Total 2325 (delta 0), reused 0 (delta 0), pack-reused 2325
Receiving objects: 100% (2325/2325), 411.02 KiB | 260 KiB/s, done.
Resolving deltas: 100% (1453/1453), done.
# mkdir rbenv/shims rbenv/versions rbenv/plugins
# groupadd rbenv
# chgrp -R rbenv rbenv
# chmod -R g+rwxXs rbenv

rbenv-buildのインストール

# cd /usr/local/rbenv/plugins
# git clone git://github.com/sstephenson/ruby-build.git ruby-build
Initialized empty Git repository in /usr/local/rbenv/plugins/ruby-build/.git/
remote: Counting objects: 5249, done.
remote: Total 5249 (delta 0), reused 0 (delta 0), pack-reused 5249
Receiving objects: 100% (5249/5249), 985.32 KiB | 930 KiB/s, done.
Resolving deltas: 100% (2893/2893), done.
# chgrp -R rbenv ruby-build
# chmod -R g+rwxs ruby-build

rbenv-default-gemsのインストール

# cd /usr/local/rbenv/plugins
# git clone git://github.com/sstephenson/rbenv-default-gems.git rbenv-default-gems
Initialized empty Git repository in /usr/local/rbenv/rbenv-default-gems/.git/
remote: Counting objects: 18, done.
remote: Total 18 (delta 0), reused 0 (delta 0), pack-reused 18
Receiving objects: 100% (18/18), done.
Resolving deltas: 100% (4/4), done.
# chgrp -R rbenv rbenv-default-gems
# chmod -R g+rwxs rbenv-default-gems

環境変数の設定

# vi /etc/profile.d/rbenv.sh
export RBENV_ROOT=”/usr/local/rbenv”
export PATH=”$RBENV_ROOT/bin:$PATH”
eval “$(rbenv init -)”
# source /etc/profile.d/rbenv.sh
# rbenv -v
rbenv 0.4.0-215-g90373d7

デフォルトでインストールするgemの指定

# vi /usr/local/rbenv/default-gems
bundler
rbenv-rehash

インストールするrubyのバージョンを確認

# rbenv install -l

rubyのインストール

# rbev install 2.2.4
Downloading ruby-2.2.4.tar.gz…
-> https://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.4.tar.gz
Installing ruby-2.2.4…

Installed ruby-2.2.4 to /usr/local/rbenv/versions/2.2.4

Fetching: bundler-1.11.2.gem (100%)
Successfully installed bundler-1.11.2
Parsing documentation for bundler-1.11.2
Installing ri documentation for bundler-1.11.2
Done installing documentation for bundler after 7 seconds
1 gem installed
Fetching: rbenv-rehash-0.3.gem (100%)
Successfully installed rbenv-rehash-0.3
Parsing documentation for rbenv-rehash-0.3
Installing ri documentation for rbenv-rehash-0.3
Done installing documentation for rbenv-rehash after 0 seconds

# rbenv global 2.2.4
# ruby -v
ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-linux]

# bundle -v
Bundler version 1.11.2

Railsのインストール

# gem search -r rails
# gem install rails
# rails -v
Rails 4.2.5

参考URL:http://www.ken10.com/ruby/ruby_install.html

Passengerのインストール

# gem install passenger –no-ri –no-rdoc -V
# passenger -v
Phusion Passenger 5.0.22
# rbenv rehash
# passenger-install-apache2-module
Welcome to the Phusion Passenger Apache 2 module installer, v5.0.22.

This installer will guide you through the entire installation process. It
shouldn’t take more than 3 minutes in total.

Here’s what you can expect from the installation process:

1. The Apache 2 module will be installed for you.
2. You’ll learn how to configure Apache.
3. You’ll learn how to deploy a Ruby on Rails application.

Don’t worry if anything goes wrong. This installer will advise you on how to
solve any problems.

Press Enter to continue, or Ctrl-C to abort.

——————————————–

Which languages are you interested in?

Use <space> to select.
If the menu doesn’t display correctly, press ‘!’

‣ ⬢ Ruby
⬢ Python
⬡ Node.js
⬡ Meteor

——————————————–
~
インストーラーが走ります
~
Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) 🙂
https://www.phusionpassenger.com

Phusion Passenger is a registered trademark of Hongli Lai & Ninh Bui.

# passenger-install-apache2-module –snippet >> /etc/httpd/conf.d/passenger.conf
LoadModule passenger_module /usr/local/rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/passenger-5.0.22/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
PassengerRoot /usr/local/rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/passenger-5.0.22
PassengerDefaultRuby /usr/local/rbenv/versions/2.2.4/bin/ruby
</IfModule>

# cd /etc/httpd/conf.d
# vim ◯◯.conf

 

はまったのはここ↓

色々なサイトに書いてあるやり方は後者のRails環境だけの場合が多いかと思います。

今回はとある別のPHPの仕組みで既に稼働しており、別ドメインではなく同じドメインを使ってSSL証明書のリソースを減らしたかったため、Aliasを使って同じドメイン配下にしたいという要望がありました。

そこで色々調べて、qiitaの「61503891さんが書かれていた内容を参考にしたところ、Aliasでもいけることが分かりましたので残しておきます。61503891さんありがとうございます!

Rails環境のみの場合か、Rails以外もある場合か(同じドメイン配下でAliasを使いたいか)でApacheのconfの書き方が異なるので注意が必要です。

[パターン1:Rails以外も使って同じドメイン配下でApacheのAliasを使いたい場合]

Alias /test-rails /var/www/vhosts/test-rails/app/public

<Location /test-rails>
PassengerEnabled on
RailsEnv development
PassengerBaseURI /test-rails
PassengerAppRoot /var/www/vhosts/test-rails/app/
</Location>

<Directory ” /var/www/vhosts/test-rails/app/public”>
AllowOverride All
Options -MultiViews
</Directory>

参考URL:http://qiita.com/61503891/items/732d9c123c39e35e97eb

[パターン2:Railsだけの場合]

DocumentRoot /var/www/vhosts/test-rails/app/public
RailsEnv development

<Directory ” /var/www/vhosts/test-rails/app/public”>
AllowOverride All
Options -MultiViews
</Directory>

# gem install sqlite3
# mkdir -p /var/www/vhosts/test-rails
# cd /var/www/vhosts/test-rails
# rails new app
Bundle complete! 12 Gemfile dependencies, 54 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
run bundle exec spring binstub –all
* bin/rake: spring inserted
* bin/rails: spring inserted

# cd app
# vi Gemfile

– #gem ‘therubyracer’, platforms: :ruby
+ gem ‘therubyracer’, platforms: :ruby

# chown -R 一般ユーザー:一般ユーザー /var/www/vhosts/test-rails
# su – 一般ユーザー
# bundle install
# service httpd configtest
# service httpd graceful

最後にhogehoge.comかhogehoge.com/test-railsでアクセスして、Railsの画面が表示されたらOKです。
参考URL:http://qiita.com/merrill/items/87bf1ecfac8aa49f06a7

 

以上。

カテゴリー: ProgramRubyTech