https://gitlab.com/matoken/koedolug-2022.07
Webサーバが死んWebサーバが死んだので503だけをだので503だけを返す返す(Lighttpd/Apache(Lighttpd/Apachehttpd)httpd)1 / 27
View Slide
南隅から参加(鹿児島の右下)好きなLinuxディストリビューションはDebianお仕事募集 mailto:work@matohara.orgKenichiro Matohara(matoken)Kenichiro Matohara(matoken)https://matoken.orghttps://matoken.org2 / 27
最近最近自宅サーバも死ぬ*Facebook復旧https://www.facebookwkhpilnemxj7asaniu7vnjjbiltxjqh3 / 27
今月壊れたもの今月壊れたものHP ProLiant MicroServer(初代)NASや自宅サーバとして利用していたUnboundも立てていたので名前解決ができなくなったりして困ったたぶん以前からS.M.A.R.T.から警告が出ていた / のdiskが死んだディスプレイとディスクが必要4 / 27
外部向けWebサーバ外部向けWebサーバ外向けにもいくつかのサービスが動いていた復旧目処がたっていないSBCのRaspberry Piでhttpdを起動してhttpステータスコード503を返したい.5 / 27
HTTPステータスコードHTTPステータスコード通常は200 OKだけど目にすることはない.404 Not Foundなどはよく見かけるのでは?今回は503 Service Unavailableにしてみた.503 といっしょにRetry-Afterで復旧予定時間も出すといいのだけど未定なので無しでHTTP レスポンスステータスコード - HTTP | MDN503 Service Unavailable - HTTP | MDNRFC 7231 - Hypertext Transfer Protocol(HTTP/1.1): Semantics and Content6 / 27
Raspberry Piの状況Raspberry Piの状況Raspberry Pi 3 model BRaspberry Pi OS bullseye armhf導入micro httpd起動中7 / 27
httpdを変更httpdを変更micro httpdでの方法がよくわからない(VirtualDomain/すべて503等)のでmicro httpdは止めて,Lighttpdに変更$ sudo systemctl stop micro-httpd$ sudo systemctl disable micro-httpd$ sudo apt install lighttpd lighttpd-mod-openssl8 / 27
SSL証明書の入手SSL証明書の入手認証局には以前も利用していたLet’s Encryptを利用,専用の証明書管理ツールのcertbotを導入して利用$ sudo apt install certbot$ sudo certbot certonly -d sub1.example.org$ sudo certbot certonly -d sub2.example.org$ sudo certbot certonly -d sub3.example.org:9 / 27
Lighttpdのhttps設定Lighttpdのhttps設定lighttpd-enable-mod コマンドでLighttpdのsslモジュールとrewriteモジュールを有効に$ sudo lighttpd-enable-mod rewrite ssl10 / 27
Lighttpdでのhttpsの設定Lighttpdでのhttpsの設定lighttpd/conf-enabled/10-ssl.conf を編集して証明書ファイル指定2つ目以降ののドメインを設定Lighttpdを再読込して動作を確認ssl.pemfile = "/etc/letsencrypt/live/sub1.example.org/fullchain.pem"ssl.privkey = "/etc/letsencrypt/live/sub1.example.org/privkey.pem"$HTTP["host"] =~ "sub2.example.org"{ssl.pemfile = "/etc/letsencrypt/live/sub2.example.org/fullchain.pem"ssl.privkey = "/etc/letsencrypt/live/sub2.example.org/privkey.pem"}$HTTP["host"] =~ "sub3.example.org"{ssl.pemfile = "/etc/letsencrypt/live/sub3.example.org/fullchain.pem"ssl.privkey = "/etc/letsencrypt/live/sub3.example.org/privkey.pem"}:$ sudo systemctl lighttpd restart$ w3m https://sub1.example.org/$ w3m https://sub2.example.org/11 / 27
HTTPステータスコード503を返HTTPステータスコード503を返すすHTTPステータスコード503を返すだけのcgi scriptを作成していつもそれを返すように12 / 27
cgiを有効にcgiを有効にlighttpd-enable-mod コマンドでLighttpdのcgiモジュールを有効に/etc/lighttpd/conf-enabled/10-cgi.conf を以下のように修正Lighttpdを再読込して動作を確認$ sudo lighttpd-enable-mod cgi$ grep -v ^# lighttpd/conf-enabled/10-cgi.conf | uniqserver.modules += ( "mod_cgi" )cgi.assign = (".cgi" => "/usr/bin/perl",)$ sudo systemctl lighttpd restart$ echo "#!/usr/bin/perlprint 'Content-Type: text/plain\n\nhello world.'" > /var/www/html/hello.cgi$ sudo chown www-data.www-data /var/www/html/hello.cgi$ sudo chmod 700 /var/www/html/hello.cgi$ w3m https://sub1.example.org/hello.cgi$ sudo rm /var/www/html/hello.cgi13 / 27
503を返すcgiを用意503を返すcgiを用意HTTPステータスコード503を返す /var/www/html/503.cgi を用意#!/usr/bin/perluse strict;use warnings;print "Status: 503 Service Unavailable\n";print "Content-Type: text/html\n\n";print <home.matoken.orgServer is down.END_OF_HTML14 / 27
cgi scriptに権限を設定して動作確cgi scriptに権限を設定して動作確認認$ sudo chown www-data.www-data /var/www/html/503.cgi$ sudo chmod 700 /var/www/html/503.cgi$ w3m -dump_head https://sub1.example.org/503.cgi | grep ^HTTPHTTP/1.0 503 Service Unavailable$ w3m https://sub1.example.org/503.cgi15 / 27
LighttpdですべてのアクセスにLighttpdですべてのアクセスに503を返すようにする503を返すようにする/etc/lighttpd/lighttpd.conf ファイルに以下を追記Lighttpdを再読込して動作を確認$HTTP["url"] != "/503.cgi$" {url.rewrite = ( "" => "/503.cgi" )}$ sudo systemctl lighttpd restart$ w3m https://sub1.example.org/hoge$ w3m https://sub2.example.org/fuga$ w3m http://sub1.example.org/hoge$ w3m http://sub2.example.org/fuga16 / 27
ログ確認ログ確認/var/log/lighttpd/を見るとerror.logはあるけどaccess.logがない?lighttpd-enable-mod コマンドでLighttpdのaccesslogモジュールを有効に$ sudo lighttpd-enable-mod accesslog$ sudo systemctl lighttpd restart17 / 27
とりあえずokとりあえずok案外アクセスある(9000/day)ほぼクロウラー503を返してるのでリトライしてる?復旧見込み時間を長めに設定して返すようにすると減るかも?18 / 27
自宅回線が遅く自宅回線が遅くもしかしてアクセスが多いせい?DNSを書き換えてアクセスの多いドメインを外のサーバに回してみるいつもはDown 8Mbps/Up 0.8Mbps程$ speedtest-cli --simplePing: 639.861 msDownload: 0.44 Mbit/sUpload: 0.77 Mbit/s19 / 27
外のサーバ外のサーバDebian bullseye amd64Apache httpd 2.4.3820 / 27
対象ドメインのSSL証明書取得対象ドメインのSSL証明書取得cpしてきてもいいと思う$ sudo certbot certonly -d sub1.example.org21 / 27
503用のhtmlを用意503用のhtmlを用意$ sudo -u www-data mkdir /var/www/sub1.example.org$ sudo -u www-data vi /var/www/sub1.example.org/503.html22 / 27
対象ドメインの設定を作成対象ドメインの設定を作成`/etc/apache2/sites-enabled/090-sub1.example.org.confapache httpd再起動1すべてを503として/503.htmlにリダイレクトServerName sub1.example.orgRedirect permanent / https://sub1.example.org/ServerName sub1.example.orgServerAdmin [email protected]DocumentRoot /var/www/sub1.example.org/RedirectMatch 503 ^/(?!503\.html) ErrorDocument 503 /503.htmlErrorLog ${APACHE_LOG_DIR}/error_sub1.example.org.logCustomLog ${APACHE_LOG_DIR}/access_sub1.example.org.log combinedSSLCertificateFile /etc/letsencrypt/live/sub1.example.org/fullchain.pemSSLCertificateKeyFile /etc/letsencrypt/live/sub1.example.org/privkey.pem1$ sudo service apache2 restart23 / 27
DNS書き換えを行い動作確認DNS書き換えを行い動作確認DNS書き換え前にhosts書き換えでテスト$ echo "192.0.2.5 sub1.example.org" | sudo tee -a /etc/hosts$ w3m -dump_head head http://sub1.example.org/piyo | grep ^HTTP/HTTP/1.1 503 Service Unavailable$ w3m -dump_extra head http://sub1.example.org/piyo | lv24 / 27
余録)cowsay余録)cowsayLighttpdはcgiなのでなんとなくcowsayに喋ってもらうようにした(偶にecho-sd).しかしcgiから呼ぶと横幅がうまく取れなくなるよう(未解決)25 / 27
まとめまとめ自宅サーバが起動しなくなったのでRaspberry Piで503を返すようにした.Lighttpdはrewrite+cgiApache httpdはrewrite+html自宅回線が遅くなったのは?復旧のためにアルバイト探し中……26 / 27
奥付奥付発表未発表発表者利用ソフトウェアライセンス小江戸らぐ 7月のオフな集まり(第240回)Kenichiro Matohara(matoken)Asciidoctor Reveal.jsCC BY 4.027 / 27