LoginSignup
15

More than 5 years have passed since last update.

VagrantでRedmineの環境を構築する(Ubuntu 14.04+nginx 1.6+MySQL 5.6+Ruby 2.1+Rails 3.2+Unicorn 4.8+Redmine 2.6)

Last updated at Posted at 2014-12-06

備忘録です。この構成の記事はたくさんあると思いますが、なるべく簡単に構築できることを目的としています。

仮想Linux環境の構築

ローカルPCに仮想Linux環境を構築します。

VirtualBox+Vagrantのインストール

https://www.virtualbox.org/wiki/Downloads
VirtualBoxを先にインストールします。

http://www.vagrantup.com/downloads.html
次にVagrantをインストールします。

Ubuntuのインストール

ローカルマシンに適当なフォルダを作成し、そこで作業します。

Vagrant Cloud (https://vagrantcloud.com/) からUbuntuの入ったBoxをインストールします。現時点での最新版は14.04 LTS(trusty64)です。

vagrant init ubuntu/trusty64

フォルダ内にVagrantfileが生成されますので、テキストエディタで開いて以下の行のコメントを外します。

config.vm.network "private_network", ip: "192.168.33.10"

これでIP 192.168.33.10で仮想マシンにアクセス出来るようになります。

vagrant up

1回目の起動はBoxをダウンロードしてくるので時間がかかります。
VMが起動したら、SSHでログインします。

vagrant ssh

ユーザーvagrantでログインしていますが、面倒なのでルートで作業します。

sudo su -

パッケージを最新にする

apt update
apt upgrade

updateでパッケージリストを最新にし、upgradeで最新のパッケージをインストールします。

nginxのインストール

PPAから最新バージョンをインストールします。

add-apt-repository -y ppa:nginx/stable
apt update
apt install -y nginx-extras

ブラウザで http://192.168.33.10/ を開いてnginxのウェルカムメッセージが表示されれば成功です。

MySQLのインストール

公式サイトの5.6安定版のパッケージからインストールします。

wget http://dev.mysql.com/get/mysql-apt-config_0.3.2-1ubuntu14.04_all.deb
dpkg -i mysql-apt-config_0.3.2-1ubuntu14.04_all.deb
rm mysql-apt-config_0.3.2-1ubuntu14.04_all.deb
apt update
apt install -y mysql-server

dpkgコマンドの途中で選択画面が出ますが、何もせず「Apply」を選びます。

パッケージインストール中にも選択画面が表示されますが、全てEnterでOKです(rootパスワード無し、testデータベースを作らない)。

Rubyのインストール

RedmineはRuby on Rails製なので、まず新しめのRubyをインストールします。一番新しいの(2.2)はダメでした。

add-apt-repository -y ppa:brightbox/ruby-ng
apt update
apt install -y ruby2.1

Redmineのインストール

データベースの作成

まず、Redmineで使うデータベースredmineを作成します。

mysql -uroot -e "create database redmine"

必要なパッケージのインストール

Redmineを動かすのに必要なパッケージをインストールします。

apt install -y ruby2.1-dev imagemagick libmagickwand-dev libmysqld-dev

Redmineのダウンロード

設置場所は /var/lib/redmine にします。

cd /var/lib/
wget http://www.redmine.org/releases/redmine-2.6.0.tar.gz
tar xf redmine-2.6.0.tar.gz
rm redmine-2.6.0.tar.gz
mv redmine-2.6.0 redmine
cd redmine

database.yml編集

exampleをコピーします。

cp config/database.yml.example config/database.yml

Unicorn用のbundleを追加

echo "gem 'unicorn'" > Gemfile.local

bundle install

先にbundlerをインストールしておきます。

gem install bundler

bundlerで不要なものは除きインストールします。

bundle install --without development test postgresql sqlite

暗号化鍵を生成する

bundle exec rake generate_secret_token

テーブルを生成する

RAILS_ENV=production bundle exec rake db:migrate

WEBrickで動作確認する

bundle exec rails s -e production

ブラウザで http://192.168.33.10:3000/ を開きRedmineが動くか確認してみます。

nginxとUnicornの設定

Unicornの設定

config/unicorn.rb を以下の内容で作成します。

@dir = "/var/lib/redmine"
working_directory @dir
pid "#{@dir}/tmp/pids/unicorn.pid"
stderr_path "#{@dir}/log/unicorn.log"
stdout_path "#{@dir}/log/unicorn.log"
listen "/tmp/unicorn.sock"
worker_processes 2
timeout 30

nginxの設定

/etc/nginx/sites-available/default を開いて内容を全て消して以下の内容を書き込みます。

upstream unicorn {
  server unix:/tmp/unicorn.sock fail_timeout=0;
}
server {
  root /var/lib/redmine/public;
  server_name _;
  try_files $uri @unicorn;
  location @unicorn {
    proxy_set_header Host $http_host;
    proxy_pass http://unicorn;
  }
}

動作確認

まずUnicronをdaemonモードを起動しておき、

bundle exec unicorn -E production -c config/unicorn.rb -D

nginxをリロードします。

nginx -s reload

ブラウザで http://192.168.33.10/ を開いてRedmineが起動するか確認してください。確認できたらUnicronを止めます。

kill -QUIT `cat tmp/pids/unicorn.pid`

Unicornの自動起動設定

最後に、マシン起動時にUnicornも自動起動するように設定します。
/etc/init/redmine.conf を以下の内容で作成します。

description "Redmine"

start on runlevel [2345]
stop on runlevel [016]

env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

chdir /var/lib/redmine
exec bundle exec unicorn -c /var/lib/redmine/config/unicorn.rb -E production

Upstartという仕組みを使っているので、start/stop/restart redmineコマンドで簡単にunicornの起動・停止・再起動ができます。

注意点

Redmineのインストールは名前が示す通り地雷がとても多いので踏まないように。

  • brightbox/ruby-ng で入るRubyの現在の最新版は2.2ですが、DBのマイグレーションでコケます。
  • Redmineの最新版を https://github.com/redmine/redmine のリポジトリからクローンしてくるとRails4が入って嬉しいのですが、プラグインが全然入らなくて死にます。

他にも大量に地雷がありましたが忘れました・・・

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
15