LoginSignup
12
10

More than 5 years have passed since last update.

ConohaVPSにまっさらな状態からCentOS+Ruby+Rails+nginx+unicornの構築

Last updated at Posted at 2016-07-26

Railsとサーバーの勉強のためConohaVPSを借りてCentOS+Ruby+Rails+nginx+unicornの環境を構築しました。
初心者なので怪しいところがあるかもしれません、、、
IPアドレスとRailsのアプリ名は適当に変えてください。

ConohaVPSの初期化

管理コンソールからCentOS(今回は7.2)をインストールしてサーバーを起動します。
rootのパスワードを入力する必要があります。
Conohaは管理画面がわかりやすくて好きです。このはちゃん可愛い

ログインとその他

ターミナルを起動してサーバーにログインします。

terminal.app
$  ssh root@$IP_ADDRESS

ssh接続してからOSを再インストールしたりすると警告が出ます。
その時は以下の以下のコマンドを実行する必要があります。

terminal.app
$  ssh-keygen -R $IP_ADDRESS

CentOSのアップデートをします。

terminal.app
$  yum update

本来はアップデート後に新規作業用ユーザーを設定したり、rootでのログインを禁止したり、鍵を作ったりしますがここでは省略します。

gitのインストール

terminal.app
$  yum install git

必要パッケージのインストール

Ruby,Railsに必要(?)な依存パッケージ達をインストールします。環境に合わせて必要なものをインストールします。
とりあえずインストールしておけば良いと思います、喧嘩はしないはず、、、!!

terminal.app
$ yum -y install openssl-devel readline-devel zlib-devel libcurl-devel gcc make openssl-devel 
$ yum install gcc-c++ glibc-headers openssl-devel readline libyaml-devel readline-devel zlib zlib-devel
$ yum install -y sqlite sqlite-devel

rbenv,Rubyをインストール

rbenvを用いてRubyをインストールします。export文のみでrubyをインストールしている記事がたまにありますが、export文だけだとログアウトするとパスが切れるので気をつけてください。

terminal.app
$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile
$ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
$ cd ~/.rbenv/plugins/ruby-build
$ sudo ./install.sh

インストール可能なRuby一覧を表示

terminal.app
rbenv install -l

今回は最新のバージョンの2.3.1をインストール

terminal.app
rbenv install -v 2.3.1

使用バージョン設定

terminal.app
$ rbenv rehash
$ rbenv global 2.3.1
$ ruby -v

Railsのインストール
バージョンを指定してインストール、ここでは4.X系の最新バージョンをインストール

terminal.app
$ gem install rails --version="~>4.0"
$ rbenv rehash
$ rails -v

RailsのアプリケーションはHomeディレクトリに作成しました。

terminal.app
$ cd /home/
$ rails new appName

Gemfileの設定です。therubyracerの行をコメントインして追加します。

terminal.app
$ cd /home/appName
$ vi Gemfile

またbundle install

terminal.app
$ bundle install

起動

terminal.app
$ rails s
$ curl localhost:3000

二つ目のターミナルを開いてサーバーにアクセス

terminal.app
$ ssh root@$IP_ADDRESS
$ curl localhost:3000

html的な何かが表示されるはずです。

Nginxインストール

terminal.app
$ yum -y install nginx

自動起動設定

terminal.app
$ systemctl enable nginx.service

ちなみにこれでも動きますがCentOS7では古くなっています。

terminal.app
$ chkconfig nginx on

起動

terminal.app
$ systemctl start nginx.service

(起動確認)

terminal.app
$ systemctl status nginx.service

これでブラウザから確認すればnginxのページを見ることができる。はず。
ブラウザから見れない時はファイヤーウォールが有効になっている場合がある

http://mosuke5.hateblo.jp/entry/2014/09/20/180326

macで以下のコマンドを実行
ファイヤーウォールが入っている場合、telentでエラーを吐く
Cent7だとiptableは無く、firewallを使うのがデフォらしい。

terminal.app
$ ping IP_ADDRESS
$ telnet IP_ADDRESS
$ traceroute IP_ADDRESS

インストール

terminal.app
$ yum install iptables-services
$ systemctl status firewalld
  #activeと出る
$ systemctl stop firewalld
$ systemctl status firewalld
$ systemctl disable firewalld
$ systemctl enable iptables
$ systemctl start iptables
$ systemctl status iptables

iptablesで解放します。

terminal.app
$ vim /etc/sysconfig/iptables

22番ポートの下に80番ポートを解放する旨を記入

terminal.app
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT

再起動

terminal.app
$ systemctl restart iptables.service

これでブラウザからIP.を打てば見えるはず

Nginx の設定

Nginxの設定を変更する
本当は cong.d ディレクトリ以下をなんとかする。
うまくできなかったので直接編集しました。

terminal.app
$ cd /etc/nginx/
$ cp nginx.conf nginx.conf_old
$ vi nginx.conf
nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
  root /home/appName/public;

  upstream unicorn-server {
    server unix:/home/appName/tmp/unicorn.sock;
  }

  server {
    listen 80;
    client_max_body_size 4G;
    server_name _;
    keepalive_timeout 5;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    root /home/appName/public;

    location / {
      proxy_pass http://unicorn-server;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;
     }
   }
}


Unicornの設定

先ほど作ったRailsディレクトリに移動する。
homeディレクトリの作ったところに行く。
uincornの行のコメントアウトを外す

terminal.app
$ cd /home/appName/
$ vi Gemfile

Bundle install

terminal.app
$ bundle install

unicornの実行ファイルを作成する

terminal.app
$ vi unicorn.rb
unicorn.rb
@app_path = '/home/appName'

worker_processes 2
working_directory "#{@app_path}/"

# This loads the application in the master process before forking
# worker processes
# Read more about it here:
# http://unicorn.bogomips.org/Unicorn/Configurator.html
preload_app true

timeout 30

# This is where we specify the socket.
# We will point the upstream Nginx module to this socket later on
listen "#{@app_path}/tmp/unicorn.sock", :backlog => 64

pid "#{@app_path}/tmp/unicorn.pid"

# Set the path of the log files inside the log folder of the testapp
stderr_path "#{@app_path}/log/unicorn.stderr.log"
stdout_path "#{@app_path}/log/unicorn.stdout.log"

before_fork do |server, worker|
  defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!

  old_pid = "#{server.config[:pid]}.oldbin"
    if old_pid != server.pid
      begin
        sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
        Process.kill(sig, File.read(old_pid).to_i)
      rescue Errno::ENOENT, Errno::ESRCH
      end
    end

    sleep 1
  end

after_fork do |server, worker|
  defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
end

railsアプリディレクトリ直下でユニコーンコマンドを起動

terminal.app
$ cd /home/appName
$ bundle exec unicorn_rails -c config/unicorn.rb -E development -D

起動確認

terminal.app
$ ps -ef | grep unicorn | grep -v grep

終了

terminal.app
$ kill -9 (プロセスID)

Nginxを再起動

terminal.app
$ systemctl restart nginx.service

Railsを起動

terminal.app
$ cd /home/appName
$ rails s

これで動くはずです!!

参考にさせていただいたページなど

ありがとうございました!

rails rubyのインストール
http://qiita.com/Fendo181/items/d14ebfb148223c8e5ecb

unicornの設定など
http://qiita.com/katsuhiko/items/90cb84033c305df2238b

12
10
0

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
12
10