読者です 読者をやめる 読者になる 読者になる

ボクココ

サービス開発を成功させるまでの歩み

Nginx,Unicorn,Ruby on Rails3.2.8 , MongoDB, FusionClound,(LocomotiveCMS) でアプリ作成

今まではHeroku様のPaaSでじゃんじゃんRailsアプリを作っていたわけだが、今回はインフラ回りもちょいと興味を持ち始めたので、IaaSでRails環境の構築からがんばってみた。
エンジニアであれば、最先端行きたいよね、ってことで nginx を選択。Apacheの構築を学習せずにいきなり行ってしまいました・・w
RailsサーバにLocomotiveを入れてブログっぽいのを軽く作ってみようと思います。
Locomotive: http://www.locomotivecms.com/
そのうちFusionCloudは有料になっちゃうので、その後どうやって運用しようかは考え中。。。



//Update 系
sudo yum update

sudo yum install build-essential zlib1g zlib1g-dev libxml2 libxml2-dev libxslt-dev sqlite3 libsqlite3-dev locate git-core


sudo yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison iconv-devel
増えた?

//RVM
bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

vi ~/.bashrc

以下を追加
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
source "$HOME/.rvm/scripts/rvm"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

source ~/.bashrc

//rails install
sudo yum install build-essential bison openssl libreadline5 libreadline-dev curl git-core zlib1g zlib1g-dev libssl-dev vim libsqlite3-0 libsqlite3-dev sqlite3 libreadline-dev libxml2-dev git-core autoconf


sudo yum -y install gcc
sudo yum -y install make
sudo yum -y install gcc-c++
sudo yum -y install zlib-devel
sudo yum -y install httpd-devel
sudo yum -y install openssl-devel
sudo yum -y install curl-devel

rvm pkg install zlib
rvm pkg install libyaml

rvm install 1.9.3
rvm use ruby-1.9.3-p194 --default
ruby -v
#ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
gem -v
#1.8.24

gem install rails -v 3.2.8 --no-rdoc --no-ri

gem install unicorn


//MongoDB
sudo yum -y install git-core scons gcc-c++ glibc-devel
mkdir src && cd src
sudo wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.2.0.tgz
tar xzf mongodb-linux-x86_64-2.2.0.tgz
sudo mkdir -p /data/db/
sudo chown `id -u` /data/db
./mongodb-linux-x86_64-2.2.0/bin/mongod
#Ctrl+C
curl -L http://ewanleith.com/downloads/mongodb.txt > mongodb.txt

