UbuntuにApache+PHP7でWebサーバー、Postfix+Dovecot+Rainloop+MySQLでWebメール構築(途中)

環境

  • Ubuntu-Server : 15.10
  • PHP : 7.0.7
  • Apache : 2.4.12 (Ubuntu)
  • Postfix : 2.11.3
  • Dovecot : 2.2.18

ポイントはApacheを先にインストールすること!

Webサーバーの構築

Apacheのインストール

$ sudo apt-get update
$ sudo apt-get install apache2

http://~などでアクセスしてみて、インストールできてるか確認

PHP7のインストール

php7は標準ではインストール出来ないのでPPAを追加します

$ sudo add-apt-repository ppa:ondrej/php

php7とphp7のMySQLモジュールをインストールします

$ sudo apt-get update
$ sudo apt-get install php7.0 php7.0-mysql libapache2-mod-php7.0 php7.0-mcrypt

インストールが完了したら、Apacheを再起動して、以下のソースをwebルート(/var/www/html/)にアップロードしてブラウザからアクセスしてみます

$ sudo service apache2 restart
<?php
    phpinfo();
?>

phpの設定が表示されれば成功です

MySQLのインストール

次にMySQLをインストールします

$ sudo apt-get install mysql-server

インストールの時にrootのパスワードの設定するよう求められるので設定します。

完了したら以下のコマンドで、rootとして設定したパスワードでMySQLにアクセスできればインストール成功です。

$ mysql -u root -p

Webメールの構築

  • ドメイン名:hoge.com
  • ユーザー名/パスワード:hoge/hagehage

とする

Postfixの設定

SMTPサーバー。ポート25を使用して、外部のSMTPサーバーからのメール受信や、外部SMTPサーバーへのメール送信を行う。MTA(Message Transfer Agent)とも呼ばれる。

インストール

$ sudo apt-get install postfix

メールサーバーのタイプは[Internet Site]を選択します。 システムメール名(デフォルトのドメイン名のようなもの)はドメイン名を指定します。

設定ファイル編集

設定ファイルをコピーしてから編集

$ mv /etc/postfix/main.cf /etc/postfix/main.cf.old
$ cp /usr/share/postfix/main.cf.dist /etc/postfix/main.cf
$ vi /etc/postfix/main.cf
#基本設定
myhostname = hoge.com
mydomain = hoge.com
myorigin = $mydomain
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
relay_domains = $mydestination
home_mailbox = Maildir/ #ユーザーディレクトリ内にメールが格納される
mynetworks = 127.0.0.0/8
inet_interfaces = all
inet_protocols = ipv4

#以下コメントアウトする
#sendmail_path =       
#newaliases_path =
#mailq_path =
#setgid_group =
#html_directory =
#manpage_directory =
#sample_directory =
#readme_directory =


#SMTP-Auth設定(追記する)
#認証にはdovecotを参照させる(Dovecotと同じユーザー名/パスワードで認証可能にする)
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/dovecot-auth
smtpd_client_restrictions = permit_mynetworks, reject_unknown_client, permit
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination

#スパム対策(追記する)
smtpd_etrn_restrictions = reject
smtpd_helo_required = yes
disable_vrfy_command = yes

Postfixを再起動する

$ newaliases
# /etc/init.d/postfix restart
[ ok ] Restarting postfix (via systemctl): postfix.service.

Dovecotの設定

DovecotはPostfixが保存したメールをメーラーへと渡すPOP3/IMAPサーバー。

この時のDovecotの認証方法をそのままPostfixのSMTP-Auth認証時に参照させる。

インストール

$ apt-get install dovecot-common dovecot-imapd dovecot-pop3d

認証方法を設定する

Dovecotの認証にはいくつかの方法があるが、最もわかりやすいパスワードフアイルを参照する方法(auth-passwdfile)で設定する。

OSのpasswdファイルと似た書式でDovecot専用のパスワードファイルを作製し参照させる。

$ vi /etc/dovecot/conf.d/10-auth.conf
disable_plaintext_auth = no   #コメントを外す
auth_mechanisms = plain login  #コメントを外す 
!include auth-passwdfile.conf.ext #コメントを外す

パスワードファイルの参照先を設定

$ vi/etc/dovecot/conf.d/auth-passwdfile.conf.ext

このように書き換える

passdb {
  driver = passwd-file
  #args = scheme=CRYPT username_format=%u /etc/dovecot/users
  args = /etc/dovecot/users
}

userdb {
  driver = passwd-file
  #args = username_format=%u /etc/dovecot/users

  # Default fields that can be overridden by passwd-file
  #default_fields = quota_rule=*:storage=1G

  # Override fields from passwd-file
  #override_fields = home=/home/virtual/%u
  args = /etc/dovecot/users
}

パスワードファイルを用意

Dovecotのパスワードフアイルは暗号化してから記載する。

ユーザー名:hoge、パスワード:hagehageの時の暗号化文は以下のコマンドで取得できる。

$ doveadm pw -s DIGEST-MD5 -u hoge -p hagehage
{DIGEST-MD5}8687f71fc51f3392269ee94cbc1d33f1

参照先として指定した場所にパスワードファイルを作製する。

(ユーザー名):(暗号化済みパスワード) の形式で記載する。

$ vi /etc/dovecot/users
hoge:{DIGEST-MD5}8687f71fc51f3392269ee94cbc1d33f1

その他編集

$ vi /etc/dovecot/dovecot.conf
# 30行目:変更(IPv6をリスンしない場合)
listen = *
$ vi etc/dovecot/conf.d/10-mail.conf
# 30行目:Maildir形式に変更
mail_location = maildir:~/Maildir
$ vi /etc/dovecot/conf.d/10-master.conf 
# 96-98行目:コメント解除し追記
# Postfix smtp-auth
unix_listener /var/spool/postfix/private/auth {
    mode = 0666
    user = postfix
    group = postfix
}
$ vi /etc/dovecot/conf.d/10-ssl.conf
# 6行目:コメント解除して変更(SSLなし)
ssl = no

# 12,13行目:コメントにする
#ssl_cert = </etc/dovecot/dovecot.pem
#ssl_key = </etc/dovecot/private/dovecot.pem

再起動

Dovecotを再起動し設定を反映させる。

$ /etc/init.d/dovecot restart

動作確認

telnetで接続してPostfix・Dovecot認証の動作確認を行う

POP3動作確認

telnetで接続して認証確認をする。

POP3は平文なので、そのままユーザー名をパスワードを打てばいい。

$ telnet hoge.com 110
Trying 127.0.1.1...
Connected to hoge.com.
Escape character is '^]'.
+OK Dovecot ready.
USER hoge
+OK
PASS hogehoge
OK LOGGED IN.

こうなれば成功

$ telnet hoge.com 110
Trying 127.0.1.1...
Connected to hoge.com.
Escape character is '^]'.
+OK Dovecot ready.
USER hoge
+OK
PASS hogehoge
-ERR [AUTH] Authentication failed.

エラーが出たら次のコマンドを実行してエラーを確認します。

$ doveconf -n

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です