GNU/Linux >> Linux の 問題 >  >> Cent OS

セットアップ方法CentOS8/RHEL8およびCentOS7/RHEL7でNginxを使用してSSL証明書を暗号化しましょう

Let’s Encryptは、TLS暗号化を有効にするためにWebサイトに無料のSSL証明書を提供する認証局です。 2016年4月に発売されました。

Let’s Encryptは、安全なWebサイトの証明書の作成、検証、署名、実装、および更新のプロセスを自動化します。

現在、Let’s Encryptは、Apache、Nginx、Plex、およびHaproxyの自動認証発行をサポートしています。

前提条件

RHEL/CentOSシステムでLEMPスタックを構成する必要があります。

読む :CentOS 8 /RHEL8にLEMPスタックをインストールする方法

読む: CentOS 7 /RHEL7にLEMPスタックをインストールする方法

Certbotのインストール

ドメインのLet’s Encrypt証明書を生成するには、ターミナルアクセスが必要であり、CertbotACMEクライアントがシステムにインストールされている必要があります。証明書の発行を処理し、作成された証明書をダウンタイムなしで使用するようにNginx構成を更新します。

Certbotは、CentOS 7 / RHEL 7のEPELリポジトリでのみ利用できます。したがって、CentOS 8 /RHEL8のCertbotインストーラーを公式サイトから手動でダウンロードする必要があります。

### CentOS 8 / RHEL 8 ###

curl -O https://dl.eff.org/certbot-auto

mv certbot-auto /usr/local/bin/certbot-auto

chmod 0755 /usr/local/bin/certbot-auto

### CentOS 7 ###

rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

yum install -y certbot python2-certbot-nginx

### RHEL 7 ###

rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

subscription-manager repos --enable rhel-7-server-optional-rpms

yum install -y certbot python2-certbot-nginx

仮想ホストの作成

ここで、ドメインwww.itzgeek.netの仮想ホスト(サーバーブロック)構成ファイルを作成します。

このサーバーブロックは、ドメインのHTTPバージョンを提供します。
vi /etc/nginx/conf.d/www.itzgeek.net.conf

以下の情報を使用してください。

server {
   server_name www.itzgeek.net;
   root /opt/nginx/www.itzgeek.net;

   location / {
       index index.html index.htm index.php;
   }

   access_log /var/log/nginx/www.itzgeek.net.access.log;
   error_log /var/log/nginx/www.itzgeek.net.error.log;

   location ~ \.php$ {
      include /etc/nginx/fastcgi_params;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   }
}

HTMLファイルを配置するためのドキュメントルートを作成します。

mkdir -p /opt/nginx/www.itzgeek.net

ディレクトリの権限を変更します。

chown -R nginx:nginx /opt/nginx/www.itzgeek.net

テストHTMLファイルをWebドメインのドキュメントルートに配置します。

echo "This is a test site @ www.itzgeek.net" > /opt/nginx/www.itzgeek.net/index.html

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

systemctl restart nginx

DNSレコードの作成/更新

DNS管理ツールまたはドメインレジストラにアクセスし、ドメインのA/CNAMEレコードを作成します。例:www.itzgeek.net。

レコードが伝播するまでしばらく待ちます。

Nslookup yum install-ybind-utilsユーティリティを使用してDNS伝播を確認します。

Let'sEncrypt証明書をインストールする

certbotコマンドを使用して、Let’sEncrypt証明書を作成してインストールします。

### CentOS 8 / RHEL 8 ###

/usr/local/bin/certbot-auto --nginx

### CentOS 7 / RHEL 7 ###

certbot --nginx

インタラクティブなプロンプトに従い、証明書を生成してインストールします。

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [email protected]  << Email Address to receive renewal/security notification

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A  << Access Terms and Conditions

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y  << Subscribe to Newsletter

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: www.itzgeek.net
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1  << Choose domain name to generate certificate
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for www.itzgeek.net
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/www.itzgeek.net.conf

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2  << Redirect traffic from HTTP to HTTPS
Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/www.itzgeek.net.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://www.itzgeek.net

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=www.itzgeek.net
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/www.itzgeek.net/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/www.itzgeek.net/privkey.pem
   Your cert will expire on 2019-11-02. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again with the "certonly" option. 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:                    https://eff.org/donate-le

