Linux(CentOS)でオープンソース家計簿「小槌」を動かす

Software ソフトウェア
この記事は約8分で読めます。

※当ブログではアフィリエイト広告を利用しています。

前回までの記事で、Linux(CentOS)でGit、Ruby on Rails、PostgreSQLを使用する準備が出来ました。

続いて、Linux(CentOS)上で、オープンソースオンライン家計簿「小槌」を動かします。

オープンソースオンライン家計簿「小槌」についてはこちら⇒Ruby on Railsのオープンソースオンライン家計簿「小槌」のPostgreSQL対応

なお、今回のバージョンは以下です。

  • CentOS-6.4-i386
  • Ruby 2.0.0-p195
  • Rails 3.2.13
  • PostgreSQL 9.2.4

リポジトリからクローン

まずは、CentOS上に作成したGitの共有リポジトリからソースを「クローン(clone)」する。

クローン後に、ディレクトリごと所有者とグループをユーザー「kozuchi」へ変更した。

なお、「chown」コマンド、「chgrp」コマンドのディレクトリごとのオプションは「-R」。

#スイッチユーザー
$ su root

#共有リポジトリからクローン
$ git clone /rep/rails/kozuchi/ /var/www/rails/kozuchi

#ディレクトリごと所有者変更
$ chown kozuchi -R /var/www/rails/kozuchi

#ディレクトリごとグループ変更
$ chgrp kozuchi -R /var/www/rails/kozuchi

iptablesで3000番ポートを開放

続いて、ポートの開放。

Railsは3000番ポートなので、iptablesで3000番ポートを開放してやる。

なお、設定は同じファイル内のSSHの記載を参考にし、Rails分を追記した。

編集後は、iptablesの再起動が必要。

#iptablesの設定ファイル編集
$ vi /etc/sysconfig/iptables

# SSH
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
# Rails
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3000 -j ACCEPT

#iptablesの再起動
$ /etc/rc.d/init.d/iptables restart

bundle install

スイッチユーザーし、ディレクトリを移動し、「bundle install」コマンドを実行する。

なお、環境依存のもの?があるので、事前に「Gemfile.lock」ファイルを削除しておく。

#スイッチユーザー
$ su kozuchi

#ディレクトリ移動
$ cd /var/www/rails/kozuchi

#「Gemfile.lock」ファイル削除
$ rm Gemfile.lock

#「bundle install」コマンド実行
$ bundle install

ちなみに、俺の環境の場合は「Gemfile.lock」ファイルを削除しておかないと、「nokogiri」でエラーになった。

$ bundle install
…
(省略)
…
Using bundler (1.3.5)
Installing nokogiri (1.5.9)
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /usr/local/bin/ruby extconf.rb
checking for libxml/parser.h... no
-----
libxml2 is missing.  please visit http://nokogiri.org/tutorials/installing_nokogiri.html for help with installing dependencies.
-----
*** 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.

環境変数設定

以下のデータベースユーザーなどは環境変数を使用しているので、設定してやる必要がある。

ちなみに環境変数を使用しているファイルは、「./config/database.yml」と「./config/initializers/hosting.rb」の2つ。

なお、環境変数はそのbashのセッション内で有効なため、スイッチユーザーから「exit」コマンドでログアウトしたら、再設定してやる必要があるよう。

#環境変数設定
$ export DB_DEV_NAME=開発環境データベース
$ export DB_DEV_USER=開発環境ユーザー
$ export DB_DEV_PASS=開発環境パスワード
$ export DB_TEST_NAME=テスト環境データベース
$ export DB_TEST_USER=テスト環境ユーザー
$ export DB_TEST_PASS=テスト環境パスワード
$ export DB_PRODUCT_NAME=本番環境データベース
$ export DB_PRODUCT_USER=本番環境ユーザー
$ export DB_PRODUCT_PASS=本番環境パスワード
$ export SUPPORT_EMAIL_ADDRESS=メールアドレス
$ export ROOT_URL=サーバ
$ export LOGIN_ENGINE_SALT=ログインエンジン

#環境変数一覧表示
$ printenv

#環境変数の値表示
$ printenv 環境変数名

データベース作成

以下のコマンドでデータベースを作成する。

$ rake db:create
$ rake db:migrate

nodejsのインストール

以下のコマンドでアプリケーションを起動してみると、JavaScriptランタイムエラーとなる。

「nodejs」とやらをインストールする必要があるようなので、インストール。

また、「nodejs」のインストールには「g++」コンパイラーが必要なので、インストール。

#アプリケーション起動でエラー
$ rails s
/usr/local/lib/ruby/gems/2.0.0/gems/execjs-1.4.0/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)

#「nodejs」のダウンロード
$ wget http://nodejs.org/dist/v0.10.11/node-v0.10.11.tar.gz

#アーカイブ展開
$ tar xvzf node-v0.10.11.tar.gz

#ディレクトリ移動
$ cd node-v0.10.11

#「configure」を実行でエラー
$ ./configure

which: no g++ in (/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/[ユーザー]/bin)
gyp: Call to '(echo | $(echo ${CXX_host:-$(which g++)}) -m32 -E - > /dev/null 2>&1) && echo "-m32" || true' returned exit status 0. while loading dependencies of /home/[ユーザー]/node-v0.10.11/node.gyp while trying to load /home/[ユーザー]/node-v0.10.11/node.gyp
Error running GYP

#g++のインストール
$ yum install gcc-c++

#「configure」を実行し、Makefile生成
$ ./configure

#makeコマンドでコンパイル
$ make

#makeコマンドでインストール
$ make install

アプリケーションの起動

以下のコマンドでアプリケーションを起動する。

$ rails s
=> Booting WEBrick
=> Rails 3.2.12 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2013-06-26 12:08:15] INFO  WEBrick 1.3.1
[2013-06-26 12:08:15] INFO  ruby 2.0.0 (2013-05-14) [i686-linux]
[2013-06-26 12:08:15] INFO  WEBrick::HTTPServer#start: pid=2244 port=3000

以上で終了!

あー長かった。

参考サイト

Railsの起動時に、「Could not find a JavaScript runtime.」と出た場合の対処方法 | 9ensanのLifeHack

CentOSへのg++の追加方法 – Reinvention of the Wheel

ソフトウェア
\よかったらシェアしてね!/
えふめん

大阪在住、30代。
業務系SE・社内SE。

PCトラブルの調査、自作デスクトップPCのこと、PC周辺機器のレビューなどの記事を書いています。

えふめんをフォローする
この記事が気に入ったら
いいね!しよう
最新情報をお届けします。

コメント

タイトルとURLをコピーしました