LoginSignup
11
7

More than 3 years have passed since last update.

マルチドメイン(VirtualHost)にSSL設定する

Last updated at Posted at 2020-08-03

マルチドメインサイトにSSL設定をする機会があり、設定方法のメモとして残します。

はじめに

マルチドメイン(複数ドメイン)とは、1つのサーバに複数のドメインでWebサイトを管理できる方法。ドメインごとに異なるWebサイトを公開・運営が可能。
その設定をするためにVirtualHostというサーバ技術を使用します。

VirtualHostには

  • NAMEベース
    • 利用するドメイン全てに同じIPアドレスを使用
  • IPベース
    • 利用するドメインごとにIPアドレスを設定(1つのサーバ内に複数のIPアドレス割振)

の2種類がある。

前提

今回の設定する環境

  • CentOS6.10
  • apache2.2.15
  • VirtualHost(NAMEベース)で80ポートで公開されている
  • sshでサーバにアクセスでき、sudo権限で操作できる状態
  • ドメイン名取得済(Let's Encryptで必須)

1. バージョン確認

CentOS

$ cat /etc/redhat-release
CentOS release 6.10 (Final) 

apache

$ httpd -v
Server version: Apache/2.2.15 (Unix)

2. ポート確認

ポート

httpsは443ポートを使用するので、ポートが開いているかをまず確認します

$ sudo netstat -ltupn

443ポートがLISTEN状態になっていればOK

iptables

次にFireWallにhttps通信が許可されているか確認します

$ sudo /etc/rc.d/init.d/iptables status

dpt:443があれば許可されています。

ない場合は新しくルールを追加します。
iptablesは上から順にルールが適用されるため、REJECTよりも上に追加する必要があります。

$ sudo iptables -I INPUT 6(追加する位置番号) -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT

追加する位置番号は適宜変更してください

もう一度一覧を表示し、REJECTより上に追加されていればOK

iptablesを再起動させる

$ sudo service iptables restart

2. Let's Encryptを利用するための準備

pythonのバージョン確認

Let's Encryptを利用するには、Certbotをインストールする必要があります。
このツールはpython2.7系が必要なので、まずはpythonのバージョンを確認します。

$ python --version
Python 2.6.6

上記のようにpython2.6系が表示されたら次に進んで2.7系をインストールします。
※すでに2.7系が入っている場合は次の手順を飛ばしてください。

python2.7系をインストール

$ sudo yum install centos-release-scl-rh
$ sudo yum install python27
$ sudo scl enable python27 bash

再度バージョンを確認して変わっていればOK

$ python --version
Python 2.7.17

certbotのインストール

cartbot-autoを取得

$ url https://dl.eff.org/certbot-auto -o /usr/bin/certbot-auto

権限の変更

$ chmod 700 /usr/bin/certbot-auto

3. certbot-autoで証明書を発行

$ certbot-auto certonly --webroot -w /var/www/hoge -d hoge.com --email hoge@hoge.com

-w /var/www/hoge:ファイルのパス
-d hoge.com:ドメイン名
--email hoge@hoge.com:メールアドレス※証明書期限切れの際、通知が届きます

実行するといくつか質問が表示されます
・Please read the Terms of Service at...:a
・Would you be willing to share your email address...:y or n ※どちらでも可

最後に以下のメッセージが表示されていればOKです。

IMPORTANT NOTES:
 - Congratulations! ...

証明書を確認

発行された証明書を確認します
まずドメイン名のディレクトリができているかを確認します

$ sudo ls -al /etc/letsencrypt/live/

合計 16
drwx------ 3 root root 4096  7月 30 12:47 2020 .
drwxr-xr-x 9 root root 4096  7月 31 11:52 2020 ..
-rw-r--r-- 1 root root  740  7月 30 12:47 2020 README
drwxr-xr-x 2 root root 4096  7月 30 12:47 2020 hoge.com

作成されたドメイン名のディレクトリに入り、証明書ファイルを確認します

$ sudo ls -al /etc/letsencrypt/live/hoge.com

合計 12
drwxr-xr-x 2 root root 4096  7月 30 12:47 2020 .
drwx------ 3 root root 4096  7月 30 12:47 2020 ..
-rw-r--r-- 1 root root  692  7月 30 12:47 2020 README
lrwxrwxrwx 1 root root   40  7月 30 12:47 2020 cert.pem -> ../../archive/hoge.com/cert1.pem
lrwxrwxrwx 1 root root   41  7月 30 12:47 2020 chain.pem -> ../../archive/hoge.com/chain1.pem
lrwxrwxrwx 1 root root   45  7月 30 12:47 2020 fullchain.pem -> ../../archive/hoge.com/fullchain1.pem
lrwxrwxrwx 1 root root   43  7月 30 12:47 2020 privkey.pem -> ../../archive/hoge.com/privkey1.pem

4.証明書を設定する

まずファイル編集をする前にバックアップを取っておきます。
cp [バックアップするファイル] [保存名]

$ cp ssl.conf ssl.conf_bk

ssh.confを編集

ssh.confを開く

$ sudo /etc/httpd/conf.d/ssl.conf

ファイル内の以下の部分を削除

<virtualhost _default_:443>
~
</virtulahost>

以下の一行を追記

NameVirtualHost *:443

:qwで保存

ドメインごとの設定ファイルを編集

ssh.confとは別にxxx.confというファイルでドメインごとのvirtualhost設定をしているとします。

xxx.confを開く

$ sudo /etc/httpd/conf.d/xxx.conf

ファイル内に以下を追記

<virtualhost *:443>
DocumentRoot "/var/www/hoge"
ServerName hoge.com:443

SSLEngine on
SSLProtocol all -SSLv2
SSLCertificateFile /etc/letsencrypt/live/hoge.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hoge.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/hoge.com/fullchain.pem
</virtualhost>

:wqで保存

Apatcheを再起動

$ sudo service httpd restart

5. サイトを確認

https://でアクセスして鍵マークがついていれば成功です!
スクリーンショット 2020-08-03 16.42.52.png

おまけ:公開中httpをhttpsにリダイレクトさせる

xxx.conf内ですでに記載されている該当ドメインの80ポート内に以下を追記する

<virtualhost *:80>
DocumentRoot "/var/www/hoge"
ServerName hoge.com
RewriteEngine On //追記
RewriteRule ^(.*)?$ https://hoge.com$1 [R=301,L] //追記
</virtualhost>

apatcheを再起動させて、確認

サブドメイン(www.hoge.com)のリダイレクト方法は以下の記事がわかりやすかったです
www有無・SSL(https)有無のリダイレクト書き方サンプルコード

ありがとうございました。

11
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
7