RoundCubeMail(ウェブメールサーバ)の設定方法



●概要

 HotmailやYahoo!メールのようなWebブラウザを利用してメールのやりとりができるRoundCube Webmailを設定します。RoundCube WebmailはAjaxを使用したリッチなユーザインタフェースが特徴のWebメールシステムとなっています。また、PHPで書かれており日本語を含む多言語に対応しています。
 下記の設定は、Webサーバ、Webサーバ間通信内容暗号化、メールサーバ、データベースサーバが既に構築されていることが前提です。

●設定の流れ

 RoundCubeはメールサーバへの接続にIMAPを使用します。POP3を使用することはできません。
 今回は1台のサーバでSMTPサーバ、IMAPサーバ、HTTPサーバ、Webmailサーバを動作させます。
 下記の設定手順はWebmailサーバ以外のサーバすべてを準備する流れとなっています。

RoundCubeが動作するようセキュリティの変更(FirewallとSELinuxの設定)
  ↓
RoundCubeでアクセスできるSMTPサーバの構築(PostfixまたはSendmailの設定)
  ↓
RoundCubeでアクセスできるIMAPサーバの構築(Dovecotの設定)
  ↓
RoundCubeが動作できるHTTPサーバの構築(Apacheの設定)
  ↓
RoundCubeが使用するDBの構築(MariaDBの設定)
  ↓
RoundCubeのインストール(リポジトリの追加とインストール)
  ↓
RoundCubeが動作できるHTTPサーバの設定(Apacheの設定)
  ↓
RoundCubeの構築(RoundCubeMailの初期設定)
  ↓
トラブル対処(エラーが表示されるとき)

オマケ メールボックスにMailDir形式を使用する場合

●RoundCubeMailのインストール

 RoundCubeMailは通常のリポジトリに含まれていません。EPELリポジトリを追加してから、インストールします。
 ※注意点 Apacheを先にインストールしないと、RoundCube向けのアクセス制御ファイルがApacheのconfディレクトリにインストールされません。
# yum install -y epel-release
# yum install -y roundcubemail

※Rocky 8の場合
# dnf install http://rpms.remirepo.net/enterprise/remi-release-8.5.rpm -y
# yum-config-manager --disable remi-modular
# yum-config-manager --disable remi-safe
# dnf --enablerepo=remi install roundcubemail -y

 RoundCube用のテーブルを作成します。roundcubeというユーザとをroundcubemailというテーブルを作成し、アクセス権を与えます。
# mysql -u root -p
Enter password:[パスワード入力]
MariaDB [(none)]> create database roundcubemail character set utf8 collate utf8_bin;
MariaDB [(none)]> grant all on roundcubemail.* to roundcube@localhost identified by 'パスワード';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> quit
 RoundCubeのDB情報を取り込みます。パスワードはroundcubeユーザで指定したパスワードです。
# mysql -u roundcube roundcubemail -p < /usr/share/roundcubemail/SQL/mysql.initial.sql
 PHP設定ファイルを編集します。
# vi /etc/php.ini
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
;date.timezone =
date.timezone = Asia/Tokyo
 ↑ メール一覧で日付が表示されるようにする
 RoundCubeのデフォルト設定ファイルを編集します。
# cp -p /etc/roundcubemail/defaults.inc.php /usr/share/roundcubemail
# vi /usr/share/roundcubemail/defaults.inc.php
// This domain will be used to form e-mail addresses of new users
// Specify an array with 'host' => 'domain' values to support multiple hosts
// Supported replacement variables:
// %h - user's IMAP hostname
// %n - http hostname ($_SERVER['SERVER_NAME'])
// %d - domain (http hostname without the first part)
// %z - IMAP domain (IMAP hostname without the first part)
// For example %n = mail.domain.tld, %t = domain.tld
$config['mail_domain'] = 'mail.server.com';
 ↑ 送信元メールアドレスの@以下を指定
// Use this charset as fallback for message decoding
$config['default_charset'] = 'ISO-2022-JP';
 ↑ 日本語対応
// enforce connections over https
// with this option enabled, all non-secure connections will be redirected.
// set the port for the ssl connection as value of this option if it differs from the default 443
$config['force_https'] = true;
 ↑ httpsアクセス(暗号化通信)を強制
// the way how contact names are displayed in the list
// 0: display name
// 1: (prefix) firstname middlename surname (suffix)
// 2: (prefix) surname firstname middlename (suffix)
// 3: (prefix) surname, firstname middlename (suffix)
$config['addressbook_name_listing'] = 2;
 ↑ アドレス帳で姓名の形式で表示する
// Set true if deleted messages should not be displayed
// This will make the application run slower
$config['skip_deleted'] = true;
 ↑ 削除済のメッセージを表示しない
 次にHTTPのRoundCubeパスのアクセスを許可させるために、roundcubemail.confを修正します(緑字部分)。
 また、httpsでアクセスさせるためDirectoryIndex index.phpを追記します。この行を記載していないと下記エラーのため接続拒否されます。
Cannot serve directory /usr/share/roundcubemail/: No matching DirectoryIndex () found, and server-generated directory index forbidden by Options directive
 /etc/httpd/conf.d/roundcubemail.confを編集します。
