#
ドキュメント

Document

自分のための備忘録です。

Lets EncryptでSSL化

s-hiroshi.comにサブドメインblogを追加してblog.s-hiroshi.comをSSL化する。

自動

https://certbot.eff.org/lets-encrypt/ubuntubionic-apache

手動作業(記載順序どおりに実行)

基本的には上述の方法で自動化されるが、手動で設定するときは以下を作業する。

  1. DNSを設定:サブドメインblogをCNAMに登録
  2. ディレクトリを作成
  3. Apache設定ファイルを作成
  4. a2ensite でサイトを有効化
  5. Let's Encryptを設定
  6. Apache設定ファイルにSSLの設定を追記
  7. Apache設定ファイルの変更を反映
  8. テストファイルを作成・確認・削除
  9. Cronで自動更新設定

DNS

サブドメインblogをCNAMに登録

ディレクトリ作成

mkdir -p /var/www/html/blog.s-hiroshi.com/current/web/.well-known/acme-challenge

Apache設定ファイル SSLなし版

/etc/apache2/sites-available/blog.s-hiroshi.com.conf

<Directory />
    AllowOverride None
</Directory>

<Directory "/var/www/html/blog.s-hiroshi.com/current/web">
    AllowOverride All
    Options -Indexes +FollowSymLinks
</Directory>

+ <Directory "/var/www/html/blog.s-hiroshi.com/current/web/.well-known/acme-challenge">
+    Allow from all
+    Satisfy any
+ </Directory>

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

<VirtualHost *:80>
    ServerName blog.s-hiroshi.com
    DocumentRoot /var/www/html/blog.s-hiroshi.com/current/web
</VirtualHost>

設定を有効化

$ a2ensite blog.s-hiroshi.com.conf

Let's Encryptを設定

$ certbot certonly --agree-tos --webroot -w /var/www/html/blog.s-hiroshi.com/current/web -d blog.s-hiroshi.com

-w は必須。--webroot を指定した場合 -w でドキュメントルートを指定する。

Apache の場合の自動化例

$ sudo certbot --apache  --agree-tos -w /var/www/html/example.com -d example.com -m info@example.com

Apache設定ファイルを修正 SSL版

  1. blog.s-hirosih.com.confにSSL設定を追記
  2. (必須ではないが重複を避けるため)80ポートのDocumentRootディレクティブを削除
  3. httpに対するリクエストをhttpsへリダイレクト
<Directory />
    AllowOverride None
</Directory>

<Directory "/var/www/html/blog.s-hiroshi.com/current/web">
    AllowOverride All
    Options -Indexes +FollowSymLinks
</Directory>

<Directory "/var/www/html/blog.s-hiroshi.com/current/web/.well-known/acme-challenge">
    Allow from all
    Satisfy any
</Directory>

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

+ <IfModule mod_ssl.c>
+     <VirtualHost *:443>
+         SSLEngine on
+         SSLCertificateFile /etc/letsencrypt/live/blog.s-hiroshi.com/fullchain.pem
+         SSLCertificateKeyFile /etc/letsencrypt/live/blog.s-hiroshi.com/privkey.pem
+         BrowserMatch "MSIE [2-6]" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
+         BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
+         <FilesMatch \.php$>
+             SSLOptions +StdEnvVars
+         </FilesMatch>
+ 
+         DocumentRoot /var/www/html/blog.s-hiroshi.com/current/web
+         ServerName blog.s-hiroshi.com
+         ErrorLog ${APACHE_LOG_DIR}blog.s-hiroshi.com.error.log
+         CustomLog ${APACHE_LOG_DIR}/blog.s-hiroshi.com.access.log common
+         CustomLog ${APACHE_LOG_DIR}/blog.s-hiroshi.com.request_uri.log request_uri
+ 
+         # mod_auth_openidc
+         # OIDCRedirectURI https://blog.s-hiroshi.com/redirect_uri
+     </VirtualHost>
+ </IfModule>

<VirtualHost *:80>
    ServerName blog.s-hiroshi.com
-    DocumentRoot /var/www/html/blog.s-hiroshi.com/current/web
+    RedirectMatch (.*) https://blog.s-hiroshi.com$1
</VirtualHost>

Apache設定ファイルの変更を反映

$ sudo systemctl reload apache2

Cronで自動更新設定

$ sudo certbot renew --dry-run