FreeBSD_Servers_V 2018-05-10

Redmineを使っているサーバのお引越しに伴い,改めてインストールからやってみるた。
もう一台作ってみるけど,今度の環境は,FreeBSD11.1
MySQLも8.0でやってみよー

材料




  1. OS Install
  2. update
  3. pkg install
  4. sudo install

iSCSI Initiator Target to TS5400D [#vce9e49a]

FreeBSD 11.1のイニシエータからBuffalo Tera Station TS5400DをターゲットとしてiSCSIで接続した。
iSCSIはSCSI(Small Computer System Interface)をTCP/IPネットワーク上で実現したものである。
iSCSIでは,サーバ側のデバイスを提供する側を「ターゲット」と呼び、サービスを利用する側を「イニシエータ」と呼ぶ。

Target Tera Station側の設定

Tera StationをiSCSIターゲットとして使用するには,最初にボリュームを作成する必要がある。

  1. ボリュームを作成する。
    1. Tera StationにWeb経由でアクセスし,「ディスク」をクリックする。
  2. LVMを有効にする
    1. LVMをクリックし,LVMメニューの「LVMを有効にする」をクリックし,LVMを有効にする。
  3. iSCSIのSWをONにする。
  4. iSCSIメニューに入る。
    1. ボリュームを作成する。
    2. ボリュームを作成するとIQNが表示されるので,メモしておく。

FreeBSD Initiator側の設定

FreeBSD ハンドブック 28.12. iSCSI Initiator and Target Configurationを参照して設定した。

FreeBSD 10以降,iSCSIはカーネルレベルでネイティブに動作するようになったので,デーモンであるiscsidを起動する。

  1. rc.conf
    1. rc.confに以下を追加する。
    2. iscsid_enable=“YES”
  2. iscsidを起動する。(再起動後は不要)
    # service iscsid start

InitiatorからTargetに接続する

とりあえず,Handbookの通り手動でつないでみる。

# iscsictl -A -p 10.10.10.10 -t iqn.xxx.xxx.xxx

繋がったかどうかの確認をしてみる。

# iscsictl
Target Name                 Target Portal  State
iqn.xxx.xxx.xxx.target0     10.10.10.10    Connected: da1
#

Connectedと表示されていれば,/dev/da1としてターゲットを接続できている。

接続できないときは,

# iscsictl
Target Name                 Target Portal  State
iqn                         10.10.10.10    Service unavailable
#

と表示され,コンソールにも以下のメッセージが表示される。

WARNING: 10.10.10.10(iqn): login timed out after 61 seconds; reconnecting

この場合は,Initiatorは,再接続にトライし続けるため,コンソールにメッセージが表示され続ける。
iscsictlコマンドにより明示的に切断する必要がある。

# iscsictl -R -p 10.10.10.10 -t iqn.xxx.xxx.xxx.target0

接続後は...

iscsiにより接続すたあとは,ファイルシステムを作ってマウントすればディスクとして使える。
もちろん,ファイルシステムを作成するのは,最初だけね。

# newfs -U /dev/da0
# mount /dev/da0 /mnt

起動時に自動的にマウントする

/etcにiscsi.confを作成する。

# cat /etc/iscsi.conf
TeraStation { # nickname
    targetaddress = 10.10.10.10
    targetname    = iqn.xxx.xxx.xxx.target0
}

/etc/rc.confにiscsictlを有効にするよう記述する

     :
iscsictl_enable="YES"
iscsictl_flags="-Aa"

iscsi.confを記述することで,ニックネームによる接続/切断が可能となる。

# iscsictl -An TeraStation

だとそのニックネームのターゲットが接続する。
全部接続する場合は,

# iscsictl -Aa

切断は-Aを-Rにすれば良い。

Shutdownの順序

イニシエータであるFreeBSDをShutdownする分には(とりあえず)気にすることなく落とせる。
ターゲットであるTera Stationを落とす場合は,イニシエータ側のFreeBSDがターゲットをマウントしたままになっていると,
iSCSIが応答しなくなるので,ちょうどその時,アクセスすると張り付く。
ターゲットが起動すれば正常に終わるかどうかは別にしてiSCSIの処理からは帰ってくるっぽい。

Targetは,Initiatorより先に落としてはいけない。
Initiatorでは,unmountする前にiscsiデバイスとremoveしてはいけない。
[まっそうだよね。普通にディスクをぶっこ抜いたらどうなるかとおんなじだもんね。]

自動マウントの設定

iscsi_mountというスクリプトを作成し,/usr/local/etc/rc.dに配置した。
起動時,iscsi_mountによりiscsiターゲットの接続,マウントを行う。
停止時は,マウント解除,iscsiターゲットの切断を行う。




mysql8.0

my.cnfでデフォルトキャラクタセットをutf8にする

[client]
  :
default-character-set    = utf8mb4
  :

[mysqld]
  :
character-set-server     = utf8mb4
collation-server         = utf8mb4_ja_0900_as_cs
log-error                = /var/log/mysqld-error.log
  :

[mysqldump]
  :
default-character-set    = utf8mb4
  :

とりあえず起動してみる。

# service mysql-server start

がしかし,ワーニングが出て起動せず。

WARNING: failed precmd routine for mysql

/var/log/mysqld-error.logにportsでインストールしたときに作成されるmysqlユーザが書き込みできないためmysqlが起動できなかった模様。

/usr/local/etc/rc.d/mysql-serverで/dev/nullにエラー出力している箇所を一度コメント化しエラーやワーニングを画面に表示した。

デフォルトだと/var/db/mysqlにデータを作成するので,my.cnfを編集して

/ts5400d/DB/mysql

にデータを置くように変更した。




Apache-2.4.25 Install [#b9467248]

apache web serverの2.4は,APRと分離されたので,先にapr, apr-utilを
インストールする必要がある。
apr(Apache Portable Runtime)は Apache HTTP Server のサポートライブラリ。
OSとソフトウェアの間でOSなどの環境の違いを吸収するAPI

apr-1.6.3

$ tar xvjf apr-1.6.3.tar.bz2
$ cd apr-1.6.3
$ ./configure
$ make
# make install

apr-util-1.6.1

$ tar xvjf apr-util-1.6.1.tar.bz2
$ cd apr-util-1.6.1
$ ./configure --with-apr=/usr/local/apr --with-mysql=/usr/local/mysql
$ make
# make install

pcre-8.42 : ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42.tar.bz2

$ tar xvjf pcre-8.42.tar.bz2
$ ./configure
$ make
# make install

httpd-2.4.33

$ tar xvjf httpd-2.4.33.tar.bz2
$ ./configure --enable-so
$ make
# make install

テストしてみる。

# /usr/local/apache2/bin/apachectl start

ブラウザからIPアドレス直打ちで "It works!"

自動起動の設定
/etc/rc.confに以下を記述

apache2_enable="YES"
apache2_http_accept_enable="YES" <-- see http://nhh.mo-blog.jp/ttt/2006/10/freebsd_aparch2_b1ae.html

/usr/local/etc/rc.d/にapache.shを配置

Accept Filter
FreeBSDではデフォルトでAccept Filterが有効になっているらしく以下のエラーが出力される。

[core:warn] (2)No such file or directory: AH00075: Failed to enable the 'httpready' Accept Filter

httpd.confに以下の設定を追加しAccept Filterを無効にする。

AcceptFilter http  none
AcceptFilter https none

php-7.2.5

PHPをインストールする前にlibxml2をインストールする。

libxml2をportsでインストールする。

# cd /usr/ports/textproc/libxml2
# make
# make install clean

PHPのインストール

$ tar xvjf php-7.2.5.bz2
$ cd php-7.2.5
$ ./configure   \
--with-apxs2=/usr/local/apache2/bin/apxs   \
--enable-mbstring                          \
--enable-mbregex                           \
--enable-mysqlnd                           \
--with-pdo-mysql=mysqlnd                   \
--with-mysql-sock=/tmp/mysql.sock          \
--with-mysqli=mysqlnd                      \
--with-gd                                  \
--with-png-dir=/usr/local/lib              \
--with-jpeg-dir=/usr/local/lib             \
--with-webp-dir=/usr/local/lib
$ make
$ make test
$ sudo make install
LoadModule php5_module     modules/libphp7.so  ←はphpでインストールされるはず
AddType application/x-httpd-php  .php

Image Magick

Image Magickは,多くのライブライに依存しているので,パッケージでインストールしてしまう。

# pkg install imagemagick-nox11
Message from trousers-0.3.14_2:

To run tcsd automatically, add the following line to /etc/rc.conf:

tcsd_enable="YES"

You might want to edit /usr/local/etc/tcsd.conf to reflect your setup.

If you want to use tcsd with software TPM emulator, use the following
configuration in /etc/rc.conf:

tcsd_enable="YES"
tcsd_mode="emulator"
tpmd_enable="YES"

To use TPM, add your_account to '_tss' group like following:

# pw groupmod _tss -m your_account
Message from freetype2-2.8_2:

The 2.7.x series now uses the new subpixel hinting mode (V40 port's option) as
the default, emulating a modern version of ClearType. This change inevitably
leads to different rendering results, and you might change port's options to
adapt it to your taste (or use the new "FREETYPE_PROPERTIES" environment
variable).

The environment variable "FREETYPE_PROPERTIES" can be used to control the
driver properties. Example:

FREETYPE_PROPERTIES=truetype:interpreter-version=35 \
        cff:no-stem-darkening=1 \
        autofitter:warping=1

This allows to select, say, the subpixel hinting mode at runtime for a given
application.

The controllable properties are listed in the section "Controlling FreeType
Modules" in the reference's table of contents
(/usr/local/share/doc/freetype2/reference/ft2-toc.html, if documentation was installed).
Message from ghostscript9-agpl-base-9.16_5:

Note: in order to use the script "dvipdf", dvips must be installed.
This program is provided by another package print/tex-dvipsk.

FAPIfontmap and FAPIcidfmap in /usr/local/share/ghostscript/9.16/Resource/Init
have to be configured if you want to use FAPI feature.
Message from libwmf-nox11-0.2.8.4_15:

===>   NOTICE:

The libwmf-nox11 port currently does not have a maintainer. As a result, it is
more likely to have unresolved issues, not be up-to-date, or even be removed in
the future. To volunteer to maintain this port, please create an issue at:

https://bugs.freebsd.org/bugzilla

More information about port maintainership is available at:

https://www.freebsd.org/doc/en/articles/contributing/ports-contributing.html#maintain-port
Message from liblqr-1-0.4.2:

==========================================================================
NOTE: In order to compile examples for liblqr, you will
also need pngwriter port (/usr/ports/graphics/pngwriter).
Examples are located in /usr/local/share/examples/liblqr-1
==========================================================================
#

Mercurial [#h1f5ed17]

Mercurialインストール

Mercurial自体のインストールはportsでインストールする。
バージョンは,4.6

# cd /usr/ports/devel/mercurial
# make; make install

====
Note that we now install a few more files in /usr/local/share/mercurial:
- shell completion scripts (bash, tcsh and zsh)
- FastCGI / WSGI scripts for web access to repositories
  see http://www.mercurial-scm.org/wiki/PublishingRepositories
- various documentation files
====
#

hgユーザの作成

hgユーザを作成しリポジトリのオーナー/グループをhgにしておく。
ユーザー: hg (143)
グループ: hg (143)

分散処理と中央集権

MercurialはサーバにPushするためにSSHとHTTP経由が使える(らしい)。
今回は,HTTP経由を選択した。

HTTP経由で公開するには,hgweb.cgiを公開する必要がある。
pathを変更する。Mercurialは,python2を使用している。

hgweb.cgi

#!/usr/bin/local/python2.7
#
# An example hgweb CGI script, edit as necessary
# See also http://www.mercurial-scm.com/wiki/PublishingRepositories 

# Path to repo or hgweb config to serve (see 'hg help hgweb')
config = "/PATH/hg/hgweb.config"

# Uncomment and adjust if Mercurial is not installed system-wide
# (consult "installed modules" path from 'hg debuginstall'):
#import sys; sys.path.insert(0, "/path/to/python/lib")
import os
os.environ["HGENCODING"] = "UTF-8"

# Uncomment to send python tracebacks to the browser if an error occurs:
#import cgitb; cgitb.enable()

from mercurial import demandimport; demandimport.enable()
from mercurial.hgweb import hgweb, wsgicgi
application = hgweb(config)
wsgicgi.launch(application)

hgweb.config

[web]
description = repository
author = mail@address
push_ssl = false
allow_push = *

[trusted]
users=hg
groups=hg

[collections]
/PATH/hg/repos = /PATH/hg/repos

[paths]

httpd.confの設定

http://server/cgi-bin/hgweb.cgiでアクセスするのは面倒なのでhttp://server/hgwebでアクセスできるようにする

# vi httpd.conf
    :
  #LoadModule cgid_module modules/mod_cgid.so
  LoadModule cgid_module modules/mod_cgid.so                  <-- コメント削除
    :
  ScriptAlias /cgi-bin "/usr/local/apache2/cgi-bin/"
  ScriptAlias /hgweb   "/usr/local/apache2/cgi-bin/hgweb.cgi" <-- 追加
    :

Redmine

Installing Redmine によれば,現時点(July 14th, 2017)でのrubyバージョンとRedmineのバージョンは以下の通り。

Redmine versionSupported Ruby versionsRails version used
4.0(upcoming)ruby 2.2(2.2.2 and later), 2.3, 2.4Rails 5.1
3.4ruby 1.9.34, 2.0.03, 2.1, 2.22, 2.3, 2.41Rails 4.2
3.3ruby 1.9.34, 2.0.03, 2.1, 2.22, 2.3Rails 4.2
3.2ruby 1.9.34, 2.0.03, 2.1, 2.22Rails 4.2
Support for Ruby 1.9.3, 2.0 and 2.1 has ended by Ruby community.

Redmine 3.4, Ruby 2.4, Rails 4.2の組合せでインストールする。

Ruby

Rubyをportsでインストールする。

# cd /usr/ports/lang/ruby24
# make
# make install
# make clean
Installing ruby-2.4.4,1...
====
Some of the standard commands are provided as separate ports for ease
of upgrading:

        devel/ruby-gems:        gem - RubyGems package manager
        devel/rubygem-rake:     rake - Ruby Make

And some of the standard libraries are provided as separate ports
since they require extra dependencies:

        databases/ruby-gdbm:    GDBM module

Install them as occasion demands.
====

gem

# cd /usr/ports/devel/ruby-gems
# make
# make install
# make clean

gem自身でgemのアップデートをする

# gem update --system

rubygem-rake

# cd /usr/ports/devel/rubygem-rake
# make
# make install
# make clean

さて,ここまでが,redmineをインストールするための種
ここから先は,redmineにインストール手順に従ってっやてみましょ。

Redmine

Step 1 - Redmaine本体のインストール
redmienのソースをサイトからgetする。

これを展開してインストールしていくんだけど,展開した先でredmineが動作する(?)
ようなので,/usr/local/redmineに配置する。

% tar xvzf redmine-3.4.5
# mv redmine-3.4.5 /usr/local/redmine-3.4.5
# cd /usr/local/redmine-3.4.5
# chown -R redmine:redmine /usr/local/redmine-3.4.5

redmineユーザを作成しておく。

# adduser redmine
nologinとしておく

Step 2 - MySQLにユーザとデータベースを作成
データベース,ユーザを作成し,ユーザに権限を与える。
データベース名はredmineとする。別の名前も可。

mysql> CREATE DATABASE redmine CHARACTER SET utf8mb4;
mysql> CREATE USER 'redmine'@'localhost' IDENTIFIED BY 'my_password';
mysql> GRANT ALL PRIVILEGES ON redmine.* TO 'redmine'@'localhost';

Step 3 - データベースに接続するための設定
config/database.example.yml をコピーして config/database.yml を作成する。
作成後,config/database.ymlを編集し,"production"環境用のデータベース設定を行う。

# cd /usr/local/redmine-3.4.5
# cp -p config/database.yml.example config/database.yml
# vi database.yml
production:
  adapter:  mysql2
  database: redmine
  host:     localhost
  username: redmine
  password: MY_PASSWORD

Step 4 - 依存するソフトウェアのインストール
RedmineはRubyGemの依存関係を管理するためBunderを使用する。
まずはBundlerをインストールする。

# gem install bundler

Bundlerのインストール後は,以下のコマンドを実行することでRedmineを実行するために必要な すべてのgemをインストールする。 RDBは,mysqlを使用するので,postgresql, sqliteは除外する。

# bundle install --without development test postgresql sqlite

データベースアダプタ
Redmineはデータベースの設定ファイル config/database.ymlを読み取って,必要なデータベースアダプタを自動的にインストールする。

mysql2が入って調べる。

# gem list --local | grep msyql
[redmine.jpのインストール手順では,記載されて]
[いなかったので,コメントにしておく          ]
[--MySQLのC bindingをインストール            ]
[ # gem install mysql2                       ]

Step 5 - セッションストア秘密鍵の生成
Railsはセッションハイジャックを防ぐために,セッション情報を格納するcookieをエンコードする。
この処理で使われるランダムなキーを生成する。

# bundle exec rake generate_secret_token

Step 6 - データベースのテーブル等の作成
データベース上にテーブルを作成する。
これによりテーブルとRedmineの管理者アカウントが作成される。

# bundle exec rake db:migrate RAILS_ENV=production

Step 7 - デフォルトデータ
下記コマンドを実行し、デフォルトデータをデータベースに登録する。

# bundle exec rake redmine:load_default_data RAILS_ENV=production

コマンドを実行中、どの言語のデフォルトデータを登録するのか選択を求められます。なお、コマンドラインで REDMINE_LANG 環境変数を指定すると、言語の選択を求められることなく自動的に処理が勧められる。

# bundle exec rake redmine:load_default_data RAILS_ENV=production REDMINE_LANG=ja

Step 8 - ファイルシステムのパーミッション
Redmineを実行するOSのユーザーは、以下のディレクトリに対する書き込み権限が必要。

  1. files (添付ファイルの保存ディレクトリ)
  2. log (Redmineのログファイル @production.log@)
  3. tmp と tmp/pdf (もしこれらのディレクトリがなければ作成してください)
  4. public/plugin_assets (プラグインが使用する画像やCSS)

例えば、ユーザーアカウント redmine でアプリケーションを実行する場合:

$ mkdir -p tmp tmp/pdf public/plugin_assets
$ sudo chown -R redmine:redmine files log tmp public/plugin_assets
$ sudo chmod -R 755 files log tmp public/plugin_assets

注意: これらのディレクトリにすでにファイルが存在する場合(例: バックアップからリストアした場合など)、ファイルに実行属性がついていないことを確認する。

$ sudo find files log tmp public/plugin_assets -type f -exec chmod -x {} +

Step 9 - インストールの確認
WEBrickによるwebサーバを起動して,インストールできたかテストする。

# bundle exec rails server webrick -e production

GUIはインストールしていないので,CUIのブラウザ lynxをインストールする。
(久しぶりに使ったかもw)

WEBrickが起動したら、ブラウザで http://localhost:3000/にアクセスする

デフォルトでは,ユーザ名,パスワードともadmin

Apacheとpassengerを連携させる

Step 1 - Passengerのインストール
Apache上でRedmineなどのRailsアプリケーションを実行するために使われるPhusion Passengerをインストールする。

# gem install passenger --no-rdoc --no-ri

Step 2 - PassengerのApache用モジュールのインストール
Apacheをソースからビルドしてインストールした環境では、APXS2およびPATH環境変数を設定する必要がある。

# export APXS2=/usr/local/apache2/bin/apxs
# export PATH=/usr/local/apache2/bin:$PATH

Apache用のモジュールのビルドとインストールを行う。

# passenger-install-apache2-module --auto --languages ruby

モジュールが作成されるとApacheのコンフィグレーションが表示される。

LoadModule passenger_module /usr/local/lib/ruby/gems/2.4/gems/passenger-5.3.1/buildout/apache2/mod_passenger.so
   <IfModule mod_passenger.c>
     PassengerRoot /usr/local/lib/ruby/gems/2.4/gems/passenger-5.3.1
     PassengerDefaultRuby /usr/local/bin/ruby24
   </IfModule>

以下のコマンドでも確認可能

# passenger-install-apache2-module --snippet

Step 3 - Apacheの設定
/usr/local/apache2/conf/httpd.confに追加する。

# Redmineの画像ファイル・CSSファイル等へのアクセスを許可する設定。
# Apache 2.4のデフォルトではサーバ上の全ファイルへのアクセスが禁止されている。
<Directory "/usr/local/redmine-2.4.5/public">
  Require all granted
</Directory>

# Passengerの基本設定。
# passenger-install-apache2-module --snippet で表示された設定を記述。
# 環境によって設定値が異なるため以下の5行はそのまま転記せず、必ず
# passenger-install-apache2-module --snippet で表示されたものを使用すること。
#
LoadModule passenger_module /usr/local/lib/ruby/gems/2.4/gems/passenger-5.3.1/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
  PassengerRoot /usr/local/lib/ruby/gems/2.4/gems/passenger-5.3.1
  PassengerDefaultRuby /usr/local/bin/ruby24
</IfModule>

# 必要に応じてPassengerのチューニングのための設定を追加(任意)。
# 詳しくはPhusion Passenger users guide(https://www.phusionpassenger.com/library/config/apache/reference/)参照。
PassengerMaxPoolSize 20
PassengerMaxInstancesPerApp 4
PassengerPoolIdleTime 864000
PassengerStatThrottleRate 10

#
# Configuration for Radmine
#
Alias /redmine /usr/local/redmine-3.4.5/public
<Location /redmine>
  PassengerBaseURI /redmine
  PassengerAppRoot /usr/local/redmine-3.4.5
</Location>

apachectlでhttp.confの書式を検査する。

# apachectl configtest

問題なければ,apacheを再起動する。

# apachectl restarat

Redmine バージョンアップ (Redmine 2.4 -> Redmine 3.4)

旧サーバから新サーバへRedmineのデータを移行する。

旧サーバ
OSFreeBSD 9.2
Redmine2.4.2
Ruby1.9.3
Rails3.2.16
MySQL5.6.15


新サーバ
OSFreeBSD 11.1
Redmine3.4.5
Ruby2.4.4
Rails4.2.8
MySQL8.0.11

旧サーバでデータの保存
データベースのデータをダンプする。

$ mysqldump -u username -p redmine > redmine_data_date.dump

files, plug-inを保存

新サーバへデータベースを移行する。

$ mysql -u username -p redmine < redmine_data_date.dump

データベースを移動したのち,マイグレーションを行う。

# bundle exec rake db:migrate RAILS_ENV=production

エラーが表示されるので,該当のデータを削除する。

mysql> drop table xxxx;

再度マイグレーションを実施。エラーが無くなるまでテーブルの削除とマイグレーションを繰り返す。


Last-modified: 2022-05-05 (木) 22:27:10