Railsのインストールの仕方(apache2.4 & mysql & ubuntu系)

ゲームアプリで使うランキング用にRailsでランキングシステムを作ろうと思います。今回はRailsをさくらVPSにインストールしてとりあえず動くようにします。

Get Ruby on Rails in no timeを参考にインストールしていきます。

Ruby2.1以上をインストールする

とりあえず自分のサーバーのrubyのバージョンを調べています。

ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]

古いです。新しいRubyをrbenvを使ってインストールします。

git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
Cloning into '/home/hoge/.rbenv'...
error: Couldn't resolve host 'github.com' while accessing https://github.com/sstephenson/rbenv.git/info/refs
fatal: HTTP request failed

github.comが見つからないと・・。

他サイトを試してみる。

ping yahoo.com
ping: unknown host yahoo.com

どうも名前解決ができないようです。

直前にサーバーのOSをアップグレードした影響で名前解決ができなくなっていました。

sudo /etc/init.d/networking restart
 * Running /etc/init.d/networking restart is deprecated because it may not enable again some interfaces
 * Reconfiguring network interfaces...                                                                                RTNETLINK answers: File exists
Failed to bring up eth0.

よくわからないエラーが出ている。(上記のエラーは名前解決とは関係ない模様です)

わりと時間をかけて、ようやく下記の方法で問題を解決しました。

sudo vim /etc/resolvconf/resolv.conf.d/base

search sakura.ne.jp
nameserver 210.188.224.11
//保存して終了

sudo resolvconf -u

nslookup goo.ne.jp
Server:		210.188.224.11
Address:	210.188.224.11#53

Non-authoritative answer:
Name:	goo.ne.jp
Address: 153.254.147.65

参考:さくらのVPS Ubuntu 10.04から12.04 LTSヘバージョンアップする

rbenvをインストールする。

git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc

Ubuntu以外はbashrc→bash_profileに変更する。

これでrbenvのインストールは完了です。つぎにruby-buildをインストールします。

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

これだけでruby-buildのインストールは完了です。

次にrubyをインストールします。

rbenv install -l
rbenv install 2.2.0

