Web + Life Hack

〜True But Useless〜

【初心者】「Rails + Passenger+memchached」の構成にピンっと来た人に見てほしい。

私が携わっているサービスは最近のweb系の環境と少し変わってて、
サーバーを「Rails + Passenger+memchached」で構成しています。

次回からサクッと出来るように、
そして初心者の方には詰まる苦しみから解放するために
自分が詰まったところを纏めてみました。

少し長めの記事です。。。

・タイプミスをしていないかチェック

基本中の基本ですが
あながち見落としがちなのでまずはじめにタイプミスをチェックします。
例えば
Cuteがスペルとしては正なのですが
Quteなど
「本来の意味やスペルとは違うが意図が合ってわざとそのような名称を使用している」場合が
稀にあります。

その場合、通常の設定よりもかなりタイプミスが発生しますので、注意しましょう。

・AWSのEC2ならubuntuのバージョンを確認する。

設定は正しいのにバージョン依存で失敗するという場合もあります。
なのでまずはサーバーのOSバージョンを確認しましょう。

そのコマンドはこちら

cat /etc/lsb-release

本記事は以下のバージョンを前提で行っています。

DISTRIB_DESCRIPTION="Ubuntu 12.04.2 LTS"

・apache2.confを確認する。

デフォルトの状態から自分のアプリに修正する必要があります。
こちらは「apache2.conf」からコメントアウトを外したものや
追記したものになります。
場所を特定しやすいようにあえてコメントの箇所も追記しています。

ちなみに配置先は/etc/apache2になります。

# Do NOT add a slash at the end of the directory path.
#
ServerRoot "/etc/apache2"

# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here. If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
#
ErrorLog /var/log/apache2/error.log


