アンドロイド

CentOS 8で暗号化してApacheを保護する

Установка и настройка CentOS в VirtualBox

Установка и настройка CentOS в VirtualBox

目次:

Anonim

Let's Encryptは、無料のSSL証明書を提供するInternet Security Research Group(ISRG)によって開発された、無料の自動化されたオープンな認証局です。

Let's Encryptによって発行された証明書は、すべての主要なブラウザーによって信頼されており、発行日から90日間有効です。

このチュートリアルでは、ApacheをWebサーバーとして実行しているCentOS 8に無料のLet's Encrypt SSL証明書をインストールする方法について説明します。 certbotツールを使用して、証明書を取得および更新します。

前提条件

続行する前に、次の前提条件が満たされていることを確認してください。

  • パブリックサーバーのIPを指すドメイン名を持っています。 example.com を使用し example.com 。Apacheがインストールされ、ドメイン用に構成された仮想ホストを使用してサーバー上で実行されます。ポート80および443はファイアウォールで開いています。

SSL暗号化Webサーバーに必要な次のパッケージをインストールします。

sudo dnf install mod_ssl openssl

mod_sslパッケージをインストールすると、localhostの自己署名キーと証明書ファイルが作成されます。 ファイルが自動的に作成されない場合は、 openssl コマンドを使用して作成できます。

sudo openssl req -newkey rsa:4096 -x509 -sha256 -days 3650 -nodes \ -out /etc/pki/tls/certs/localhost.crt \ -keyout /etc/pki/tls/private/localhost.key

Certbotをインストールする

Certbotは無料のコマンドラインツールで、サーバーからHTTPS暗号化を取得して更新し、HTTPSを自動有効化するプロセスを簡素化します。

certbotパッケージは標準のCentOS 8リポジトリには含まれていませんが、ベンダーのWebサイトからダウンロードできます。

rootまたはsudoユーザーとして次の wget コマンドを実行して、certbotスクリプトを /usr/local/bin ディレクトリーにダウンロードします。

sudo wget -P /usr/local/bin

ダウンロードが完了したら、ファイルを実行可能にします。

sudo chmod +x /usr/local/bin/certbot-auto

Strong Dh(Diffie-Hellman)グループを生成

Diffie–Hellmanキー交換(DH)は、セキュリティで保護されていない通信チャネルを介して暗号キーを安全に交換する方法です。 セキュリティを強化するために、2048ビットDHパラメーターの新しいセットを生成します。

sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048

サイズは最大4096ビットまで変更できますが、システムのエントロピーによっては生成に30分以上かかる場合があります。

Let's Encrypt SSL証明書の取得

ドメインのSSL証明書を取得するには、 ${webroot-path}/.well-known/acme-challenge ディレクトリに要求されたドメインを検証するための一時ファイルを作成することで機能するWebrootプラグインを使用します。 Let's Encryptサーバーは、一時ファイルに対してHTTP要求を行い、要求されたドメインがcertbotが実行されているサーバーに解決されることを検証します。

セットアップをより簡単にするために、 .well-known/acme-challenge すべてのHTTPリクエストを単一のディレクトリ /var/lib/letsencrypt ます。

次のコマンドを実行してディレクトリを作成し、Apacheサーバーに対して書き込み可能にします。

sudo mkdir -p /var/lib/letsencrypt/.well-known sudo chgrp apache /var/lib/letsencrypt sudo chmod g+s /var/lib/letsencrypt

コードの重複を避け、構成の保守性を高めるには、次の2つの構成スニペットを作成します。

/etc/httpd/conf.d/letsencrypt.conf

Alias /.well-known/acme-challenge/ "/var/lib/letsencrypt/.well-known/acme-challenge/" AllowOverride None Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec Require method GET POST OPTIONS Alias /.well-known/acme-challenge/ "/var/lib/letsencrypt/.well-known/acme-challenge/" AllowOverride None Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec Require method GET POST OPTIONS /etc/httpd/conf.d/ssl-params.conf

