EC2でRedmineを構築する手順

Redmineを構築する手順

Version

| Package       | Version       |
| ------------- | ------------- |
| Redmine       | 3.4.2         |
| Ruby          | 2.4.2         |
| Apache        | 2.4.27        |
| Passenger     | 5.1.8         |
| Rails         | 5.1.4         |
| MySQL         |5.6.37         |

アプリケーションのディレクトリ構成

| Name          | Directory                   |
| ------------- | -------------               |
| Redmine       | /opt/local/redmine/src      |
| rbenv          | /usr/local/src/.rbenv/     |

設定ファイル

| Name          | Directory                   |
| ------------- | -------------               |
| Passenger     | /etc/httpd/conf.d/passenger.conf      |
| Redmine　　　　|  /etc/httpd/conf.d/redmine.conf     |
| Database 　　　| /opt/local/redmine/src/latest/config/database.yml     |
| SMTP   　　　　|  /opt/local/redmine/src/latest/config/configuration.yml     |

===============================
# 準備
## 必要なものをインストール
```bash
$ yum -y install selinux-policy-targeted policycoreutils-python
$ yum -y install gcc kernel-devel kernel-headers gcc-c++ glibc-headers openssl-devel

# gitのインストール
$ yum install git

#RubyとPassengerのビルドに必要なヘッダファイルなどのインストール
$ yum install readline-devel libxml2 libxslt libxml2-devel libxslt-devel
$ yum install curl-devel

# redmineのビルドに必要なヘッダファイルなどのインストール
$ yum install ImageMagick ImageMagick-devel
```
## 必要なディレクトリ作成
```bash
$ mkdir -p /opt/local/redmine/src
```
## SELinux security設定
```
$ vim /etc/sysconfig/selinux
```

```diff
@@ -4,7 +4,7 @@
 #     enforcing - SELinux security policy is enforced.
 #     permissive - SELinux prints warnings instead of enforcing.
 #     disabled - No SELinux policy is loaded.
-SELINUX=enforcing
+SELINUX=disabled
 # SELINUXTYPE= can take one of these two values:
 #     targeted - Targeted processes are protected,
 #     mls - Multi Level Security protection.
```

# httpd24をインストール

```bash
$ yum install httpd24 httpd24-devel

$ chown -R apache. /var/log/httpd/
$ chkconfig httpd on

$ httpd -v
# バージョンより値が違い
```

```
Server version: Apache/2.4.27 (Amazon)
Server built:   Sep 15 2017 22:56:01
```

# MySQLをインストール

## インストール
```bash
$ rpm -ihv http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm
$ yum install mysql-server mysql-devel

$ chkconfig mysqld on
$ service mysqld start
```
## 設定
```
# データベース初期設定
$ /usr/bin/mysql_secure_installation
# .....
# Set root password? [Y/n]
# ....
```
## データベース作成
```bash
# Redmine用のデータベース作成
$ mysql -uroot -p
```

```mysql
mysql> create database redmine default character set utf8;
mysql> show databases;
```

```
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| redmine            |
+--------------------+
4 rows in set (0.00 sec)
```

```mysql
mysql> GRANT ALL PRIVILEGES ON `redmine`.* TO 'redmine_admin'@'localhost' identified by 'redmine_admin';
mysql> exit;
```

# Rubyをインストール

```bash
$ git clone https://github.com/sstephenson/rbenv.git /usr/local/src/.rbenv/

$ echo 'export RBENV_ROOT="/usr/local/src/.rbenv"' >> /etc/profile.d/rbenv.sh
$ echo 'export PATH="${RBENV_ROOT}/bin:${PATH}"' >> /etc/profile.d/rbenv.sh
$ echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh
$ cat /etc/profile.d/rbenv.sh
```
```
export RBENV_ROOT="/usr/local/src/.rbenv"
export PATH="${RBENV_ROOT}/bin:${PATH}"
eval "$(rbenv init -)"
```

```bash
$ reboot

# 再SSHログイン
$ env | grep RBENV
```

```
RBENV_ROOT=/usr/local/src/.rbenv
RBENV_SHELL=bash
```

```bash
$ rbenv --version
# バージョンより値が違い
```

```
rbenv 1.1.1-6-g2d7cefe
```

```bash
$ git clone https://github.com/sstephenson/ruby-build.git /usr/local/src/.rbenv/plugins/ruby-build
$ rbenv install -l
```
```
...
2.4.0
2.4.1
2.4.2
2.5.0-dev
...
```
```bash
# ruby-2.4.2をインストール
$ rbenv install -v 2.4.2

$ rbenv rehash
$ rbenv global 2.4.2

$ ruby -v
# バージョンより値が違い
```

