LoginSignup
112
113

More than 5 years have passed since last update.

UbuntuでNginxとUnicornを使いRailsアプリを動かす

Last updated at Posted at 2013-09-19
  • Ubuntu 13.04
  • Nginx 1.5.4
  • Ruby 2.0.0p247
  • Ruby on Rails 4.0.0

最新のNginxを入れる

cd ~
Nginxの鍵をダウンロードし追加する
wget http://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key

/etc/apt/sources.listに以下の2行を追加する
deb http://nginx.org/packages/mainline/ubuntu/ raring nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ raring nginx

apt-getを更新
sudo apt-get update

Nginxをインストール
sudo apt-get install nginx

ついでにGitもインストール
sudo apt-get install git

rbenvを入れる

cd ~
git clone git://github.com/sstephenson/rbenv.git .rbenv
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

~/.bash_profileに以下の2行を追加する

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

source ~/.bash_profileで設定を反映させる

Rubyをインストール

インストールできるバージョンを確認
rbenv install --list
今回は2.0.0-p247というバージョンをインストールします
rbenv install 2.0.0-p247
rbenv rehash
デフォルトに設定
rbenv global 2.0.0-p247

Railsをインストール

gem install rails
rbenv rehash

ついでにMySQLをインストール

sudo apt-get install mysql-server

Nginxの設定

/etc/nginx/conf.d/に設定ファイルを作成する
sudo vim /etc/nginx/conf.d/test.conf
設定ファイル(例)

    upstream test {
            server unix:/tmp/test.sock;
    }

    server {
            listen          80;
            server_name     test.jp;
            access_log      /var/www/test/logs/access.log;
            error_log       /var/www/test/logs/error.log;

            location / {
                    proxy_pass http://test;
            }


            location = /robots.txt  { access_log off; log_not_found off; }
            location = /favicon.ico { access_log off; log_not_found off; }

    }

Nginxの再起動
service nginx restart

作ったRailsアプリの設定

GemfileにUnicornで必要なものを追加する
gem 'unicorn'
gem 'execjs'
gem 'therubyracer'

そして
bundle install

RailsのconfigフォルダにUnicornの設定ファイルを作成
vim config/unicorn.rb
設定ファイル(例) 最低限なものしか書いてありません。。。
listen '/tmp/test.sock'
pid '/tmp/test.pid'

最後にUnicornを起動
unicorn_rails -c config/unicorn.rb -E production -D

Unicornが動いているか確認するには
ps -ef | grep unicorn | grep -v grep

Unicornをとめるには
kill -QUIT 【プロセスID】

112
113
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
112
113