SSLCipherSuite EECDH+AESGCM:EDH+AESGCM # Requires Apache 2.4.36 & OpenSSL 1.1.1 SSLProtocol -all +TLSv1.3 +TLSv1.2 SSLOpenSSLConfCmd Curves X25519:secp521r1:secp384r1:prime256v1 # Older versions # SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1 SSLHonorCipherOrder On Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" Header always set X-Frame-Options DENY Header always set X-Content-Type-Options nosniff # Requires Apache >= 2.4 SSLCompression off SSLUseStapling on SSLStaplingCache "shmcb:logs/stapling-cache(150000)" # Requires Apache >= 2.4.11 SSLSessionTickets Off SSLOpenSSLConfCmd DHParameters "/etc/ssl/certs/dhparam.pem"

上記のスニペットは、Cipherli.stが推奨するチッパーを使用しています。 OCSP Stapling、HTTP Strict Transport Security(HSTS)、Dhキーを有効にし、セキュリティに重点を置いたHTTPヘッダーをほとんど適用しません。

変更を有効にするために、Apache構成をリロードします。

sudo systemctl reload

これで、webrootプラグインを使用してcertbotスクリプトを実行し、SSL証明書ファイルを取得できます。

sudo /usr/local/bin/certbot-auto certonly --agree-tos --email [email protected] --webroot -w /var/lib/letsencrypt/ -d example.com -d www.example.com

成功すると、certbotは次のメッセージを出力します。

IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/example.com/privkey.pem Your cert will expire on 2020-01-26. To obtain a new or tweaked version of this certificate in the future, simply run certbot-auto again. To non-interactively renew *all* of your certificates, run "certbot-auto 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:

すべてが設定されたので、次のようにドメイン仮想ホスト構成を編集します。

/etc/httpd/conf.d/example.com.conf

ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Protocols h2 http:/1.1 Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog /var/log/httpd/example.com-error.log CustomLog /var/log/httpd/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem # Other Apache Configuration ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Protocols h2 http:/1.1 Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog /var/log/httpd/example.com-error.log CustomLog /var/log/httpd/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem # Other Apache Configuration ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Protocols h2 http:/1.1 Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog /var/log/httpd/example.com-error.log CustomLog /var/log/httpd/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem # Other Apache Configuration ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Protocols h2 http:/1.1 Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog /var/log/httpd/example.com-error.log CustomLog /var/log/httpd/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem # Other Apache Configuration

上記の構成では、HTTPSを強制し、wwwからwww以外のバージョンにリダイレクトしています。 また、HTTP / 2が有効になり、サイトがより高速で堅牢になります。 必要に応じて自由に構成を調整できます。

Apacheサービスを再起動します。

sudo systemctl restart

これで、 https:// を使用してWebサイトを開くことができ、緑色のロックアイコンが表示されます。

SSL証明書を自動更新しましょう

Let's Encryptの証明書は90日間有効です。 有効期限が切れる前に証明書を自動的に更新するには、1日に2回実行されるcronjobを作成し、有効期限の30日前に証明書を自動的に更新します。

次のコマンドを実行して、証明書を更新してApacheを再起動する新しいcronjobを作成します。

echo "0 0, 12 * * * root python3 -c 'import random; import time; time.sleep(random.random() * 3600)' && /usr/local/bin/certbot-auto -q renew --renew-hook \"systemctl reload httpd\"" | sudo tee -a /etc/crontab > /dev/null

更新プロセスをテストするには、certbotコマンドに続けて --dry-run スイッチを使用します。

sudo /usr/local/bin/certbot-auto renew --dry-run

エラーがない場合、更新プロセスが成功したことを意味します。

結論

このチュートリアルでは、CentOSでLet's Encryptクライアントcertbotを使用してドメインのSSL証明書を取得する方法について説明しました。 また、証明書を使用するようにApacheを構成し、自動証明書更新用のcronjobをセットアップする方法も示しました。

Certbotスクリプトの詳細については、Certbotのドキュメントをご覧ください。

Apache CentOS certbot sslを暗号化しましょう