```
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]
```

# Railsをインストール

```bash
$ gem update --system
$ gem install nokogiri -- --use-system-libraries
$ gem install bundler --no-rdoc --no-ri
$ gem install --no-ri --no-rdoc rails
$ rbenv rehash

$ rails -v
# バージョンより値が違い
```

```
Rails 5.1.4
```

# Passengerをインストール

```bash
$ gem install passenger --no-rdoc --no-ri
$ rbenv rehash
$ dd if=/dev/zero of=/swap bs=1M count=1024
$ mkswap /swap
$ swapon /swap

$ passenger-install-apache2-module
# ...
# ...
# ...
# 成功したら、
$ passenger-install-apache2-module --snippet > /etc/httpd/conf.d/passenger.conf
$ cat /etc/httpd/conf.d/passenger.conf
```

```
LoadModule passenger_module /usr/local/src/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/passenger-5.1.8/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
  PassengerRoot /usr/local/src/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/passenger-5.1.8
  PassengerDefaultRuby /usr/local/src/.rbenv/versions/2.4.2/bin/ruby
</IfModule>
```

# Redmineをインストール

```bash
$ wget http://www.redmine.org/releases/redmine-3.4.2.tar.gz
$ tar zxvf redmine-3.4.2.tar.gz
$ mv redmine-3.4.2 /opt/local/redmine/src
$ ln -ns /opt/local/redmine/src/redmine-3.4.2 /opt/local/redmine/src/latest
$ ll /opt/local/redmine/src
```
```
total 4
lrwxrwxrwx  1 root root   36 Sep 29 19:23 latest -> /opt/local/redmine/src/redmine-3.4.2
drwxrwxr-x 18 1000 1000 4096 Jul 17 03:18 redmine-3.4.2
```

```bash
$ vim /etc/httpd/conf.d/redmine.conf
```
```
<VirtualHost *:80>
    Alias /redmine /opt/local/redmine/src/latest/public
    <Directory "/opt/local/redmine/src/latest/public/">
        PassengerAppRoot /opt/local/redmine/src/latest/
        RailsEnv production
        RailsBaseURI /redmine
        Options -MultiViews
        Options Indexes FollowSymLinks
        Require all granted
    </Directory>
</VirtualHost>
```

# Redmineの設定を行う

```bash
$ cd /opt/local/redmine/src/latest/config
$ cp database.yml.example database.yml
$ vim database.yml
```

```diff
@@ -6,8 +6,8 @@
   adapter: mysql2
   database: redmine
   host: localhost
-  username: root
-  password: ""
+  username: redmine_admin
+  password: "redmine_admin"
   encoding: utf8
```

```bash
$ cd /opt/local/redmine/src/latest/config
$ cp configuration.yml.example configuration.yml
$ vim configuration.yml
```

```diff
@@ -22,6 +22,23 @@
   #      address: "localhost"
   #      port: 25
   #
+
+
+
+  # ==== SMTP server at example.com using LOGIN authentication and checking HELO for foo.com
+  #
+  # email_delivery:
+    delivery_method: :smtp
+    smtp_settings:
+      address: "{SMTP_HOST}"
+      enable_starttls_auto: true
+      port: {SMTP_PORT}
+      authentication: :plain
+      domain: "{SERVER_HOST}"
+      user_name: {USER_NAME}
+      password: {USER_PASSWORD}
+
+
   # ==== SMTP server at example.com using LOGIN authentication and checking HELO for foo.com
   #
   #  email_delivery:
```

# bundle installを行う

```bash
$ cd /opt/local/redmine/src/latest
$ bundle install --without development test --path vendor/bundle
$ bundle exec rake generate_secret_token

$ RAILS_ENV=production bundle exec rake db:migrate
$ RAILS_ENV=production REDMINE_LANG=ja bundle exec rake redmine:load_default_data
```

# Apacheを起動

```bash
$ cd /opt/local/redmine/src/
$ chown -R apache. redmine-3.4.2

$ service httpd restart
$ service httpd status
# pidより値が違い
```

```
httpd (pid  1201) is running...
```
# 画面確認

ブラウザで http://example.com/redmine をアクセスると、トップ画面が出る。

![730a1fb2e6d2f8eed4840c8d87cbc16d.png](https://qiita-image-store.s3.amazonaws.com/0/155256/94a1ac37-d46c-4428-1af2-a91d01a26a04.png)

== end ==