sudo cp mongodb.txt /etc/init.d/mongodb
sudo chmod 755 /etc/init.d/mongodb
sudo chown root:root /etc/init.d/mongodb
sudo mkdir -p /usr/local/mongodb/bin/
sudo cp -r mongodb-linux-x86_64-2.2.0/bin/* /usr/local/mongodb/bin/
sudo /sbin/chkconfig
sudo /sbin/chkconfig mongodb on
sudo /sbin/iptables -F
sudo /etc/init.d/iptables save
sudo vi /etc/sysconfig/iptables
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

OUTPUT ACCEPT [70
7580]
  • A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sudo /sbin/service iptables restart


//nginx
sudo useradd -s/sbin/nologin -d/usr/local/nginx -M nginx
sudo yum install pcre pcre-devel
sudo wget http://nginx.org/download/nginx-1.3.5.tar.gz
tar zxf nginx-1.3.5.tar.gz
cd nginx-1.3.5
./configure --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module
make
sudo make install
sudo vi /etc/init.d/nginx
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/usr/local/nginx/logs/nginx.lock

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
sleep 1
start
}

reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

sudo chmod +x /etc/init.d/nginx
sudo /sbin/chkconfig --add nginx
sudo /sbin/chkconfig nginx on
sudo vim /usr/local/nginx/conf/nginx.conf
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
user nobody;
コメント外す
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sudo /sbin/service nginx start

ここで固定IPアドレスでアクセスしてみる。
「Welcome to nginx!」
との感動のご対面。


//アプリ用環境
//RMagic

cd ~/src
sudo wget ftp://ftp.kddlabs.co.jp/graphics/ImageMagick/ImageMagick-6.7.9-3.tar.gz
tar xzf ImageMagick-6.7.9-3.tar.gz
cd ImageMagick-6.7.9-3
./configure
make
sudo make install
sudo ln -s /usr/local/lib/libMagickCore.so.2 /lib/


//Locomotive
cd ~/dev/
sudo yum install libxml2-devel
sudo yum install libxslt-devel
rails new blog --skip-active-record --skip-test-unit --skip-javascript --skip-bundle
cd blog/
vim Gemfile
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
group:assets の中に
gem 'compass-rails', '~> 1.0.2'

外に
gem 'locomotive_cms', '~> 2.0.0.rc9', :require => 'locomotive/engine'
gem 'therubyracer', '>= 0.8.2'

コメントを外す
# Use unicorn as the app server
gem 'unicorn'

# Deploy with Capistrano
gem 'capistrano'

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
bundle install
sudo vi /etc/ld.so.conf
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1行目に追加
/usr/local/lib
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sudo /sbin/ldconfig


bundle exec rails g locomotive:install

vim config/initializers/locomotive.rb
それっぽく設定
vim config/mongoid.yml
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
production:
<<: *defaults database: locomotive_pro ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ vim config/unicorn.rb ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # ワーカーの数 worker_processes 2 # capistrano 用に RAILS_ROOT を指定 working_directory "/home/f-user/dev/blog" # ソケット listen '/tmp/unicorn_of_example.sock' # ログ stderr_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT']) stdout_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT']) # ダウンタイムなくす preload_app true before_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! old_pid = "#{ server.config[:pid] }.oldbin" unless old_pid == server.pid begin # SIGTTOU だと worker_processes が多いときおかしい気がする Process.kill :QUIT, File.read(old_pid).to_i rescue Errno::ENOENT, Errno::ESRCH end end end after_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sudo mkdir -p /etc/nginx/sites-available/ sudo vim /etc/nginx/sites-available/myblog.com ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ upstream unicorn-of-example { server unix:/tmp/unicorn_of_example.sock; } server { listen 80; server_name {servername}; rewrite ^/(.*) http://{servername}//$1 permanent; } server { listen 80; server_name {servername}; # http://blog.monoweb.info/article/2012021823.html add_header X-Frame-Options DENY; add_header X-Content-Type-Options nosniff; root /home/f-user/dev/blog/public; error_log /home/f-user/dev/blog/log/error.log; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; if (!-f $request_filename) { proxy_pass http://unicorn-of-example; } } location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ { expires 1y; add_header Cache-Control public; # http://guides.rubyonrails.org/asset_pipeline.html#server-configuration add_header Last-Modified ""; add_header ETag ""; } } ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ sudo mkdir /etc/nginx/sites-enabled sudo ln -s /etc/nginx/sites-available/myblog.com /etc/nginx/sites-enabled/myblog.com sudo /sbin/service nginx restart sudo pgrep -f 'unicorn_rails master' XXXX XXXX sudo kill -HUP XXXX すみません、ここらへんnginx+unicorn+railsつなげるのに苦戦。 メモ: px aux | grep nginx で出てきたユーザ名と、 nginx.conf にある user名が一致していないと、パーミッションエラーで悩まされることになる。 再起動などしながらプロセスのユーザ名には注意。 cd {railsroot} vi config/environments/production.rb ^^^^^^^^^^^^^^ config.assets.compress = true ^^^^^^^^^^^^^ bundle exec rake assets:precompile


Locomotive がんばりましたが、「アセットプリコンパイルできていないよ」エラーがどうしても取れず。。ローカルでやると普通に動くのに;; ということで普通のRailsアプリをこちょこちょ作ってみます。

sudo cp /etc/nginx/sites-available/myblog.com /etc/nginx/sites-available/hello.com
sudo rm /etc/nginx/sites-enabled/myblog.com
sudo ln -s /etc/nginx/sites-available/hello.com /etc/nginx/sites-enabled/
rvm gemset list
rvm gemset use rails3.2
rails new hello -T -O
cd hello/
vim Gemfile
^^^^^^^^^
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer', :platforms => :ruby

gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'
gem 'mongoid'
gem 'mongo_ext'
gem 'bson_ext'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
gem 'unicorn'
^^^^^^^^^
bundle install
rails g mongoid:config
vim config/mongoid.yml
^^^^^^^^^
production:
# Configure available database sessions. (required)
sessions:
# Defines the default session. (required)
default:
# Defines the name of the default database that Mongoid can connect to.
# (required).
database: hello_development
# Provides the hosts the default session can connect to. Must be an array
# of host:port pairs. (required)
hosts: - localhost:27017 options: # Change whether the session persists in safe mode by default.
# (default: false)
# safe: false

# Change the default consistency model to :eventual or :strong.
# :eventual will send reads to secondaries, :strong sends everything
# to master. (default: :eventual)
consistency: :strong

^^^^^^^^^
rails g scaffold Post name:string body:text
sudo vim /etc/nginx/sites-available/hello.com
^^^^^^^^^^^^^
root 変更
^^^^^^^^^^^^^
vim config/unicorn.rb
^^^^^^^^^^^^^
listen を /etc/nginx/sites-available/hello.com のところと合わせる
^^^^^^^^^^^^^
bundle exec rake assets:precompile
unicorn_rails -c config/unicorn.rb -E production -p 5000 -D
sudo /sbin/service nginx restart
ps aux | grep nginx
kill 10979
sudo /sbin/service nginx restart


で ~/posts アクセスできたら成功!