masalibの日記

システム開発、運用と猫の写真ブログです

さくらVPSでRuby on Railsの実行環境を作成した手順

この記事は古いので新しい記事を参照してください
前提
 作業用として/homeを使用する
 すべてrootでおこなう
 (sudoをつかうことをすすめます)
 
環境
 CentOS 6.2 64bit(さくらVPSの標準)
 ruby 1.9.3
 rails3.2.7
 apache
 passenger
 mysql5.5
 php5.3
 phpMyAdmin 3.5
 rvn rbenvは使わない
 なぜか自分がインストールすると
 gemのパスが見つかりませんとかよくわからないエラーがでる
 
 railsのパス
 /var/www/ruby/rails_projects/catmovie/public
phpmyadminは別ポートで実行する
 少しでもセキュリティをあげるため
 
1・作業用フォルダ作成

cd /home
mkdir src

 
2・システムのアップデートといらないサービス停止

yum -y update
chkconfig acpid off
chkconfig apmd off
chkconfig atd off
chkconfig avahi-daemon off
chkconfig bluetooth off
chkconfig cups off
chkconfig hidd off
chkconfig ip6tables off
chkconfig netfs off
chkconfig nfslock off
chkconfig pcscd off
chkconfig portmap off
chkconfig restorecond off
chkconfig rpcgssd off
chkconfig rpcidmapd off
chkconfig xfs off
chkconfig yum-updatesd off
chkconfig --list
/sbin/service iptables stop

 
3・RubyMySQLなどで必要なパッケージを
  yumでインストールする
 
Ruby系列

yum -y install readline readline-devel zlib zlib-devel openssl-devel
yum -y install gcc gcc-c++ autoconf
yum -y install openssl-devel zlib-devel readline-devel
yum -y install httpd-devel sqlite-devel curl-devel gettext-devel
yum -y install zlib-devel.x86_64
yum -y install openssl-devel.x86_64

MySQL系列

yum -y install php php-mbstring php-mcrypt php-mysql php-gd php-mssql mysql mysql-server mysql-devel


4・yamlのインストール(Railsで必要)

cd /home/src/
wget http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz
tar xzf yaml-0.1.4.tar.gz
cd yaml-0.1.4
./configure --prefix=/usr/local
make 
make install

 
5・sqliteのバージョンが古いらしいのでインストール

cd /home/src/
wget http://www.sqlite.org/sqlite-amalgamation-3.7.2.tar.gz
tar xzf sqlite-amalgamation-3.7.2.tar.gz
cd sqlite-amalgamation-3.7.2.tar.gz 
cd sqlite-3.7.2/
./configure
make 
make install
sqlite3 -version

 
6・Rubyのインストール

cd /home/src/
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p125.tar.gz
tar xzf ruby-1.9.3-p125.tar.gz
cd ruby-1.9.3-p125
./configure --enable-shared --with-opt-dir=/usr/local/lib
make 
make install
#バージョンの確認
ruby -v

 
7・zlibのインストール(圧縮するのに必要らしいので)

cd /home/src/ruby-1.9.3-p125/ext/zlib
ruby extconf.rb 
make
make install

8・MySQLphpphpMyAdminのインストール
 単純にyumでインストールする
 5.1系になりphpMyAdminのバージョンチェックに引っかかる
 
8-1・専用のパッケージを使ってインストールする

rpm -ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm 
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm

#enabled=1 → enabled=0に修正する
vi /etc/yum.repos.d/epel.repo #常時つかわない設定

#enabled=1 → enabled=0に修正する
vi /etc/yum.repos.d/remi.repo #常時つかわない設定

#現在はいっているのをアップデート
yum update --enablerepo=remi,epel php mysql mysql-server -y 
php -v #バージョン確認で5.2以上がかえってくる

 
8-2・/etc/my.cnfの編集

vi /etc/my.cnf
#[mysqld]のしたにutf8の設定を追加する
#5.5
character-set-server=utf8 

#もし5.5未満なら
default-character-set=utf8

 
8-3・MySQL起動

/etc/rc.d/init.d/mysqld start	

 
8-4・MySQL自動起動設定

chkconfig mysqld on	
chkconfig --list mysqld	
#※2~5のonを確認

 
8-5・MySQLにログインし、ユーザを確認してパスワード変更す

mysql -u root	

select user,host,password from mysql.user;	
#初期設定はブランクなのでパスワードを変更	
set password for root@localhost=password('xxxxxpasswordxxxxx');	
set password for root@'OSインストール時に指定したホスト名'=password('xxxxxpasswordxxxxx');	
set password for root@127.0.0.1=password('xxxxxpasswordxxxxx');	
exit

8-6・phpMyAdminのインストール

cd /home/src/
wget http://sourceforge.net/projects/phpmyadmin/files/phpMyAdmin/3.5.2/phpMyAdmin-3.5.2-all-languages.zip
unzip phpMyAdmin-3.5.2-all-languages.zip 
mv phpMyAdmin-3.5.2-all-languages /var/www/phpMyAdmin
cd /var/www/phpMyAdmin
mv config.sample.inc.php config.inc.php
vi config.inc.php
#以下を修正
$cfg['blowfish_secret'] = 'zzxddda8b7c6d'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'tcp'; ← cookieからtcpに変更

