Skip to content

Instantly share code, notes, and snippets.

@jacoyutorius
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save jacoyutorius/7187fc9879aef1d8c5a7 to your computer and use it in GitHub Desktop.

Select an option

Save jacoyutorius/7187fc9879aef1d8c5a7 to your computer and use it in GitHub Desktop.
CentOS + Ruby + Rails + unicorn + nginx

VM作り

cd 作業ディレクトリ
mkdir hmrb
cd hmrb
vagrant init

# centosのboxが無い場合インストール
vagrant box install http://centos6.4x86_64(適当) centos6.4x86_64   <-  コマンドうろ覚え・・・

Vagranfile

Vagrant.configre(2) do |config|
  config.vm.box = "centos6.4x86_64"
  config.vm.network "forwarded_port", guest: 22, host: 2210
  config.vm.network "forwarded_port", guest: 80, host: 8010   # nginx用
  config.vm.network "forwarded_port", guest: 3000, host: 3010 # Rails用
end

VM起動

vagrant up

CentOSセットアップ

vagrant ssh
sudo yum update
sudo yum install gcc gcc-c++ make zlib-devel httpd-devel opebssl-devel curl-devel sqlite-devel readline readline-devel vim tree

サーバーはnginx + unicornにしてみようと思う

sudo yum install nginx
sudo nginx
gem install unicorn

nginxが起動するので、起動確認。 Vagrantfileのforwardportで指定した8010ポートへアクセスする。

ルートパスの変更

nginxインストール直後だと、/etc/nginx/htmlがルートパスに設定されているので変更。 /var/www/htmlにする。

/etc/nginx/conf.d/default.conf

/etc/nginx/html => /var/www/html に書き換え
chown -R vagrant /var/www/html
chgrp -R vagrant /var/www/html
touch /var/www/html/index.html

index.html設置後、nginxをリロード。

sudo nginx -s reload

localhost:8010にアクセスして、設置したhtmlが表示されていればOK。

zsh / oh-my-zshのインストール

特にこだわりがあるというわけでもないけど、zshにしておく。

sudo yum install zsh wget
chsh   => "/bin/zsh"を入力
wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O - | sh

~/.zshrc

plugins=(git bundler osx rake ruby) <=に書き換え

テーマはhttps://github.com/robbyrussell/oh-my-zsh/wiki/themes を見て好みのものを設定してださい。

Ruby / Railsのインストール

基本的には参考ページに書いてあるコマンド丸コピでOK。 zshにしたので、.bash_profileではなく、.zshrcにパスの設定を書いています。

git clone git://github.com/sstephenson/ruby-build.git
cd ruby-build
sudo ./install.sh

git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(rbenv init -)"' >> ~/.zshrc
source ~/.bashrc
exec $SHELL -l
ebenv install -l
rbenv install 2.1.5
rbenv global 2.1.5
rbenv rehash

起動(unicorn + nginx)

unicornの設定

/var/www/html/sample/config/unicorn.conf

worker_processes 2
#listen '/tmp/unicorn.sock'
listen File.expand_path("unicorn.sock", File.dirname(__FILE__) + "/../tmp/sockets")

stderr_path File.expand_path('unicorn.log', File.dirname(__FILE__) + '/../log')
stdout_path File.expand_path('unicorn.log', File.dirname(__FILE__) + '/../log')
preload_app true
unicorn -c config/unicorn.conf -D

バーチャルホストの設定

/etc/nginx/conf.d/virtual.conf

upstream sampleapp {
	server unix:/var/www/html/sample/tmp/sockets/unicorn.sock;
}

server {
	listen       3000;
	listen       localhost:3000;
        server_name  localhost;

        location / {
            root /var/www/html/sample/public;
	    proxy_pass http://sampleapp;
        }
}
sudo nginx -t
sudo /etc/init.d/nginx restart

サービスの再起動が成功したら、ホスト側からアクセスしてみる。

http://localhost:3010

Railsの画面が表示されればOK!

VagrantのBoxを作る

作ったVMを再利用できるようにします。

vagrant halt
vagrant package --output base.box

参考にしたページ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment