Let’s EncryptでWebサイトSSL化に挑戦したら簡単過ぎた(ConoHa, CentOS7)

昨今の時代の流れに乗ってサイトのSSL化をやってみた。以前別のサービスを使ってSSL化していたことがあったけど、そのためにはSSL化するドメインのメールサーバを立ててないといけなくて、メールのやりとりとかも必要で正直メンドくさかった(しかも英語で)。

↑の経験からSSL化っていろいろ面倒なんだろうなーとか思ってたけど、Let’s Encryptで挑戦してみたら一瞬にできてしまった。すごい。

必要なWebサーバ環境

メールサーバとかは立てなくて大丈夫。でも一応最低限必要な環境を用意しないといけないので念のため記載。

  • ドメイン名(DNS名)でアクセスできるサーバであること
  • http(80番ポート/tcp)、https(443番ポート/tcp)でアクセスできること

また、今回はApacheが動いている状態で実行。他のWebサーバ(Nginxとか)が動いている場合も同じコマンドでいけるみたい。
Webサーバが動いていないサーバでもオプション追加でOK。詳しくは参考サイトを参照。

インストール

# yum install epel-release
# yum install certbot

証明書取得

-wオプションはドキュメントルートディレクトリ、-dはドメイン(FQDN)

[root@www ~]# certbot certonly --webroot -w /var/www/html -d www.tetsis.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): (連絡がとれるメールアドレス)
Starting new HTTPS connection (1): acme-v01.api.letsencrypt.org

-------------------------------------------------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf. You must agree
in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: A

-------------------------------------------------------------------------------
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o: Y
Starting new HTTPS connection (1): supporters.eff.org
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for www.tetsis.com
Using the webroot path /var/www/html for all unmatched domains.
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/www.tetsis.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/www.tetsis.com/privkey.pem
Your cert will expire on 2018-01-27. To obtain a new or tweaked
version of this certificate in the future, simply run certbot
again. To non-interactively renew *all* of your certificates, run
"certbot renew"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- If you like Certbot, please consider supporting our work by:

Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
Donating to EFF:                    https://eff.org/donate-le

Apacheに証明書を設定

# vim /etc/httpd/conf.d/ssl.conf

SSLCertificateFile /etc/letsencrypt/live/www.tetsis.com/fullchain.pem

SSLCertificateKeyFile /etc/letsencrypt/live/www.tetsis.com/privkey.pem

# systemctl restart httpd

これで完了。いい時代になりました。

参考

https://letsencrypt.jp/usage/

CentOS6.5にfmlを導入

ConoHaでメールサーバを構築するで構築したメールサーバにfmlを導入してメーリングリスト(ML)機能を追加したので、その時のログです。
fmlを導入する際には、管理・運用体制をどのようにするかによってインストール方法が変わるのですが、今回は「グループfmlに所属しているユーザはMLの作成ができる」を運用ポリシーにしました。

fmlの実行スクリプトはperlで書かれているが、そのファイル内ではパスが「/usr/local/bin/perl」と指定されていて、元のパス(「/usr/bin/perl」)とは違っているのでシンボリックリンクを追加

# ln -s /usr/bin/perl /usr/local/bin/perl

グループfmlとユーザfmlを作成

# groupadd fml
# useradd -g fml fml

自分が普段使用している一般ユーザ(username)をグループfmlに追加

# vim /etc/group
fml:x:501(←ここの数字は環境によって違う):username

ディレクトリ作成とオーナー・権限変更

# mkdir /usr/local/fml
# mkdir /var/spool/ml
# chown fml:fml /usr/local/fml
# chown fml:fml /var/spool/ml
# chmod g+ws /var/spool/ml

fmlをインストールするディレクトリへ移動し、fmlのソースコードをダウンロード、fmlをインストール

# cd /usr/local/src

# wget ftp://ftp.fml.org/pub/fml/stable/fml-4.0-stable-20040215.tar.gz
# tar zxvf fml-4.0-stable-20040215.tar.gz

