はじめに
今回、用いた/インストールした環境は以下になります。
OS: Ubuntu 14.04
Ruby: 2.2.3
Redmine: 3.0.4
IaaS: azure vm standard a0
準備
アップデートと必要なモジュールをインストールします。
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install git vim build-essential libssl-dev libreadline-dev
MySQLインストール
$ sudo apt-get install mysql-server
$ sudo apt-get install libmysql++-dev
mysql-serverインストール時に、rootのパスワードをきかれるので、入力します。
ImageMagickインストール
$ sudo apt-get install imagemagick libmagickcore-dev libmagick++-dev libmagickwand-dev
Apacheインストール
$ sudo apt-get install apache2
Rubyインストール
passengerとの連携の関係で、ユーザ環境ではなくシステムワイドにrbenvをインストールします。
rbenvのインストールについては、基本的にここを参考に設定しました。
sudoers の設定
$ sudo visudo -f /etc/sudoers.d/00_base
自動的にエディタがたちがるので、以下を記入します。
Defaults !secure_path
Defaults env_keep += "PATH RBENV_ROOT"
$ export RBENV_ROOT=/usr/local/rbenv
$ export PATH=${RBENV_ROOT}/bin:${PATH}
rbenvをインストール
$ sudo git clone git://github.com/sstephenson/rbenv.git ${RBENV_ROOT}
$ sudo git clone git://github.com/sstephenson/ruby-build.git ${RBENV_ROOT}/plugins/ruby-build
.profile の末尾に以下を追記
bash -c 'cat <<\__EOT__ >> ~/.profile
export RBENV_ROOT="/usr/local/rbenv"
export PATH="${RBENV_ROOT}/bin:${PATH}"
eval "$(rbenv init -)"
__EOT__'
シェルの再起動
$ exec ${SHELL} -l
ruby 2.2.3をインストール
$ sudo rbenv install 2.2.3
$ sudo rbenv rehash
$ sudo rbenv global 2.2.3
Redmineインストール
若干記事が古いようですが、公式サイトの手順を参考に進めます。
パッケージ取得
公式サイトからリソースパッケージを入手し、/var/www配下に展開します。
$ wget http://www.redmine.org/releases/redmine-3.0.4.tar.gz
$ sudo tar zxvf redmine-3.0.4.tar.gz -C /var/www
$ export RAILS_DIR=/var/www/redmine-3.0.4
データベース作成/設定
$ mysql -u root -p
mysql > create database redmine character set utf8;
mysql > create user 'redmine'@'localhost' identified by 'password';
mysql > grant all privileges on redmine.* to 'redmine'@'localhost';
$ sudo cp ${RAILS_DIR}/config/database.yml.example ${RAILS_DIR}/config/database.yml
$ sudo vim ${RAILS_DIR}/config/database.yml
usernameとpasswordを変更
production:
adapter: mysql2
database: redmine
host: localhost
username: redmine
password: password
bundleインストール
$ sudo gem install bundler
$ cd ${RAILS_DIR}
$ bundle install --without development test
セッションストア秘密鍵を生成
$ rake generate_secret_token
データベース上にテーブルとデフォルトデータを作成
$ RAILS_ENV=production rake db:migrate
$ RAILS_ENV=production rake redmine:load_default_data
デフォルトデータ作成時に、言語を聞かれるので、jaを選択します。
パーミッション設定
sudo chown -R www-data:www-data files log tmp public/plugin_assets
sudo chmod -R 755 files log tmp public/plugin_assets
WEBrickによるwebサーバ起動
今回はAzureのVMを使いました。事前にポートを開けておきます。
AzureでのRailsの動かし方は、公式ドキュメントに詳しく書かれています。
$ sudo ruby bin/rails server webrick -e production -b 0.0.0.0 -p 3000
http://redmine20150918.cloudapp.net/ にアクセスすると、Redmineの画面が確認できます。
「redmine20150918」はVMのホスト名(DNS名)です。
デフォルトのログイン情報は、以下になります。
login: admin
password: admin
Passengerインストール
gemでpassengerをインスールします。
$ sudo gem install passenger
passenger-install-apache2-moduleコマンドでapache2のモジュールのインスールを行います。
$ sudo passenger-install-apache2-module
対話形式で進んでいきます。
必要なモジュールのチェックがはしり、足りないものがあるとインスールしろと言われます。ご丁寧にインスールコマンドも教えてくれるので、言われたとおりにインスールします。
$ sudo apt-get install libcurl4-openssl-dev apache2-threaded-dev libapr1-dev libaprutil1-dev
再度、passenger-install-apache2-moduleコマンドを実行します。
$ sudo passenger-install-apache2-module
必要なモジュールのチェックはOKでしたが、メモリが少ないけど大丈夫?って言われ、このまま進めることもできるけど、仮想メモリをつくったほうがいいいんじゃない?とアドバイスをいただいたので、仮想メモリを作成します。
これも指示されたコマンド通りに仮想メモリを作成します。
$ sudo dd if=/dev/zero of=/swap bs=1M count=1024
$ sudo mkswap /swap
$ sudo swapon /swap
ちなみに、今回は冒頭で書いたようにAzureのA0を使っています。メモリは0.75GBなので、2GBくらいはあったほうがいいいようです。
再び、passenger-install-apache2-moduleコマンドを実行します。
チェックが全部問題なければ、インスールがはじまります。結構時間がかかります。
ログは以下のような感じです。
$ sudo passenger-install-apache2-module
Welcome to the Phusion Passenger Apache 2 module installer, v5.0.18.
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.
--------------------------------------------
Which languages are you interested in?
Use <space> to select.
If the menu doesn't display correctly, press '!'
‣ ⬢ Ruby
⬢ Python
⬡ Node.js
⬡ Meteor
--------------------------------------------
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...
Found: yes
curl-config location: /usr/bin/curl-config
Header location: /usr/include/curl/curl.h
Version: libcurl 7.35.0
Usable: yes
Supports SSL: yes
* Checking for OpenSSL development headers...
Found: yes
Location: /usr/include/openssl/ssl.h
* Checking for Zlib development headers...
Found: yes
Location: /usr/include/zlib.h
* Checking for Apache 2...
Found: yes
Location of httpd: /usr/sbin/apache2
Apache version: 2.4.7
* Checking for Apache 2 development headers...
Found: yes
Location of apxs2: /usr/bin/apxs2
* Checking for Rake (associated with /usr/local/rbenv/versions/2.2.3/bin/ruby)...
Found: yes
Location: /usr/local/rbenv/versions/2.2.3/bin/ruby /usr/local/rbenv/versions/2.2.3/bin/rake
* Checking for OpenSSL support for Ruby...
Found: yes
* Checking for RubyGems...
Found: yes
* Checking for Ruby development headers...
Found: yes
Location: /usr/local/rbenv/versions/2.2.3/include/ruby-2.2.0/ruby.h
* Checking for rack...
Found: yes
* Checking for Apache Portable Runtime (APR) development headers...
Found: yes
Location: /usr/bin/apr-1-config
Version: 1.5.1
* Checking for Apache Portable Runtime Utility (APU) development headers...
Found: yes
Location: /usr/bin/apu-1-config
Version: 1.5.3
--------------------------------------------
Checking whether there are multiple Apache installations...
Only a single installation detected. This is good.
--------------------------------------------
Compiling and installing Apache 2 module...
(略)
--------------------------------------------
Almost there!
Please edit your Apache configuration file, and add these lines:
LoadModule passenger_module /usr/local/rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/passenger-5.0.18/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
PassengerRoot /usr/local/rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/passenger-5.0.18
PassengerDefaultRuby /usr/local/rbenv/versions/2.2.3/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 when you are done editing.
--------------------------------------------
Validating installation...
* Checking whether this Passenger install is in PATH... ✓
* Checking whether there are no other Passenger installations... (!)
You are currently validating against Phusion Passenger 5.0.18, located in:
/usr/local/rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/passenger-5.0.18/bin/passenger
Besides this Passenger installation, the following other
Passenger installations have also been detected:
/usr/local/rbenv/shims/passenger
Please uninstall these other Passenger installations to avoid
confusion or conflicts.
* Checking whether Apache is installed... ✓
* Checking whether the Passenger module is correctly configured in Apache... (!)
You did not specify 'LoadModule passenger_module' in any of your Apache
configuration files. Please paste the configuration snippet that this
installer printed earlier, into one of your Apache configuration files, such
as /etc/apache2/apache2.conf.
Detected 0 error(s), 2 warning(s).
Press ENTER to continue.
--------------------------------------------
Deploying a web application
To learn how to deploy a web app on Passenger, please follow the deployment
guide:
https://www.phusionpassenger.com/library/deploy/apache/deploy/
Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) :-)
https://www.phusionpassenger.com
Phusion Passenger is a registered trademark of Hongli Lai & Ninh Bui.
なんか警告が出てる気がしますが、インスールができました。
最後の方にある設定をapacheのconfファイルに記載します。
--snippetオプションであとでもみれます。
$ passenger-install-apache2-module --snippet
apacheの設定
apacheの設定は、/etc/apache2/apache2.confに記載してもいいのですが、ファイルを分けて細かく管理するらしいので、/etc/apache2/sites-available配下に新しくファイルをつくります。
<VirtualHost *:80>
ServerName redmine20150918.cloudapp.net
DocumentRoot /var/www/redmine-3.0.4/public
</VirtualHost>
LoadModule passenger_module /usr/local/rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/passenger-5.0.18/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
PassengerRoot /usr/local/rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/passenger-5.0.18
PassengerDefaultRuby /usr/local/rbenv/versions/2.2.3/bin/ruby
</IfModule>
今動いている000-default.confをとめて、passenger.confを有効にします。対話形式なので、それぞれファイル名を入力します。
$ sudo a2dissite # 000-defaultを入力
$ sudo a2ensite # passengerを入力
sites-enabled/にリンクがはられるようです。
設定が完了したら、apacheを再起動します。
$ sudo service apache2 restart
WEBrickのときに、3000番のポートをあけていたら、80番に変更しておきます。うまく行ければ、http://redmine20150918.cloudapp.net/ でアクセスできます。