9・rubygemsのインストール

cd /home/src/
wget http://files.rubyforge.vm.bytemark.co.uk/rubygems/rubygems-1.8.19.tgz
tar xzf rubygems-1.8.19.tgz 
cd rubygems-1.8.19
ruby setup.rb
gem update --system

#railsよりrdocを先にインストールする
#先にrailsをインストールすると
#file 'lib' not foundとかでる

gem install rdoc
gem install therubyracer 
gem install execjs
gem install rails

#あともしSELinuxを使用している人は
#オフにする。やらないとexecjsでこける

 
10・passengerのインストール

gem install passenger
passenger-install-apache2-module

#なんか画面がでるの実行を押す
#最後に下記の画面がでるので
#これをメモする

--------------------------------------------
The Apache 2 module was successfully installed.

Please edit your Apache configuration file, and add these lines:

   LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.14/ext/apache2/mod_passenger.so
   PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.14
   PassengerRuby /usr/local/bin/ruby

After you restart Apache, you are ready to deploy any number of Ruby on Rails
applications on Apache, without any further Ruby on Rails-specific
configuration!

Press ENTER to continue.

 
puttyでいうと
画面を右クリックして
すべてをクリップボードにコピーする
 

11・Apacheの設定を修正する

vi /etc/httpd/conf/httpd.conf
#一番最後に下記をたす
   LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.14/ext/apache2/mod_passenger.so
   PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.14
   PassengerRuby /usr/local/bin/ruby

ついでにRailsphpMyAdminの設定をたす

Listen 8999
NameVirtualHost *:80
NameVirtualHost *:8999
 
<virtualhost *:8999="">
   ServerName catmovie.info
    DocumentRoot /var/www/phpMyAdmin
</virtualhost>

<virtualhost *:80="">
    ServerName catmovie.info
	DocumentRoot /var/www/ruby/rails_projects/catmovie/public
    RailsBaseURI /rails
  RailsEnv production
</virtualhost>

 
12・Railsの設定
12-1・railsの基本フォルダ作成

cd /var/www/
mkdir ruby
cd /var/www/ruby
mkdir rails_projects
cd /var/www/ruby/rails_projects
rails new catmovie #ここでrailsができるのか確認できます

 
12-2・gemfile編集(execjsがデフォルトではいっていない)

vi /var/www/ruby/rails_projects/catmovie/Gemfile

以下を追加

gem 'mysql2'

#日本語か
gem 'rails-i18n'

#ページ制御
gem 'will_paginate', '~> 3.0'

#テストモジュール
group :test do
  gem 'factory_girl_rails', '~> 1.4.0'
end

#エンジン
gem 'execjs'
gem 'therubyracer' 

をたす

12-3・gem設定内容を反映する

cd /var/www/ruby/rails_projects/catmovie/
bundle install

12-4・ローカルからソースをアップする
app、config、db、libのフォルダアップする
ソース(テスト的な)がない場合は下記を実行

rails g scaffold item name:string body:text price:integer

そしてDBの設定を修正するあとルールも修正が必要かも

vi /var/www/ruby/rails_projects/catmovie/db/database.yml
production:
  adapter: mysql2
  database: catmovie_production
  host: localhost
  username: root
  password: xxxxxpasswordxxxxx
  encoding: utf8

12-5・データベースの作成と反映

cd /var/www/ruby/rails_projects/catmovie/
#開発の場合
rake db:create
rake db:migrate

#本番の場合
rake db:create RAILS_ENV=production
rake db:migrate RAILS_ENV=production


#問題なければapacheの再起動とリブート対策
/etc/rc.d/init.d/httpd restart
chkconfig httpd on
chkconfig --list httpd

12-6・publicからindexを削除する

cd /var/www/ruby/rails_projects/catmovie/public/
mv index.html index.bk

12-7・assets系の圧縮する

 touch tmp/restart.txt
 rake assets:precompile --trace
 touch tmp/restart.txt

public/assetsのフォルダにいろいろ作成される
 
12-8・ログのパーミッションを変更する
     初期設定がなぜか644

 chmod 666 /var/www/ruby/rails_projects/catmovie/development.log
 chmod 666 /var/www/ruby/rails_projects/catmovie/production.log

12-9・念のために再起動する

/etc/rc.d/init.d/httpd restart
 rake assets:precompile --trace
 touch tmp/restart.txt

以上です

参考にしたサイト
http://www.oiax.jp/rails3book

改訂新版 基礎Ruby on Rails (IMPRESS KISO SERIES)

改訂新版 基礎Ruby on Rails (IMPRESS KISO SERIES)

Railsの勉強のために5冊かったけど
この本が一番初心者にやさしかった
scaffoldではない方を
よく書いてあってたすかりました

またサポートサイトも充実していて
バージョンちがいのことなどが記載されています

他の4冊は以下

Ruby on Rails 3 ポケットリファレンス

Ruby on Rails 3 ポケットリファレンス

10日でおぼえる Ruby on Rails入門教室

10日でおぼえる Ruby on Rails入門教室

Ruby on RailsによるWebアプリケーション・スーパーサンプル改訂版

Ruby on RailsによるWebアプリケーション・スーパーサンプル改訂版

Rails3レシピブック 190の技

Rails3レシピブック 190の技