# Include module configuration:
Include /etc/apache2/mods-enabled/*.load
Include /etc/apache2/mods-enabled/*.conf


# Include ports listing
Include /etc/apache2/ports.conf

#
# Define an access log for VirtualHosts that don't define their own logfile
CustomLog /var/log/apache2/other_vhosts_access.log vhost_combined

# Include generic snippets of statements
Include /etc/apache2/conf.d/

# Include the virtual host configurations:
# httpで使用する設定ファイルの配置先になります。
Include /etc/apache2/sites-enabled/

# ServerName g08m11jp:80
# 自分の持っているドメイン名になります。
ServerName g08m11.jp

・defaultファイルを修正する。

passengerによりhttp通信をする場合は、defaultファイルを使用します。
しかしこちらも自分のアプリ用に修正する必要があります。
配置先は/etc/apache2/site-availableです。

NameVirtualHost *:80

<VirtualHost *:80>

RailsEnv production
PassengerMaxPoolSize 20
PassengerMaxRequests 400
PassengerPreStart http://g08m11.jp
ServerAdmin webmaster@g08m11.jp
ServerAlias g08m11.jp *.g08m11.jp
ServerName g08m11.jp



DocumentRoot /home/g08m11/apli/public
PassengerEnabled on
PassengerHighPerformance on
ErrorLog /var/log/apache2/error.log
ErrorLog "|/usr/bin/logger -p local6.info -t http-error"
LogLevel warn
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{X-Forwarded-Port}i\" \"%{X-Forwarded-Proto}i\" \"%{X-Forwarded-For}i\"" combined
CustomLog /var/log/apache2/access.log combined
CustomLog "|/usr/bin/logger -p local6.info -t http-access" combined


Header add X-App-Server "production-test"
Header always unset X-Powered-By
Header always unset X-Rack-Cache
Header always unset Server
</VirtualHost>

・database.ymlを修正する

こちらはrails側の設定になります。
productionモードで起動するための設定です。
配置先は/home/g08m11/apli/configです。
また以降でも配置先にg08m11/apliがでますが、
こちらは自分のRailsソースの配置先に読み変えてください。


production:
adapter: mysql2
database: g08m11_test
host: g08m11.jp
username: g08m11
password: g08m11
pool: 5
timeout: 5000
encoding: utf8
socket: /usr/local/var/mysql/mysql.sock

production_slave_database:
adapter: mysql2
host: g08m11.jp
#host: g08m11.cevifac5tytd.ap-northeast-1.rds.amazonaws.com
database: g08m11
username: g08m11
password: g08m11_pass
pool: 5
timeout: 5000
encoding: utf8

production_slave_database_2:
adapter: mysql2
host: g08m11.jp
# host: g08m11.cevifac5tytd.ap-northeast-1.rds.amazonaws.com
database: g08m11_test
username: g08m11
password: g08m11_pass
pool: 5
timeout: 5000
encoding: utf8

・production.rbを修正する。

こちらはrails側の設定になります。
今までdevelopmentモードで起動していた方は.sampleになっていますので

cd /home/g08m11/apli/config/environment
mv -f production.sample.rb production.rb 

などでファイル名を変更しましょう。

production.rbの配置先は/home/g08m11/apli/config/environmentになります。

require 'tlsmail'
require 'memcache'
require 'openid/store/memcache'
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'admin.g08m11.net',
:authentication => :plain,
:user_name => 'g08m11@g08m11.com',
:password => 'g08m11'
}


G08m11::Application.configure do
# Settings specified here will take precedence over those in config/application.rb

# Code is not reloaded between requests
config.cache_classes = true


# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = true
config.action_controller.perform_caching = false

# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = true

# Compress JavaScripts and CSS
config.assets.compress = false 


# Specify the default JavaScript compressor
config.assets.js_compressor = :uglifier


# See everything in the log (default is :info)
config.log_level = :debug


# config.action_mailer.raise_delivery_errors = false
config.action_mailer.default_url_options = { :host => 'g08m11.jp' }


# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true


# Send deprecation notices to registered listeners
#config.active_support.deprecation = :notify
# for multi_db only (need slave database setting on database.yml)
config.active_support.deprecation = :notify
config.after_initialize do
G08m11::Application.routes.draw do
match "*path" => "application#render_404"
end


if defined?(PhusionPassenger)
  PhusionPassenger.on_event(:starting_worker_process) do |forked|
    if forked
       MultiDb::ConnectionProxy.setup!
    end
  end
else
  MultiDb::ConnectionProxy.setup!
end


config.cache_store = :mem_cache_store, 'localhost', {:namespace => 'G08m11'}
config.middleware.insert_before(Warden::Manager, Rack::OpenID, OpenID::Store::Memcache.new(MemCache.new('localhost:11211')))

end

・tmpディレクトリの実行権限を変更する。

サーバーの設定をスーパユーザーのみで行った場合、他のユーザーだと権限が無いことから落ちる場合があり、
その場合の多くはtmpファイルのログファイル書き込みや一時ファイルの作成が出来ずに落ちるパターン
多いです。
またこの手の問題は事象の切り分けが難しい(「Rails側の設定の誤りなのか?」や「apacheの設定誤りなのか?」など)ので
詰まる前に権限を「600」と変更しておきましょう。

コマンドは以下です。

chmod -R 600 /tmp

・database.ymlの設定を見直す。

タイプミスをしていないかもそうですが、
database.ymlは「:」が多々あり、
「:」の箇所は後ろに半角スペースが必要になります。
これを知らずに設定するとハマります。
ターミナルやvimの表示設定を変えるなりして分かりやすいように変更しましょう。

apache再起動を忘れない。

いざ、apacheの設定、Railsの設定、passengerの設定が終わった後は設定を反映させるべく、
再起動をしましょう。

コマンドは以下です。

sudo /etc/init.d/apache2 restart

・ブラウザの履歴を削除する。

以前のサーバーの情報がローカルPC内のブラウザ内に残っており、反映がされていないように見える場合があります。
その時は「履歴」を削除してみましょう。

各ブラウザの履歴削除方法はこちらを参考。
http://www.landmarx.co.jp/%E3%82%B3%E3%83%A9%E3%83%A0/%E3%83%96%E3%83%A9%E3%82%A6%E3%82%B6%E5%88%A5%E3%82%AD%E3%83%A3%E3%83%83%E3%82%B7%E3%83%A5%E3%81%AE%E3%82%AF%E3%83%AA%E3%82%A2%E6%96%B9%E6%B3%95%E3%81%BE%E3%81%A8%E3%82%81/


その他にもbundle installの実行忘れやGemfileのバージョン指定忘れなどもありますが、
それはまた別の機会に書きたいと思います。