61
61

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.

VPS上のCentOS 7にRails環境を構築する

Last updated at Posted at 2014-11-13

RailsアプリをCentOSのApache上でサブディレクトリ(hogehoge.com/piyo)で動作させる方法についてまとめてみました。
[環境]CentOS7、Ruby2.1.4、Rails4.1.7、rbenv0.4.0-129-g7e0e85b、Passenger4.0.531
##必要なソフトウェアのインストール

$ sudo yum -y update && sudo yum -y install git sqlite sqlite-devel httpd-devel curl-devel apr-devel apr-util-devel libffi-devel openssh
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: www.ftp.ne.jp
 * extras: www.ftp.ne.jp
 * updates: www.ftp.ne.jp
~~略~~
完了しました!

##rbenv、ruby-build、Rubyのインストール

$ cd /usr/local
$ sudo git clone git://github.com/sstephenson/rbenv.git
Cloning into 'rbenv'...
remote: Counting objects: 2002, done.
remote: Total 2002 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (2002/2002), 318.88 KiB | 186.00 KiB/s, done.
Resolving deltas: 100% (1249/1249), done.
$ sudo mkdir rbenv/plugins && cd rbenv/plugins
$ sudo git clone git://github.com/sstephenson/ruby-build.git
Cloning into 'ruby-build'...
remote: Counting objects: 4031, done.
remote: Total 4031 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (4031/4031), 701.51 KiB | 267.00 KiB/s, done.
Resolving deltas: 100% (2043/2043), done.


$ sudo visudo -f /etc/sudoers.d/rbenv

#↓追加
Defaults !secure_path
Defaults env_keep += "PATH RBENV_ROOT"

$ sudo vim /etc/profile.d/rbenv.sh

#↓追加
export RBENV_ROOT="/usr/local/rbenv"
export PATH="${RBENV_ROOT}/bin:${PATH}"
eval "$(rbenv init -)"

#ここまでできたら一度ログアウトし再度ログインしてください。

$ sudo chmod -R 777 /usr/local/rbenv
$ rbenv install 2.1.4 #他のバージョンでも可
Downloading ruby-2.1.4.tar.gz...
-> http://
Installing ruby-2.1.4...


$ rbenv global 2.1.4 #他のバージョンをインストールした場合はそのバージョンを指定する
$ rbenv rehash

##必要なgemのインストール

$ gem install rails passenger therubyracer sqlite3 bundler
Fetching: thread_safe-0.3.4.gem (100%)
Successfully installed thread_safe-0.3.4
Fetching: tzinfo-1.2.2.gem (100%)
Successfully installed tzinfo-1.2.2
~~略~~
33 gems installed

##passengerモジュールをApacheにインストール

$ sudo passenger-install-apache2-module
/usr/local/rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/passenger-4.0.53/bin/passenger-install-apache2-module:42: warning: Insecure world writable dir /usr/local/rbenv/versions/2.1.4/bin in PATH, mode 040777
Welcome to the Phusion Passenger Apache 2 module installer, v4.0.53.

This installer will guide you through the entire installation process. It
shouldn't take more than 3 minutes in total.

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.

Don't worry if anything goes wrong. This installer will advise you on how to
solve any problems.

Press Enter to continue, or Ctrl-C to abort.
#enterを押す

--------------------------------------------

Which languages are you interested in?

Use <space> to select.
If the menu doesn't display correctly, press '!'

 ‣ ⬢  Ruby
   ⬢  Python
   ⬡  Node.js
   ⬡  Meteor
#enterを押す

--------------------------------------------

Checking for required software...

 * Checking for C compiler...
      Found: yes
      Location: /usr/bin/cc
 * Checking for C++ compiler...
      Found: yes
      Location: /usr/bin/c++
 * Checking for Curl development headers with SSL support...
~~略~~

--------------------------------------------
Almost there!

Please edit your Apache configuration file, and add these lines:

   LoadModule passenger_module /usr/local/rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/passenger-4.0.53/buildout/apache2/mod_passenger.so
   <IfModule mod_passenger.c>
     PassengerRoot /usr/local/rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/passenger-4.0.53
     PassengerDefaultRuby /usr/local/rbenv/versions/2.1.4/bin/ruby
   </IfModule>

After you restart Apache, you are ready to deploy any number of web
applications on Apache, with a minimum amount of configuration!

Press ENTER to continue.
#enterを押す


--------------------------------------------

Deploying a web application: an example

Suppose you have a web application in /somewhere. Add a virtual host to your
Apache configuration file and set its DocumentRoot to /somewhere/public:

   <VirtualHost *:80>
      ServerName www.yourhost.com
      # !!! Be sure to point DocumentRoot to 'public'!
      DocumentRoot /somewhere/public    
      <Directory /somewhere/public>
         # This relaxes Apache security settings.
         AllowOverride all
         # MultiViews must be turned off.
         Options -MultiViews
         # Uncomment this if you're on Apache >= 2.4:
         #Require all granted
      </Directory>
   </VirtualHost>

And that's it! You may also want to check the Users Guide for security and
optimization tips, troubleshooting and other useful information:

  /usr/local/rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/passenger-4.0.53/doc/Users guide Apache.html
  https://www.phusionpassenger.com/documentation/Users%20guide%20Apache.html

Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) :-)
https://www.phusionpassenger.com

Phusion Passenger is a trademark of Hongli Lai & Ninh Bui.

##Apacheへの設定追加

$ passenger-install-apache2-module --snippet > ~/passenger.conf
$ sudo mv ~/passenger.conf /etc/httpd/conf.d/
$ sudo vim /etc/httpd/conf.d/rails.conf

NameVirtualHost *:80
<VirtualHost *:80>
    PassengerEnabled On
    RailsEnv development
    RailsBaseURI /hellorails
</VirtualHost>
#↑追加

$ sudo systemctl restart httpd

##テスト用のRailsアプリの作成

$ sudo mkdir /var/www/app && sudo chmod 777 /var/www/app && cd /var/www/app
$ rails new hellorails
      create  
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
~~略~~
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
         run  bundle exec spring binstub --all
* bin/rake: spring inserted
* bin/rails: spring inserted

$ cd hellorails
$ vim Gemfile

# gem 'therubyracer',  platforms: :ruby
#↓javascriptランタイムを有効にする
gem 'therubyracer',  platforms: :ruby

$ cd /var/www/html
$ ln -s /var/www/app/hellorails/public hellorails

##SELinuxの無効化

$ sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      28
$ sudo vim /etc/selinux/config 
#↑/etc/sysconfig/selinux ではなく

SELINUX=enforcing
#↓デフォルトの起動モードを変更
SELINUX=permissive

$ setenforce 0

http://[ドメイン名]/hellorails
でテストアプリへアクセスできるはずです。

##参考リンク

rbenvをシステムワイドにインストールする » つくば日記(仮)
rbenv+ruby-buildをインストールする手順 (CentOS/RedHat) | WEB ARCH LABO
CentOSにPassenger/Ruby2.0/Rails4.0をインストールしてみる(rbenv版) - yk5656 diary
Linux(CentOS)のApache+PassengerでRuby on Railsを動かす
Apache 2.2 に Phusion Passenger をプラグインする手順 (CentOS/RedHat) | WEB ARCH LABO
Redmine 2.5をCentOS 6.5にインストールする手順 | Redmine.JP Blog
5.4. SELINUX の有効化および無効化

61
61
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?