# cd fml-4.0-stable-20040215
# su fml
$ perl makefml install
unshift(@INC, /usr/local/src/fml-4.0-stable-20040215/src)
---NOT USING configuration file (for the first time)

THIS HOST (www.tetsis.com) IS [x86_64-unknown-linux-gnu]

---Please Define Your Fml System Configurations

Personal Use or ML-Admin-Group-Shared or fmlserv you use?
Personal, Group, Fmlserv (personal/group/fmlserv) [personal] group

Please define the group (in /etc/group) ML Operators use
Group of Mailing List Operators (fml or GID ([\w\d]+)) [fml]

DOMAIN NAME               [tetsis.com]
FQDN                      [www.tetsis.com]
EXEC FILES DIRECTORY      [/usr/local/fml]
TOP LEVEL ML DIRECTORY    [/var/spool/ml]

Language (Japanese or English) [English] Japanese

TimeZone (TZ: e.g. +0900, -0300) [+0900]

--- summary ---
DOMAIN: tetsis.com
FQDN: www.tetsis.com
EXEC_DIR: /usr/local/fml
ML_DIR: /var/spool/ml
Language: Japanese
TimeZone: +0900

15/03/29 00:27:04 makefml::mkdir /usr/local/fml/.fml (fml)

The current configuration is saved in /usr/local/fml/.fml/system

---Install the Fml system to /usr/local/fml. (y/n) [n] y

(以下インストールが始める)

$ exit

Postfixの設定を変更してPostfixを再起動

# vim /etc/postfix/main.cf
allow_mail_to_commands = alias, forward, include

# service postfix reload

テストMLを作成し、Postfixの設定を変更して、Postfixを再起動
(ここはmain.cfの設定を一度で済ませたいところだが、最初にalias_mapsとalias_databaseを変更すると、newmlを実行した際に/var/spool/etc/aliasesファイルがないとかで、処理が途中で止まってしまう。なので一旦newmlによりMLを作成し、aliasesファイルが生成された後にalias_mapsとalias_databaseに設定を加える流れを採用した。もっとスマートなやり方があればご教授願いたい)

# su fml
$ /usr/local/fml/makefml newml fml-test
$ exit

# vim /etc/postfix/main.cf
alias_maps = hash:/etc/aliases, hash:/var/spool/ml/etc/aliases
alias_database = hash:/etc/aliases, hash:/var/spool/ml/etc/aliases

# service postfix reload

newaliasesを実行し、aliases.dbファイルを生成
そして、aliases.dbのオーナー・権限を変更

# newaliases

# chmod 666 aliases.db
# chown root:root aliases.db

これによりfmlの導入は完了

あとはaddコマンドでメンバーを追加すればMLとして使用できる

$ /usr/local/fml/makefml add fml-test (メールアドレス)

 

おまけ

毎回makefmlする度に「/usr/local/fml/makefml」と打つのは面倒なのでシンボリックリンクによりパスを通す

# ln -s /usr/local/fml/makefml /usr/bin/makefml

 

(2015/06/19追記)

ずっと探していたテンプレートファイルの場所が分かった

/usr/local/fml/etc/makefml/cf

これを変更することでnewmlした際に作成されるML内のcfに反映される
でも、それよりもコマンドのほうが分かりやすいかも

$ makefml config-template

このコマンドで設定したファイルは「/var/spool/ml/etc/makefml/cf」に作成され、上記と同様にnewmlした際に反映される
ただしこの場合はcfファイルの権限をグループfmlに与えなければfmlグループの他のユーザが作成したMLには反映されないので注意

(参考文献)
http://www.fml.org/fml/INSTALL/9.html

http://www.fml.org/software/fml8/Documentation/ja/tutorial/changes.cui.html

ConoHaでメールサーバを構築する

ConoHaでメールサーバを構築しようとしたのだけれど薄い本(専門書)の設定をしただけではメールができなかったので、いろいろ調査して設定を試してみたらメールサーバが完成したので、その時のログです。
また、途中に参考にさせて頂いたサイトのリンクを載せています。

まずはConoHaの薄い本通りに設定

