Apache httpd 2.4でも503を返すようにする

自宅サーバが起動しなくなったのでLighttpdで全ページ503を返すようにしました.

思ったよりアクセスが多いのでアクセスの多いドメインのDNSを変更して外のサーバに向けることにしました.
このサーバはApache httpdが動いているのでLighttpdと同じ設定は使えないので少し調べて設定してみました.

対象ドメインの設定を作成.ここでは「 sub1.example.org 」としています.

`/etc/apache2/sites-enabled/090-sub1.example.org.conf
<VirtualHost *:80>
        ServerName sub1.example.org
        Redirect permanent / https://sub1.example.org/
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerName sub1.example.org
        ServerAdmin webmaster@example.org
        DocumentRoot /var/www/sub1.example.org/
        RedirectMatch 503 ^/(?!503\.html) (1)
        ErrorDocument 503 /503.html
        ErrorLog ${APACHE_LOG_DIR}/error_sub1.example.org.log
        CustomLog ${APACHE_LOG_DIR}/access_sub1.example.org.log combined
        SSLCertificateFile /etc/letsencrypt/live/sub1.example.org/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/sub1.example.org/privkey.pem
</VirtualHost>
</IfModule>
  1. すべてのURLを /503.html に転送します.

対象ドメインの証明書を旧サーバからコピーしておきます.

ディレクトリ作成
$ sudo mkdir -p /etc/letsencrypt/tmp/sub1.example.org/

リモートサーバの /etc/letsencrypt/live/sub1.example.org/fullchain.pem/etc/letsencrypt/live/sub1.example.org/privkey.pem/etc/letsencrypt/tmp/sub1.example.org/ 以下に置く.

権限設定してリンクを貼ります.
$ sudo chown -R root.ssl-cert /etc/letsencrypt
$ sudo ln -s /etc/letsencrypt/tmp/sub1.example.org/fullchain.pem /etc/letsencrypt/live/sub1.example.org/fullchain.pem
$ sudo ln -s /etc/letsencrypt/tmp/sub1.example.org/privkey.pem /etc/letsencrypt/live/sub1.example.org/privkey.pem
$ sudo -u www-data mkdir /var/www/sub1.example.org
$ sudo -u www-data vi /var/www/sub1.example.org/503.html

apache httpd再起動

$ sudo service apache2 restart

DNS書き換え前に検証の行えるローカルのPCのhosts書き換えて動作テスト.

$ echo "192.0.2.5 sub1.example.org" | sudo tee -a /etc/hosts (1)
$ w3m -dump_head head http://sub1.example.org/piyo | grep ^HTTP/ (2)
HTTP/1.1 503 Service Unavailable
$ w3m -dump_extra head http://sub1.example.org/piyo | lv (3)
  1. 新しいサーバのIPアドレスを指定する.
  2. 503が帰ってくることを確認
  3. 証明書も確認

動作確認が出来たら /etc/hosts を戻しておく.

この後,DNS書き換えを行いDNSが伝播してから再度動作確認を行う.

対象ドメインのSSL証明書更新もしておく.

$ sudo certbot certonly -d sub1.example.org

という感じでサーバ移行しました.
それまで自宅に9000アクセスほど有りましたが8500程は外のサーバに行くようになりました.

環境
$ dpkg-query -W apache2 certbot
apache2 2.4.38-3+deb10u7
certbot 0.31.0-1+deb10u1
$ lsb_release -dr
Description:    Debian GNU/Linux 10 (buster)
Release:        10
$ arch
x86_64

コメントを残す

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