LoginSignup
27
27

More than 5 years have passed since last update.

AWSのEC2インスタンスをローンチしてから、Apache + passenger + Ruby2.1.2 + Rails4.1.4の環境を構築し世の中に公開するまでのコマンドまとめ

Posted at

ひたすら、コマンドだけまとめてます。
本職デザイナーなので間違ってるとこあるかもしれませんが、その場合はご指摘ください。

立ち上げたインスタンスにSSHで接続してください。

以下のコマンド内では
ドメイン名: domain_name
railsアプリ名: rails_app_name
→ 環境変数で使用する場合は RAILS_APP_NAME
として記載してますので、それぞれ書き換えて使用してください。

あと、途中で 長い文字列1 長い文字列2 を生成して使っている箇所もあるので、
そこも書き換えて使用してください。

sudo yum update -y
sudo yum install gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison httpd httpd-devel curl-devel git sqlite sqlite-devel mysql mysql-devel mysql-server -y
sudo git clone git://github.com/sstephenson/rbenv.git /usr/local/rbenv
sudo mkdir /usr/local/rbenv/shims /usr/local/rbenv/versions /usr/local/rbenv/plugins /var/apps
sudo git clone git://github.com/sstephenson/ruby-build.git /usr/local/rbenv/plugins/ruby-build
echo 'export RBENV_ROOT="/usr/local/rbenv"' >> .bash_profile
echo 'export PATH="$RBENV_ROOT/bin:$PATH"' >> .bash_profile
echo 'eval "$(rbenv init -)"' >> .bash_profile
source .bash_profile
sudo visudo

# ↓79行目に以下を追記
Defaults    env_keep += "RBENV_ROOT"

# ↓87行目を以下に変更
Defaults    secure_path = /usr/local/rbenv/shims:/usr/local/rbenv/bin:/sbin:/bin:/usr/sbin:/usr/bin
sudo rbenv install 2.1.2
sudo rbenv global 2.1.2
sudo gem install bundler passenger rails
sudo rbenv rehash
sudo passenger-install-apache2-module

sudo vi /etc/httpd/conf/httpd.conf

/etc/httpd/conf/httpd.conf
# ※以下はRubyやPassengerのバージョンが変わると内容が変わります
# ※passengerをビルドした時に最後に表示されるものを追記してください

# ↓202行目に以下を追記
LoadModule passenger_module /usr/local/rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/passenger-4.0.48/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
  PassengerRoot /usr/local/rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/passenger-4.0.48
  PassengerDefaultRuby /usr/local/rbenv/versions/2.1.2/bin/ruby
</IfModule>
sudo chkconfig httpd on
sudo chkconfig mysqld on
sudo service httpd start
sudo service mysqld start

ここまでで、EC2インスタンスのイメージを作成しておくとのちのち便利。

irb
require 'securerandom'
SecureRandom.hex(64)
"長い文字列1"
SecureRandom.hex(64)
"長い文字列2"
exit

長い文字列1長い文字列2 は環境変数として使うのでmemoなどにコピーしておく。

echo 'export SECRET_KEY_BASE=長い文字列1' >> .bash_profile
echo 'export RAILS_APP_NAME_DATABASE_PASSWORD=長い文字列2' >> .bash_profile
source .bash_profile
sudo visudo

# ↓80行目に以下を追記
Defaults    env_keep += "SECRET_KEY_BASE RAILS_APP_NAME_DATABASE_PASSWORD"
cd /var/apps/
sudo rails new rails_app_name -d mysql --skip-bundle
cd rails_app_name
sudo vi Gemfile

# ↓15行目 行頭の#を削除してコメントイン
gem 'therubyracer',  platforms: :ruby
sudo bundle install
mysql -u root -p

password聞かれるので何も入れずにEnter。

create user 'rails_app_name'@'localhost' identified by '長い文字列2';
grant all on *.* to 'rails_app_name'@'localhost';
exit
rake db:create:all
sudo vi /etc/httpd/conf/httpd.conf

/etc/httpd/conf/httpd.conf
# ↓末尾
<VirtualHost *:80>
  ServerName dmain_name
  DocumentRoot /var/apps/rails_app_name/public
  <Directory /var/apps/rails_app_name/public>
    SetEnv SECRET_KEY_BASE 長い文字列1
    SetEnv RAILS_APP_NAME_DATABASE_PASSWORD 長い文字列2
    AllowOverride all
    Options -MultiViews
  </Directory>
</VirtualHost>
sudo service httpd restart

あとは、routeの設定してやればオッケー。なはず。

27
27
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
27
27