この時、/etc/postfix/main.cfの最後の設定(smtpd_recipient_restrictions)の項目はカンマかもしくは改行した時にスペースが必要
参考にしたのはこちら

それから、今回構築するメールサーバはドメインtetsis.com全体のメールサーバにする予定なのでmydestinationsを以下の様に変更

/etc/postfix/main.cf

mydestination = $myhostname, localhost.$mydomain, localhost
↓
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

それから、私の環境ではISPが25番ポートの通信を遮断しているみたい(OB25B)なので通常のSMTPではサーバと通信できないのでSubmission(587番)ポートを使ってサーバと通信するように設定
参考にしたのはこちら

/etc/postfix/master.cf

#submission inet n       -       n       -       -       smtpd
↓(コメントを外す)
submission inet n       -       n       -       -       smtpd

ここまででローカル配送ができる
しかし、Gmail配送できない。ログを見るとIPv6で通信しているので今回は通信をIPv4に限定する

/etc/postfix/main.cf

inet_protocols = all
↓
inet_protocols = ipv4

これでGmailに配送できる
しかし、迷惑メールトレイに入れられてしまうので、DNSにSPFレコードを追加する

以下の図はDNSのお名前.comにSPFレコードを登録した状態
VALUE部分は「v=spf1 +ip4:(IPアドレス) ~all」にする
参考にしたのはこちら
spf

ここまででGmailの受信トレイに入れられる
一応ConoHaにメールサーバを構築する目標が達成

メールの暗号化

SMTPS、POP3S、IMAPSに対応させる

まずは自己証明書を作成する

[root@www ~]# cd /etc/pki/tls/certs
[root@www certs]# make server.key
umask 77 ; \
/usr/bin/openssl genrsa -aes128 2048 > server.key
Generating RSA private key, 2048 bit long modulus
.........................................+++
...............................+++
e is 65537 (0x10001)
Enter pass phrase:
Verifying - Enter pass phrase:
[root@www certs]# openssl rsa -in server.key -out server.key
Enter pass phrase for server.key:
writing RSA key
[root@www certs]# make server.csr
umask 77 ; \
/usr/bin/openssl req -utf8 -new -key server.key -out server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:JP
State or Province Name (full name) []                                      ~
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:www.tetsis.com
Email Address []:info@tetsis.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@www certs]#
[root@www certs]# openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 3650
Signature ok
subject=/C=JP/L=Default City/O=Default Company Ltd/CN=www.tetsis.com/emailAddress=info@tetsis.com
Getting Private key
[root@www certs]# cp server.key ../private/

最後にPostfixとDovecotの設定をする

/etc/postfix/main.cfに以下を追加

smtpd_use_tls = yes
smtpd_tls_cert_file = /etc/pki/tls/certs/server.crt
smtpd_tls_key_file = /etc/pki/tls/private/server.key
smtpd_tls_session_cache_database = btree:/etc/postfix/smtpd_scache
smtpd_tls_session_cache_timeout = 3600s

/etc/postfix/master.cfの以下のコメントを外す

smtps     inet  n       -       n       -       -       smtpd
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
-o smtpd_client_restrictions=permit_sasl_authenticated,reject

/etc/dovecot/conf.d/10-sslを以下のように修正

#ssl = yes
ssl_cert = </etc/pki/dovecot/certs/dovecot.pem
ssl_key = </etc/pki/dovecot/private/dovecot.pem
↓
ssl = yes
ssl_cert = </etc/pki/dovecot/certs/server.crt
ssl_key = </etc/pki/dovecot/private/server.key

/etc/dovecot/conf.d/10-master.confを以下のように修正

service imap-login {
inet_listener imap {
#port = 143
port = 0
}
inet_listener imaps {
port = 993
ssl = yes
}

service pop3-login {
inet_listener pop3 {
#port = 110
port = 0
}
inet_listener pop3s {
port = 995
ssl = yes
}
}

参考にしたのはこちらこちらこちら

最後にPostfixとDovecotを再起動する

# service postfix restart
# service dovecot restart