0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

KAGOYAのVPSレンタルサーバーでRuby On Rails環境を構築

Posted at

KAGOYAのVPSレンタルサーバーで、
構築の勉強も含めてRubyOnRails環境を構築してみました。
自分用として載せておきます。

使用環境

MacOS
Linux
CentOS7

構築したい環境

Ruby On Rails
MySQL(MariaDBを使用)

参考にしたサイト

CentOS7にRubyとRuby on Rails環境構築
https://qiita.com/ponsea/items/aaff4c6b0bb8eed1d5e3

MariaDBのインストール/初期設定 [CentOS7]
https://qiita.com/ponsea/items/9578659009b434c60959

第4回CentOS7 に Ruby on Rails 環境をつくってみた
https://www.kagoya.jp/cloud/vps/tutorial/04.html

事前準備のコマンド

# sudo yum update
# sudo yum install -y git
# sudo yum install -y bzip2 gcc openssl-devel readline-devel zlib-devel

サーバーを最新の状態にアップデートし、
gitとコンパイルに必要な物をインストール

rbenvのインストール

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

ホームディレクトリにインストール

bashファイルに追記

# vi ~/.bashrc
↓下記を追加
--------------------------
# rbenv
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
--------------------------

追記したことを
以下のコマンドで反映

# source ~/.bashrc
# rbenv -v #バージョン確認

Rubyをインストール

# rbenv install 2.5.1 # 時間かかります

参考サイトと同様、2.5.1のバージョンのRubyをインストール。
下記のコマンドでインストールしたバージョンのRubyをrbenvに設定し、確認。

# rbenv global 2.5.1 # ※インストールしたバージョンを指定
# ruby -v # Rubyが実行できることを確認

Rails環境の作成

# gem install bundler

bundlerをインストールして、
作ったディレクトリにRailsの任意のバージョンの環境を構築する。

# mkdir <ディレクトリ名>
# cd <ディレクトリ名>
[〜 <ディレクトリ名>]# bundle init # カレントディレクトリにGemfileを作成
[〜 <ディレクトリ名>]# echo 'gem "rails", "5.2.0"' >> Gemfile # ※ 任意のバージョンを指定
[〜 <ディレクトリ名>]# bundle install --path vendor/bundle # ./vendor/bundleにRails関連のGemが入る

「bundle exec」省略設定

railsコマンドの「bundle exec」の部分を省略するために、
下記のコマンドでスクリプトをダウンロード

$ curl -L https://github.com/gma/bundler-exec/raw/master/bundler-exec.sh > ~/.bundler-exec.sh

viコマンドでbundler-exec.shを編集

# vi ~/.bundler-exec.sh
↓railsを下記の部分に追記
-----------------------------
## Main program

BUNDLED_COMMANDS="${BUNDLED_COMMANDS:-
cap
capify
chef
<<< 省略 >>>
unicorn
unicorn_rails
wagon
rails   <-ここに追記
}"

define-bundler-aliases

unset -f define-bundler-aliases
-----------------------------------

bashrcにも追記。

# vi ~/.bashrc
↓下記を追記
----------------------------
# "bundle exec" shortcut setting
[ -f ~/.bundler-exec.sh ] && source ~/.bundler-exec.sh
----------------------------

上記を反映

# source ~/.bashrc

railsプロジェクトの作成

Railsのプロジェクトは先ほど作成したディレクトリ内で作成します。

# cd <先ほど作成したディレクトリ名>

参考サイトのようにGemをプロジェクトごとのディレクトリにインストールするようにします。
下記のコマンドでRuby自体にGemがインストールされないようにします。

$ rails new <プロジェクト名> --skip-bundle # プロジェクトの作成

プロジェクトフォルダ内のvendor/bundle内に、プロジェクトで必要なGemをインストール。

# cd <プロジェクト名>
[〜 <ディレクトリ名>]# bundle install --path vendor/bundle # ./vendor/bundleにGemをインストール
[〜 <ディレクトリ名>]# echo '/vendor/bundle' >> .gitignore # Gitの管理対象から外す

MySQL(MariaDB)の設定

参考サイトより(今回は省略)

MariaDBのインストール/初期設定 [CentOS7]
https://qiita.com/ponsea/items/9578659009b434c60959

Railsアプリケーション起動

railsを起動する前に、
Rails のいくつかの機能は、Javascript ランタイムが必要なので、
Node.js をインストールします。

# yum -y install epel-release
# yum install nodejs

準備が整ったので、
railsを起動させます。

rails s --binding=<任意のIPアドレス>

下記が表示されたらOK

=> Booting Puma
=> Rails 5.2.4.3 application starting in development 
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.6 (ruby 2.5.1-p57), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://<任意のIPアドレス>:3000
Use Ctrl-C to stop
Started GET "/" for <IPアドレス> at <日時>
Cannot render console from <IPアドレス>! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Rails::WelcomeController#index as HTML
  Rendering vendor/bundle/ruby/2.5.0/gems/railties-5.2.4.3/lib/rails/templates/rails/welcome/index.html.erb
  Rendered vendor/bundle/ruby/2.5.0/gems/railties-5.2.4.3/lib/rails/templates/rails/welcome/index.html.erb (2.4ms)
Completed 200 OK in 16ms (Views: 6.0ms | ActiveRecord: 0.0ms)

ブラウザでアクセスして表示を確認
http://<任意のIPアドレス>:3000

上記のページが出ればOK。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?