<Directory /usr/share/roundcubemail/>
    <IfModule dir_module>
        DirectoryIndex index.php
    </IfModule>
    <IfModule mod_authz_core.c>
        # Apache 2.4
        Require all granted
    </IfModule>
    <IfModule !mod_authz_core.c>
        # Apache 2.2
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
        Allow from ::1
    </IfModule>
</Directory>>
<Directory /usr/share/roundcubemail/installer/>
    <IfModule mod_authz_core.c>
        # Apache 2.4
        Require all granted
    </IfModule>
    <IfModule !mod_authz_core.c>
        # Apache 2.2
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
        Allow from ::1
    </IfModule>
</Directory>
 デーモンを自動起動に設定し、httpdを起動します。
#systemctl enable httpd
#systemctl start httpd

●RoundCubeMailの初期設定

 ブラウザでhttp(s)://サーバアドレス/roundcubemail/installer/へアクセスします。正常にインストールがされていれば、画面にOKの文字が並びます。
 この時、config.inc.phpが存在しないというエラーが表示された場合、下記を実施してください。
# cp -p /etc/roundcubemail/config.inc.php.sample /etc/roundcubemail/config.inc.php
 DB周辺にNot Availableがあると思いますが、MySQLがOKになっていれば問題ありません。
 ページ下部の「Next」を押下し、Create Configの画面に移動します。

Database setup
 Database typeでMySQLを選択
 Database serverにlocalhostと入力
 Database nameにroundcubemailと入力
 Database user nameにroundcubeと入力
 Database passwordにroundcubemailデータベース用のパスワードを入力

IMAP Settings
 default_hostにメールサーバ名またはIPアドレスを入力します。SSL/TSLを使用する場合、ssl://localhostのように入力します。
 default_portに143を入力します。SSL/TSLの場合、993と入力します。
 junk_mboxに「Spam」と入力します。

SMTP Settings
 smtp_serverにメールサーバ名またはIPアドレスを入力します。
 Use the current IMAP username and password for SMTP authenticationをチェックしSMTP認証にIMAPサーバのユーザ名/パスワードを使用するようにします。
 default_portに587を入力します。

Display settings & user prefs
 languageに「ja_JP」と入力します。

 「CREATE CONFIG」ボタン押下します。
 「Download」ボタン押下します。
 ダウンロードしたconfig.inc.phpをWinSCP等を利用して/etc/roundcubemail/config.inc.phpにアップロードします。
 configのアップロードまたは更新後「CONTINUE」ボタンを押下します。

 TestConfigがOKになっていることを確認します。MariaDBへの接続のパスワードなどが間違っているとエラーが表示されます。
 SMTPテスト及びIMAPテストを実施します。
 localhostで設定してOKにならない場合は、127.0.0.1に置き換えてください。

 全て完了すると画面下部に/usr/share/roundcubemail/installerを削除するか、config.inc.phpにenable_installerをdisabledに設定するか、と表示されます。

 設定完了後、インストーラーを無効にする必要がありますので、/etc/roundcubemail/config.inc.phpに下記を追記します。
$config['enable_installer'] = false;
 設定後、ブラウザでアクセスするとインストーラ画面が拒否されます。
 また、ブラウザのキャッシュが残っていることが多く正常アクセスが見えてしまうことがあります。その場合は、別のPCや別のブラウザからアクセスして確認してください。
 installerのディレクトリを何かしらの理由で削除したくないが、$config['enable_installer'] = false;の設定だけでは不安の場合は、roundcubemail.confでinstallerディレクトリへのアクセスをローカルホストからのみにしてしまいましょう。
 /etc/httpd/conf.d/roundcubemail.confを編集します。
<Directory /usr/share/roundcubemail/installer/>
    <IfModule mod_authz_core.c>
        # Apache 2.4
        # Require all granted
        Require local
    </IfModule>
    <IfModule !mod_authz_core.c>
        # Apache 2.2
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
        Allow from ::1
    </IfModule>
</Directory>
 デーモンを自動起動に設定し、httpdを起動します。
#systemctl enable httpd
#systemctl start httpd.service
 これでローカルホスト以外からのアクセスは拒否されます。

 ブラウザからアクセスし、ログインユーザを指定します。
http(s)://サーバアドレス/roundcubemail/


●RoundCubeMailからメール送信ができない現象

 構築中にSMTPテスト及びIMAPテストを実施し問題はありませんでした。ところが、いざRoundCubeMailにログインしたところIMAPにおる受信は問題ありませんでしたが、SMTP送信ができませんでした。
 いろいろ調べた結果、ModSecurityが原因であることが判明しました。ModSecurityのログに出力された該当IDを除外したところ正常に送信することが出来領になりました。

●defaults.inc.phpの更なる設定変更

 ログ出力時の日付形式の変更
$config['log_date_format'] = 'Y-M-d H:i:s O';
 RoundCubeのバージョン情報隠蔽
$config['useragent'] = 'Roundcube Webmail
 received headerの暗号化