www以外のHTTPリクエストをNginxを使用してwwwHTTPSにリダイレクトする(オプション)

ここで、www以外のHTTPサイトからのトラフィックをWW HTTPSサイト、つまり http://itzgeek.netにリダイレクトするようにNginxサーバーを構成します。>> https://www.itzgeek.net

ここでは、サイトのHTTPバージョン用に作成したものと同じ構成ファイルを使用して、www以外のHTTPサイトからWWHTTPSへのリダイレクトを実行します。

vi /etc/nginx/conf.d/www.itzgeek.net.conf

ファイルの最後に以下の情報を追加してください。

# Redirect NON-WWW HTTP to WWW HTTPS

server {
    if ($host = itzgeek.net) {
        return 301 https://www.itzgeek.net$request_uri;
    }


   server_name itzgeek.net;
    listen 80;
    return 404;

}

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

systemctl restart nginx

ファイアウォール

HTTPS要求を許可するようにファイアウォールを構成します。

firewall-cmd --permanent --add-port=443/tcp

firewall-cmd --reload

Let'sEncrypt証明書を確認する

ウェブサイトのHTTPSバージョンにアクセスして、Let’sEncrypt証明書の詳細を確認します。

http:// your-http-web-site

または

https:// your-https-web-site

SSL証明書のテスト

以下のURLにアクセスして、SSL証明書に問題とそのセキュリティ評価がないか確認してください。

https://www.ssllabs.com/ssltest/analyze.html?d=www.itzgeek.net

Let'sEncrypt証明書を更新

Let's Encryptの証明書の有効期間は90日であり、有効期限が切れる前に更新する必要があります。

デフォルトでは、CentOS 7 / RHEL 7はどちらも、Let’sEncrypt証明書を更新するために1日2回実行されるcronスケジューラエントリで構成されています。

残念ながら、CentOS 8 / RHEL 8の場合、cronスケジューラを手動で構成する必要があります。

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

以下のコマンドを使用して証明書の更新プロセスをシミュレートし、更新がスムーズに行われるようにすることもできます。

### CentOS 8 / RHEL 8 ###

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

### CentOS 7 / RHEL 7 ###

certbot renew --dry-run

出力:

Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/www.itzgeek.net.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator nginx, Installer nginx
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for www.itzgeek.net
Waiting for verification...
Cleaning up challenges

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed with reload of nginx server; fullchain is
/etc/letsencrypt/live/www.itzgeek.net/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates below have not been saved.)

Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/www.itzgeek.net/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - 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.

出力で問題が報告されない場合、証明書の更新は期待どおりに機能します。

結論

それで全部です。 CentOS 8 /RHEL8およびCentOS7/RHEL7でNginxを使用してSSL証明書を暗号化する方法を学習したことを願っています。コメントセクションでフィードバックを共有してください。


Cent OS
  1. RHEL 8 /CentOS8にSSLをインストールする方法

  2. CentOS 7 /RHEL7にNginxを使用してphpMyAdminをインストールする方法

  3. Nginxを使用してCentOS8にLet'sEncryptをインストールする方法

  1. CentOS7でLetsEncryptを使用してNginxを保護する

  2. CentOS 7 /RHEL7にNginxを使用してWordPressをインストールする

  3. CentOS 8 /RHEL8にNginxを使用してWordPressをインストールする方法

  1. セットアップ方法CentOS8/RHEL8およびCentOS7/RHEL7でApacheを使用してSSL証明書を暗号化しましょう

  2. セットアップ方法Debian10/Debian9でNginxを使用してSSL証明書を暗号化しましょう

  3. Nginxを使用してJoomlaをインストールし、Debian11にSSLを暗号化する方法