さくらVPS(Debian Jessie)で Rails を動かすまでの苦労をまとめた
もりすけです。
最近、Rubyに手を出し始めました。絶賛勉強中です。
おもしろいですよね。
今はまだフレームワークには手を出していませんが、
いつかはウェブアプリケーションを作りたいですね。
PHP では CakePHP を使っているので、
是非 Rails を使ってみたいと思っています。
しかし、インストールの難しさは一級品らしく、
みなさん苦労されているようです。
実際、僕も苦労しましたよ。
ということで、忘れないうちに残しておこうと思います。
※Rubyはインストール済みです。
sakuraVPS:~$ ruby -v ruby 2.1.5p273 (2014-11-13) [x86_64-linux-gnu]
Rails のインストール(エラー)
Ruby をインストールすると RubyGems
も一緒にインストールされます。
Rails
は RubyGems
からインストールすることになります。
sakuraVPS:~$ sudo gem install rails
Fetching: i18n-0.7.0.gem (100%) Successfully installed i18n-0.7.0 Fetching: minitest-5.8.4.gem (100%) Successfully installed minitest-5.8.4 Fetching: thread_safe-0.3.5.gem (100%) Successfully installed thread_safe-0.3.5 Fetching: tzinfo-1.2.2.gem (100%) Successfully installed tzinfo-1.2.2 Fetching: activesupport-4.2.5.1.gem (100%) Successfully installed activesupport-4.2.5.1 Fetching: builder-3.2.2.gem (100%) Successfully installed builder-3.2.2 Fetching: erubis-2.7.0.gem (100%) Successfully installed erubis-2.7.0 Fetching: mini_portile2-2.1.0.gem (100%) Successfully installed mini_portile2-2.1.0 Fetching: nokogiri-1.6.8.rc2.gem (100%) Building native extensions. This could take a while... ERROR: Error installing rails: ERROR: Failed to build gem native extension. /usr/bin/ruby2.1 extconf.rb mkmf.rb can't find header files for ruby at /usr/lib/ruby/include/ruby.h extconf failed, exit code 1 Gem files will remain installed in /var/lib/gems/2.1.0/gems/nokogiri-1.6.8.rc2 for inspection. Results logged to /var/lib/gems/2.1.0/extensions/x86_64-linux/2.1.0/nokogiri-1.6.8.rc2/gem_make.out
さっそくエラー。
nokogiri のインストール
nokogiri
がインストールできないエラーは、サイトを参照します。
必要なものは、build-essential patch ruby-dev zlib1g-dev liblzma-dev libgmp-dev
ですね。
sakuraVPS:~$ sudo aptitude install build-essential patch ruby-dev zlib1g-dev liblzma-dev libgmp-dev
以下の新規パッケージがインストールされます: libgmp-dev libgmpxx4ldbl{a} liblzma-dev ruby-dev ruby2.1-dev{a} zlib1g-dev 更新: 0 個、新規インストール: 6 個、削除: 0 個、保留: 0 個。 2,214 k バイトのアーカイブを取得する必要があります。展開後に 7,546 k バイトのディスク領域が新たに消費されます。 先に進みますか? [Y/n/?]
さらに、libxml2-dev
libxslt1-dev
もインストールしておきます。
sakuraVPS:~$ sudo aptitude install libxml2-dev libxslt1-dev
以下の新規パッケージがインストールされます: libxml2-dev libxslt1-dev libxslt1.1{a} 更新: 0 個、新規インストール: 3 個、削除: 0 個、保留: 0 個。 1,437 k バイトのアーカイブを取得する必要があります。展開後に 5,712 k バイトのディスク領域が新たに消費されます。 先に進みますか? [Y/n/?] ...
そして、インストールした libxml2
を使って nokogiri
をインストールします。
まずは、libxml2
の 保存先を確認します。
sakuraVPS:~$ sudo find /usr/ | grep libxml/parser.h /usr/include/libxml2/libxml/parser.h
このアドレスを参照するために、オプションを付け加えます。
sakuraVPS:~$ sudo gem install nokogiri -- --with-xml2-dir=/usr/include/libxml2
Building native extensions. This could take a while... Successfully installed nokogiri-1.6.7.2 Parsing documentation for nokogiri-1.6.7.2 Installing ri documentation for nokogiri-1.6.7.2 Done installing documentation for nokogiri after 6 seconds 1 gem installed
無事にインストールできました。
再度 Rails のインストール(成功)
sakuraVPS:~$ sudo gem install rails
ログは長いので割愛します。
sakuraVPS:~$ rails -v Rails 4.2.5.1
rails
のインストールが完了しました。
Rails アプリケーションの作成
早速 Rails で動かすためのアプリケーションを作りましょう。
新しくアプリケーションスペースを作り、
必要なGemパッケージを Bundler
で インストールします。
最初はエラーが出るので、
新規作成と、パッケージインストールを別々にしてみます。
まずは、Rails アプリケーションを作成しましょう。
僕はPublic_html
の中にドメイン別のディレクトリを作っていますので、
今回も新しいドメインとしてそこにインストールします。
(example.comは自分の名前に変えてね)
sakuraVPS:~$ cd public_html sakuraVPS:~/public_html$ rails new example.com --skip-bundle
create create README.rdoc create Rakefile create config.ru create .gitignore create Gemfile create app ... create vendor/assets/javascripts/.keep create vendor/assets/stylesheets create vendor/assets/stylesheets/.keep
Bundler
を実行させずにアプリケーションを作成しました。
Bundler で Gemパッケージのインストール
アプリケーションディレクトリに移動して、
Bundler
で Gemパッケージをインストールします。
sakuraVPS:~/public_html$ cd example.com sakuraVPS:~/public_html/example.com$ bundle install
Installing sqlite3 1.3.11 with native extensions Gem::Ext::BuildError: ERROR: Failed to build gem native extension. /usr/bin/ruby2.1 extconf.rb checking for sqlite3.h... no sqlite3.h is missing. Try 'port install sqlite3 +universal', 'yum install sqlite-devel' or 'apt-get install libsqlite3-dev' and check your shared library search path (the location where your sqlite3 shared library is located). *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options. Provided configuration options: --with-opt-dir --without-opt-dir --with-opt-include --without-opt-include=${opt-dir}/include --with-opt-lib --without-opt-lib=${opt-dir}/lib --with-make-prog --without-make-prog --srcdir=. --curdir --ruby=/usr/bin/ruby2.1 --with-sqlite3-dir --without-sqlite3-dir --with-sqlite3-include --without-sqlite3-include=${sqlite3-dir}/include --with-sqlite3-lib --without-sqlite3-lib=${sqlite3-dir}/lib extconf failed, exit code 1 Gem files will remain installed in /tmp/bundler20160208-31037-1wluagksqlite3-1.3.11/gems/sqlite3-1.3.11 for inspection. Results logged to /tmp/bundler20160208-31037-1wluagksqlite3-1.3.11/extensions/x86_64-linux/2.1.0/sqlite3-1.3.11/gem_make.out An error occurred while installing sqlite3 (1.3.11), and Bundler cannot continue. Make sure that `gem install sqlite3 -v '1.3.11'` succeeds before bundling.
sqlite3
がエラーになりました。
libsqlite3-dev
をいれてねと書いてあるので、
インストールしましょう。
sakuraVPS:~/public_html/example.com$ sudo aptitude install libsqlite3-dev
再度実行します。
sakuraVPS:~/public_html/example.com$ bundle install
Bundle complete! 12 Gemfile dependencies, 54 gems now installed. Use `bundle show [gemname]` to see where a bundled gem is installed.
上手くインストールできました。
Passenger を使って apache2 でアプリを起動
Passenger
を使って apache2 から Rubyアプリを起動するようにします。
RubyGems
で Passenger
をインストールします。
sakuraVPS:~$ sudo gem install passenger
Fetching: passenger-5.0.24.gem (100%) Building native extensions. This could take a while... Successfully installed passenger-5.0.24 Parsing documentation for passenger-5.0.24 unable to convert "\x88" from ASCII-8BIT to UTF-8 for src/cxx_supportlib/vendor-modified/boost/thread/future.hpp, skipping unable to convert "\x97" from ASCII-8BIT to UTF-8 for src/cxx_supportlib/vendor-modified/boost/utility/declval.hpp, skipping Installing ri documentation for passenger-5.0.24 Done installing documentation for passenger after 209 seconds 1 gem installed
passenger-install-apache2-module
を実行します。
sakuraVPS:~$ sudo passenger-install-apache2-module
Installation instructions for required software * To install Curl development headers with SSL support: Please run apt-get install libcurl4-openssl-dev or libcurl4-gnutls-dev, whichever you prefer. * 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 If the aforementioned instructions didn't solve your problem, then please take a look at our documentation for troubleshooting tips: https://www.phusionpassenger.com/library/install/apache/ https://www.phusionpassenger.com/library/admin/apache/troubleshooting/
何をインストールするか教えてくれました。
sakuraVPS:~$ sudo aptitude install libcurl4-openssl-dev apache2-threaded-dev libapr1-dev libaprutil1-dev
注意: 仮想パッケージ "apache2-threaded-dev" の代わりに "apache2-dev" を選択します 以下の新規パッケージがインストールされます: apache2-dev autopoint{a} debhelper{a} gettext{a} intltool-debian{a} libapr1-dev libaprutil1-dev libasprintf-dev{a} libcroco3{a} libcurl4-openssl-dev libexpat1-dev{a} libgettextpo-dev{a} libgettextpo0{a} libglib2.0-0{a} libglib2.0-data{a} libldap2-dev{a} libmail-sendmail-perl{a} libsctp-dev{a} libsctp1{a} libsys-hostname-long-perl{a} libunistring0{a} lksctp-tools{a} po-debconf{a} shared-mime-info{a} xdg-user-dirs{a} 更新: 0 個、新規インストール: 25 個、削除: 0 個、保留: 0 個。 11.0 M バイトのアーカイブを取得する必要があります。展開後に 48.3 M バイトのディスク領域が新たに消費されます。 先に進みますか? [Y/n/?]
インストールが終了したら、
passenger-install-apache2-module
をもう一度実行します。
sakuraVPS:~$ sudo passenger-install-apache2-module
長いログの後に、下記のように表示されます。
-------------------------------------------- Almost there! Please edit your Apache configuration file, and add these lines: LoadModule passenger_module /var/lib/gems/2.1.0/gems/passenger-5.0.24/buildout/apache2/mod_passenger.so <IfModule mod_passenger.c> PassengerRoot /var/lib/gems/2.1.0/gems/passenger-5.0.24 PassengerDefaultRuby /usr/bin/ruby2.1 </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 when you are done editing. --------------------------------------------
このままで、別画面で apache2.conf
に追加します。
sakuraVPS:~$ sudo vim /etc/apache2/apache2.conf
/etc/apache2/apache2.conf --- LoadModule passenger_module /var/lib/gems/2.1.0/gems/passenger-5.0.24/buildout/apache2/mod_passenger.so <IfModule mod_passenger.c> PassengerRoot /var/lib/gems/2.1.0/gems/passenger-5.0.24 PassengerDefaultRuby /usr/bin/ruby2.1 </IfModule>
そして apache2
の再起動をします。
sakuraVPS:~$ sudo /etc/init.d/apache2 restart
完了したら、元の画面に戻り ENTER
を押して、作業を進めます。
-------------------------------------------- Validating installation... * Checking whether this Passenger install is in PATH... ✓ * Checking whether there are no other Passenger installations... ✓ * Checking whether Apache is installed... ✓ * Checking whether the Passenger module is correctly configured in Apache... ✓ Everything looks good. :-)
インストールが完了しました。
次に今回作成したアプリ用のコンフィグファイルを作ります。
sakuraVPS:~$ cd /etc/apache2/sites-available sakuraVPS:~/etc/apache2/sites-available$ sudo vim example.com.conf
example.com.conf --- # バーチャルホストのデフォルトで80ポートを使用 <VirtualHost *:80> # ドメイン名 ServerName example.com # 管理者のメールアドレス ServerAdmin support@example.com # ドキュメントルートまでのパス DocumentRoot /home/管理者/public_html/example.com/public/ # 開発モードで動かす RailsEnv development # 制御するディレクトリ <Directory /home/管理者/public_html/example.com/public/> # オプション Options FollowSymLinks # .htaccessの許可 AllowOverride all # アクセス許可 Require all granted </Directory> # エラーログ ErrorLog ${APACHE_LOG_DIR}/error_example.log # エラーログを記録するレベル LogLevel warn # アクセスログ CustomLog ${APACHE_LOG_DIR}/access_example.log combined </VirtualHost>
サイトコンフィグファイルを有効にします。
sakuraVPS:~$ sudo a2ensite example.com.conf
最後に apache2
を再起動します。
sakuraVPS:~$ sudo /etc/init.d/apache2 restart
アプリの表示
きちんと設定されているかサイトにアクセスしてみます。
http://example.com
どうも失敗していますね。
nodejs
がインストールされていないので、
このようなエラーが出るようです。
sakuraVPS:~$ sudo aptitude install nodejs
以下の新規パッケージがインストールされます: libc-ares2{a} libv8-3.14.5{a} nodejs 更新: 0 個、新規インストール: 3 個、削除: 0 個、保留: 0 個。 1,994 k バイトのアーカイブを取得する必要があります。展開後に 7,491 k バイトのディスク領域が新たに消費されます。 先に進みますか? [Y/n/?]
更新してみると、
ウェルカム画面が表示されました!
最後に
これでやっとこのサーバで Rails のアプリケーションが公開できるようになりました。
新しくアプリを追加する場合は、
sakuraVPS:~$ rails new ****
でエラーなくインストールできるでしょう。
さて、Ruby の勉強でもしてきます。
ありがとうございました。