$config['http_received_header_encrypt'] = true;
 設定が反映されない場合、httpdを再起動します。
#systemctl enable httpd
#systemctl start httpd.service


●config.inc.phpの設定変更

 IMAPサーバを指定 (SSL経由)
 $config['default_host'] = 'ssl://mail.bigbang.mydns.jp';
 IMAPポート変更 (SSL経由)
 $config['default_port'] = 993;
 SMTPサーバを指定 (SSL経由)
 $config['smtp_server'] = 'ssl://mail.bigbang.mydns.jp';
 SMTPポート変更 (SSL経由)
 $config['smtp_port'] = 465;
 SMTP認証にIMAP認証と同じユーザ名を使う )
 $config['smtp_user'] = '%u';


●ログインできない

 ブラウザからログインしようとすると「IMAPサーバへの接続に失敗しました」と表示され、ログインできません。thunderbirdで接続すると問題なく接続できます。

# tail -f /var/log/roundcubemail/errors.log
[26-Apr-2022 06:57:47 +0000]: <2j3ircmf> IMAP Error: Login failed for アカウント名 against localhost from 60.***.***.***. Could not connect to ssl://localhost:993: Unknown reason in /usr/share/roundcubemail/program/lib/Roundcube/rcube_imap.php on line 211 (POST /roundcubemail/?_task=login&_action=login)

# tail -f /var/log/maillog
Apr 26 06:57:47 ホスト名 dovecot[11944]: imap-login: Disconnected (disconnected before auth was ready, waited 0 secs): user=<>, rip=::1, lip=::1, TLS, session=<E2TfNYndppMAAAAAAAAAAAAAAAAAAAAB>


 参考URL:Roundcubeでsmtps(またはimaps)接続できない

 下記行をconfig.inc.phpに追記することにより問題が解決しました。
# vi /etc/roundcubemail/config.inc.php
$config['imap_conn_options'] = array(
  'ssl' => array(
    'verify_peer' => false,
    'verify_peer_name' => false,
  ),
);
 smtpsの場合、下記のように記載します。
$config['smtp_server'] = 'ssl://localhost';

(途中、省略)

$config['smtp_conn_options'] = array(
  'ssl' => array(
    'verify_peer' => false,
    'verify_peer_name' => false,
  ),
);


●送信試験が失敗する

 参考URL:「fatal: no SASL authentication mechanisms」 によりPostfixでSMTP Authによるメール送信ができない

 Roundcube Webmail Installerの「3.Test config」の「Test SMTP config」で「NOT OK」となる現象が発生しました。
 最初、submissionポートの問題かと思われましたがsmtpポートに設定変更しても問題が解消されませんでした。
 その時のログは下記のとおりです。

May  8 23:34:25 tk2-235-27182 postfix/smtpd[86200]: connect from localhost[::1]
May  8 23:34:25 tk2-235-27182 postfix/smtpd[86200]: warning: SASL authentication failure: Internal Error -4
May  8 23:34:25 tk2-235-27182 postfix/smtpd[86200]: warning: SASL authentication failure: Internal Error -4
May  8 23:34:25 tk2-235-27182 postfix/smtpd[86200]: warning: SASL authentication failure: Internal Error -4
May  8 23:34:25 tk2-235-27182 postfix/smtpd[86200]: warning: xsasl_cyrus_server_get_mechanism_list: no mechanism available
May  8 23:34:25 tk2-235-27182 postfix/smtpd[86200]: fatal: no SASL authentication mechanisms
May  8 23:34:26 tk2-235-27182 postfix/master[1628]: warning: process /usr/libexec/postfix/smtpd pid 86200 exit status 1
May  8 23:34:26 tk2-235-27182 postfix/master[1628]: warning: /usr/libexec/postfix/smtpd: bad command startup -- throttling


 問題を解消させるため、下記パッケージをインストールします。
 ・cyrus-sasl-md5
 Plain認証を利用する場合は
 ・cyrus-sasl-plain
 をインストールします。
# dnf install cyrus-sasl-md5 -y
# dnf install cyrus-sasl-plain -y
 インストール後、動作確認してみます。

May 9 00:43:11 tk2-235-27182 postfix/smtpd[86734]: connect from localhost[::1]
May 9 00:43:11 tk2-235-27182 postfix/smtpd[86734]: discarding EHLO keywords: CHUNKING
May 9 00:43:11 tk2-235-27182 postfix/smtpd[86734]: NOQUEUE: reject: RCPT from localhost[::1]: 554 5.7.1 <アカウント名@ホスト名>: Recipient address rejected: Access denied; from=<アカウント名@ホスト名> to=<アカウント名@ホスト名> proto=ESMTP helo=<メールサーバ名>
May 9 00:43:11 tk2-235-27182 postfix/smtpd[86734]: lost connection after RSET from localhost[::1]
May 9 00:43:11 tk2-235-27182 postfix/smtpd[86734]: disconnect from localhost[::1] ehlo=1 mail=1 rcpt=0/1 rset=1 commands=3/4


 rejectされていました。
 reject解消のため、/etc/postfix/main.cfを編集し、「mynetworks = 」に「, [::1]/128」を追記しました。