Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.
  • 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】

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away