LoginSignup
0
0

More than 3 years have passed since last update.

【コピペで行ける】CentOS7環境でRubyOnRailsを動かす_02

Posted at

3.前回の続き

前回の記事で作成したCentos7+Apache2.4+MariaDB環境にRubyOnRailsを導入します。
導入にあたりpassengerと呼ばれるミドルウェアを使用するのですが、お作法がわかればそんなに難しくないです。

3.1.諸々の追加設定

■rootで

yum install -y nodejs

yum -y install MariaDB-shared mariadb-devel

sed -i -e "$ a # rbenv" /home/butamayo/.bash_profile
sed -i -e '$ a export PATH="/home/butamayo/.rbenv/bin:$PATH"' /home/butamayo/.bash_profile
sed -i -e '$ a eval "$(rbenv init -)"' /home/butamayo/.bash_profile

mkdir -p /var/www/vhost/rails_dir

mkdir -p /var/log/httpd/{www,stg}/{80,443}

chmod 2755 -R /var/www/vhost

sed -i -e '$ a # "bundle exec" shortcut setting' /home/butamayo/.bash_profile
sed -i -e '$ a [ -f /home/butamayo/.bundler-exec.sh ] && source /home/butamayo/.bundler-exec.sh' /home/butamayo/.bash_profile

yum install -y sqlite-devel gcc-c++

yum install -y libcurl-devel httpd-devel apr-devel apr-util-devel

chmod o+x -R /var/www/vhost/rails_dir

vim /etc/httpd/conf.d/passenger.conf

LoadModule passenger_module /home/butamayo/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/passenger-6.0.4/buildout/apache2/mod_passenger.so
   <IfModule mod_passenger.c>
     PassengerRoot /home/butamayo/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/passenger-6.0.4
     PassengerDefaultRuby /home/butamayo/.rbenv/versions/2.6.4/bin/ruby
   </IfModule>


vim /etc/httpd/conf.d/01_www.conf

#rails
<VirtualHost *:80>
   ServerName  www.butamayo.jp
   DocumentRoot /var/www/vhost/rails_dir/www/public
   CustomLog /var/log/httpd/www/80/access_log combined
   ErrorLog /var/log/httpd/www/80/error_log
   RailsEnv production
   PassengerEnabled on
  <Directory /var/www/vhost/rails_dir/www/public>
    Options FollowSymLinks
    AllowOverride All
    Require all granted
    AddDefaultCharset UTF-8
  </Directory>
</VirtualHost>


sudo vim /etc/httpd/conf.d/02_stg.conf

#rails
<VirtualHost *:80>
   ServerName  stg.butamayo.jp
   DocumentRoot /var/www/vhost/rails_dir/stg/public
   CustomLog /var/log/httpd/stg/80/access_log combined
   ErrorLog /var/log/httpd/stg/80/error_log

   RailsEnv staging
   PassengerEnabled on
  <Directory /var/www/vhost/rails_dir/stg/public>
    Options FollowSymLinks
    AllowOverride All
    Require all granted
    AddDefaultCharset UTF-8
  </Directory>
</VirtualHost>

3.2.RubyOnRails導入

■butamayoで   

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

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

rbenv install 2.6.4

rbenv global 2.6.4

source ~/.bash_profile

gem update

gem install bundler

cd /var/www/vhost/rails_dir

bundle init

bundle update

echo 'gem "rails", "5.2.3"' >> Gemfile

echo 'gem "mysql2"' >> Gemfile

gem update

gem install -v 5.2.3 rails

gem install mysql2 -v '0.5.2'

bundle update

bundle install --path vendor/bundle

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

sed -i -e '/^wagon$/a rails' ~butamayo/.bundler-exec.sh

rails new www --skip-bundle

rails new stg --skip-bundle

cd /var/www/vhost/rails_dir/www/

sed -i -e "s/^# gem 'mini_racer'/gem 'mini_racer'/" Gemfile

echo 'gem "mysql2"' >> Gemfile

gem install mysql2 -v '0.5.2'

gem install passenger

vim /var/www/vhost/rails_dir/www/config/database.yml

#以下に記述変更

default: &default
  adapter: mysql2
  encoding: utf8
  collation: utf8_general_ci  
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  reconnect: false
  timeout: 5000
  database: pro_rails
  username: root
  password: hogehoge
  host: localhost


production:
  <<: *default
  database: pro_rails
  username: root
  password: hogehoge
  host: localhost


development:
  <<: *default
  database: stg_rails
  username: root
  password: hogehoge
  host: localhost


staging:
  <<: *default
  database: stg_rails
  username: root
  password: hogehoge
  host: localhost



cp -rp /var/www/vhost/rails_dir/www/config/database.yml /var/www/vhost/rails_dir/stg/config/database.yml

cd /var/www/vhost/rails_dir/stg/

sed -i -e "s/^# gem 'mini_racer'/gem 'mini_racer'/" Gemfile

echo 'gem "mysql2"' >> Gemfile

gem install mysql2 -v '0.5.2'

gem install passenger

passenger-install-apache2-module --auto --languages=ruby

cd /var/www/vhost/rails_dir/www/

bundle update

bundle exec rails secret

#↓コピー

54eb6211e80ffd407635926eec441e29fc382d9bd9da94db8f48021db7842792093f49972778a5ad23ee7421f14e8b122184385a4eba35d2d6a767c74cf87791

echo 'export SECRET_KEY_BASE=54eb6211e80ffd407635926eec441e29fc382d9bd9da94db8f48021db7842792093f49972778a5ad23ee7421f14e8b122184385a4eba35d2d6a767c74cf87791' >> ~/.bashrc


source ~/.bashrc

cd /var/www/vhost/rails_dir/www

bundle update

bundle exec rake assets:precompile RAILS_ENV=production

cd /var/www/vhost/rails_dir/stg

bundle update

bundle exec rake assets:precompile RAILS_ENV=staging

vim /var/www/vhost/rails_dir/www/config/environments/production.rb

#23行目コメントアウトし、直下に以下文字列を追記

config.public_file_server.enabled = true


vim /var/www/vhost/rails_dir/stg/config/environments/production.rb

#23行目コメントアウトし、直下に以下文字列を追記

config.public_file_server.enabled = true


sudo service httpd restart

sudo chmod 2755 /var/www/vhost -R

3.3.動作確認

■動作テスト

cd /var/www/vhost/rails_dir/www

rails g controller test index

#hostsを設定しブラウザでアクセス
http://www.butamayo.jp/test/index



cd /var/www/vhost/rails_dir/stg

rails g controller test index

#hostsを設定しブラウザでアクセス
http://stg.butamayo.jp/test/index

4.最後に

こんな感じで設定をすればApache上でRailsを動かすことができます。Apache+Rails(+passenger)の記事があまりないため似たような場面に遭遇した人の助けになればうれしいです。

適当に環境作ったので動かないなどありましたら、コメントかTwitterでご指摘いただけると助かります(__)

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