conohaのクラウド centOS 6.7に、sinatoraをインスツール

Ubuntu16でRubyを使ってみる

を、元に、centOS 6.7にsinatraをインスツールしてみた。

■gitのインストール
gitのインストールしているか確認

git --version
・結果表示
-bash: git: コマンドが見つかりません

gitのがないのでインストール

yum -y install git

git --version
・結果表示
git version 1.7.1

■benv のインストール

git clone https://github.com/sstephenson/rbenv.git ~/.rbenv

echo 'export PATH="$HOME/.rbenv/bin:$PATH"'>> ~/.bashrc

echo 'eval "$(rbenv init -)"' >> ~/.bashrc

シェルを再起動

source ~/.bashrc

インストールの確認

type rbenv
・結果表示
rbenv is a function
rbenv () 
{ 
    local command;
    command="$1";
    if [ "$#" -gt 0 ]; then
        shift;
    fi;
    case "$command" in 
        rehash | shell)
            eval "$(rbenv "sh-$command" "$@")"
        ;;
        *)
            command rbenv "$command" "$@"
        ;;
    esac
}

■ruby-build のインストール

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

■ruby のインストール
インストール可能な、最新バージョンを確認

rbenv install -l

2.2.5をインストール

rbenv install 2.2.5

エラーになった

・結果表示
:
checking for gcc... no

gccがないので、の追加

yum -y install gcc

再度

rbenv install 2.2.5

エラーになった

・結果表示
:
Try running <code>yum install -y openssl-devel readline-devel zlib-devel</code> to fetch missing dependencies.

指示にしたが、追加

yum install -y openssl-devel readline-devel zlib-devel

再度

rbenv install 2.2.5

インストールできた

インストールした、Rubyのバージョンを確認

rbenv shell 2.2.5
ruby -v

ruby 2.2.5p319 (2016-04-26 revision 54774) [x86_64-linux]

■bundler のインストール

rbenv shell 2.2.5
gem install bundler

■sinatra のインストール

プロジェクト用エリア(フォルダ:sina)を作成

cd
mkdir sina
cd sina

Gemfileを作成

bundle init

Gemfileにgem ‘sinatra’を追加

cat Gemfile
echo 'gem "sinatra"' >> Gemfile
cat Gemfile
mkdir vendor
mkdir vendor/bundle

bundle で、Gemfileを実行

bundle install --path vendor/bundle

■サンプルAPの作成
app.rbを作成

echo "#coding: utf-8"    >app.rb
echo "require 'sinatra'" >>app.rb
echo "get '/' do"        >>app.rb
echo "'Hello World'"     >>app.rb
echo "end"               >>app.rb

cat app.rb

■実行確認

bundle exec ruby app.rb

INFO  WEBrick::HTTPServer#start: pid=30947 port=4567

別の端末を開く

curl 'http://localhost:4567/'
・結果表示
Hello World

できた

■passengerで接続

■Gemでpassengerをインストール
Gemfileにgem ‘passenger’を追加

cd
cd sina

cat Gemfile
echo 'gem "passenger"' >> Gemfile
cat Gemfile

rbenv shell 2.2.5
bundle install

■passenger-install-apache2-moduleをインストール

bundle exec passenger-install-apache2-module
・結果表示
Here's what you can expect from the installation process:

 1. The Apache 2 module will be installed for you.
 2. You'll learn how to configure Apache.
 3. You'll learn how to deploy a Ruby on Rails application.
  :
  :
Press Enter to continue, or Ctrl-C to abort.

1を、選択

・結果表示
Use <space> to select.
If the menu doesn't display correctly, press '!'

 ‣ ⬢  Ruby
   ⬢  Python
   ⬡  Node.js
   ⬡  Meteor

entrで、進んで行く

エラーになるので、一度中断し
指摘された、不足のアプリケーションを追加

yum -y install gcc-c++ libcurl-devel httpd httpd-devel

再度

bundle exec passenger-install-apache2-module

インストールできた

apacheに設定を追加

bundle exec passenger-install-apache2-module --snippet >> /etc/httpd/conf/httpd.conf
echo "DocumentRoot /root/sina/public" >> /etc/httpd/conf/httpd.conf

アプリケーション設置用エリアを作成

mkdir /root/sina/public
mkdir /root/sina/tmp

config.ruを作成

echo "require File.expand_path(File.dirname(__FILE__)) + '/app'"  >/root/sina/config.ru
echo "run Sinatra::Application"                                   >>/root/sina/config.ru

アプリケーション修正時の自動リロードを指定

touch /root/sina/tmp/always_restart.txt

apacheの設定ファイルの整合性を確認

apachectl configtest

Syntax OKなので、起動してみる

・結果表示
httpd: apr_sockaddr_info_get() failed for 133-130-108-243
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
Syntax OK

apacheを起動

/etc/rc.d/init.d/httpd restart

動作を確認してみる

curl 'http://localhost/'
・結果表示
  :
<p>You don't have permission to access /
on this server.</p>
  :

権限がまずいとのこと
権限を変更

chmod o+x "/root"

再度を確認してみる

curl 'http://localhost/'
・結果表示
Hello World

うまくいった。。

Apacheー>Passengerー>Sinatra ができた。