./tool/rbinstall.rb:723:in `chmod': Operation not permitted @ chmod_internal - /home/hoge/.rvm/gems/ruby-1.9.2-p290/gems/passenger-3.0.8/ext/ruby/ruby-1.9.2-x86-linux/ (Errno::EPERM)

現時点で一番新しそうで安定してそうなruby2.2.0をインストールします。

いつの間にかrvmがインストールされていました。削除してrubyをインストールします。

rvm implode
rbenv install 2.2.0
rbenv global 2.2.0
ruby -v
ruby 2.2.0p0 (2014-12-25 revision 49005) [i686-linux]

ruby2.2.0のインストールが完了しました。

次にRailsをインストールします。

gem install rails
rails -v
Rails 4.2.0

時間がかかりましたがRailsのインストールが完了しました。

せっかくなのでapache2.4もインストールします。apacheの設定ファイルも削除されるのでapache2.confやsites-availableフォルダ等は別に保存しておいた方がいいです。

apache2 -v
Server version: Apache/2.2.22 (Ubuntu)

sudo apt-get remove apache2
sudo apt-get purge apache2 apache2-utils apache2.2-bin apache2-common
sudo rm -r /etc/apache2/
sudo apt-get autoremove

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:ondrej/php5-5.6
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install apache2

apache2 -v
Server version: Apache/2.4.12 (Ubuntu)
Server built:   Feb  4 2015 14:21:55

最新っぽいapache2.4をインストールすることが出来ました。

Railsとは関係ありませんが、このままではwordpressが使えないのでphp5のインストールとモジュールの追加と設定する。

sudo apt-get install php5
sudo a2enmod rewrite

sudo vim /etc/apache2/sites-available/000-default.conf

//wordpress用の設定
<Directory /home/username/wordpress/>
        allow from all
        Require all granted
        Options -Indexes
        AllowOverride all
</Directory>

//保存して終了

sudo service apache2 restart

これでwordpressもいつもどおり動くようになる。

wordpressが真っ白になった場合はwordpressをdebugモードにして原因を探します。

vim wp-config.php

define('WP_DEBUG', true);

gemをインストールしてpassengerをインストールする。

wget http://production.cf.rubygems.org/rubygems/rubygems-2.4.5.zip
unzip rubygems-2.4.5.zip
cd rubygems-2.4.5
ruby setup.rb

gem install passenger
passenger-install-apache2-module
//Rubyを選択
//Enterを押すと不足しているものが表示される(下記は私の場合)

Installation instructions for required software

 * To install Apache 2 development headers:
   Please install it with apt-get install apache2-threaded-dev

 * To install Apache Portable Runtime (APR) development headers:
   Please install it with apt-get install libapr1-dev

 * To install Apache Portable Runtime Utility (APU) development headers:
   Please install it with apt-get install libaprutil1-dev

sudo apt-get install apache2-threaded-dev
E: 問題を解決することができません。壊れた変更禁止パッケージがあります。

失敗した。

違う方法を試してみる
Installing or upgrading on Debian or Ubuntu

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7

sudo apt-get install apt-transport-https ca-certificates
sudo vim /etc/apt/sources.list.d/passenger.list

deb https://oss-binaries.phusionpassenger.com/apt/passenger precise main
//保存して終了 上記はUbuntu12.04の場合、たのOSは違うURLになる

sudo chown root: /etc/apt/sources.list.d/passenger.list
sudo chmod 600 /etc/apt/sources.list.d/passenger.list
sudo apt-get update

sudo apt-get install libapache2-mod-passenger
sudo a2enmod passenger
sudo service apache2 restart
 * Restarting web server apache2                                                                                                   [fail] 
 * The apache2 configtest failed.
Output of config test was:
apache2: Syntax error on line 140 of /etc/apache2/apache2.conf: Syntax error on line 2 of /etc/apache2/mods-enabled/passenger.load: Cannot load /usr/lib/apache2/modules/mod_passenger.so into server: /usr/lib/apache2/modules/mod_passenger.so: undefined symbol: unixd_config
Action 'configtest' failed.

sudo a2dismod passenger
service apache2 restart

失敗した。

正解が見つかった。
apache2-devを唱えることでpassenger-install-apache2-moduleが通るようになる。

sudo apt-get install apache2-dev
passenger-install-apache2-module

//ごちゃごちゃとインストールが始まる
Please edit your Apache configuration file, and add these lines:

   LoadModule passenger_module /home/username/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/passenger-4.0.59/buildout/apache2/mod_passenger.so
   <IfModule mod_passenger.c>
     PassengerRoot /home/username/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/passenger-4.0.59
     PassengerDefaultRuby /home/username/.rbenv/versions/2.2.0/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!

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>

apache2.confにpassengerの設定を追記する。

sudo vim /etc/apache2/apache2.conf

LoadModule passenger_module /home/username/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/passenger-4.0.59/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
     PassengerRoot /home/username/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/passenger-4.0.59
     PassengerDefaultRuby /home/username/.rbenv/versions/2.2.0/bin/ruby
</IfModule>

Railsアプリ、rankingを作成する。

rails new ranking -d mysql
プログラム 'rails' はまだインストールされていません。  次のように入力することでインストールできます:
sudo apt-get install rails

はて? gemでrailsをインストールしたはずなのにrailsが見つからない。

もう一度railsをインストールしてみる。

gem install rails

rails -v
Rails 4.2.0

rails new ranking -d mysql

Railsアプリの作成が完了した。

phpMyAdminとdigestの再設定をする。

sudo vim /etc/apache2/apache2.conf

Include /etc/phpmyadmin/apache.conf
//保存して終了

sudo mkdir /etc/apache2/auth
cd /etc/apache2/auth
htdigest -c digest_passwd AuthPage username

sudo a2enmod auth_digest
sudo service apache2 restart

phpMyAdminでデータベースの設定をする。rankingというデータベースを作成して、ranking/config/database.ymlにデータベースとパスワードを設定する。

apacheにrankingの設定を追加する。

sudo vim /etc/apache2/sites-available/000-default.conf

RackBaseURI /ranking
RailsEnv development
#RailsEnv production
<Directory /home/username/ranking/public>
    AllowOverride all
    Options -MultiViews
    Require all granted
</Directory>
//保存して終了

sudo service apache2 restart

http://hoge.com/rankingみたいな形になる。

シンボリックリンクを作る。

cd /home/username/hoge
sudo ln -s ../ranking/public ranking

これでhttp://hoge.com/rankingへアクセスしてみる。

cannot load such file — readline (LoadError)

エラーが出る。エラーの修正方法は下記になる。

sudo apt-get install libreadline-dev

cd /home/username/ranking
vim Gemfile
gem 'rb-readline'
//保存して終了

bundle update

これでエラーが出なくなってhttp://hoge.com/rankingへアクセスできるようになる。

やたら長くなってしまいましたが、これでapache2.4 & mysql & ubuntuでRailsが動くようになりました。

コメントを残す

メールアドレスが公開されることはありません。

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください