AlmaLinux 9.2 apacheサーバ Let’s Encrypt SSL証明書を取得する方法

環境
AlmaLinux release 9.2 (Turquoise Kodkod)

操作方法
1.open sslをインストールします。
# dnf install openssl-devel

2.パッケージ管理システムSnappyインストールを行います

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# dnf install epel-release
# dnf upgrade
# dnf install snapd
# dnf install epel-release # dnf upgrade # dnf install snapd
# dnf install epel-release
# dnf upgrade
# dnf install snapd

スナップ通信ソケットを管理するsystemdユニットを有効化する

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# systemctl enable --now snapd.socket
# systemctl enable --now snapd.socket
# systemctl enable --now snapd.socket

クラシックスナップのサポートを有効にする
# ln -s /var/lib/snapd/snap /snap

snapd のバージョンを更新します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# snap install core
# snap refresh core
# snap install core # snap refresh core
# snap install core
# snap refresh core

バージョンを確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# snap --version
# snap --version
# snap --version

 

3.certbot パッケージインストールを行います

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# snap install --classic certbot
# snap install --classic certbot
# snap install --classic certbot

/snap/bin/certbotへのシンボリックリンクを作成します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# ln -s /snap/bin/certbot /usr/bin/certbot
# ln -s /snap/bin/certbot /usr/bin/certbot
# ln -s /snap/bin/certbot /usr/bin/certbot

certbotを確認します
# ls -la /usr/bin/certbot

3.証明書を取得すします

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# certbot certonly --webroot -w /var/www/html/[FQDN] -d [FQDN]
# certbot certonly --webroot -w /var/www/html/[FQDN] -d [FQDN]
# certbot certonly --webroot -w /var/www/html/[FQDN] -d [FQDN]

4.証明書を自動更新します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# certbot renew --dry-run
# certbot renew --dry-run
# certbot renew --dry-run

snap版certbotをインストールします。
証明書自動更新機能も合わせてインストールされます
# systemctl list-timers | less

snap.certbot.renew.timer のユニットファイルを確認します。
# vi /etc/systemd/system/snap.certbot.renew.timer

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[Timer]
Unit=snap.certbot.renew.service
OnCalendar=*-*-* 03:15
[Timer] Unit=snap.certbot.renew.service OnCalendar=*-*-* 03:15
[Timer]
Unit=snap.certbot.renew.service
OnCalendar=*-*-* 03:15

実行時刻を指定します。

4.apacheのhttpsを設定します
# dnf -y install mod_ssl

ssl.conf ファイルを編集します
# vi /etc/httpd/conf.d/ssl.conf

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/**43 行目 コメント解除して変更**/
DocumentRoot "/var/www/html/<FQDN>"
/**44 行目 コメント解除して変更**/
ServerName <ドメイン名>:443
/**85行目 コメントにしてその下に追加**/
# SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateFile /etc/letsencrypt/live/<FQDN>/cert.pem
/**93行目 コメントにしてその下に追加**/
# SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
SSLCertificateKeyFile /etc/letsencrypt/live/<FQDN>/privkey.pem
/**103行目 追加**/
SSLCertificateChainFile /etc/letsencrypt/live/<FQDN>/chain.pem
/**43 行目 コメント解除して変更**/ DocumentRoot "/var/www/html/<FQDN>" /**44 行目 コメント解除して変更**/ ServerName <ドメイン名>:443 /**85行目 コメントにしてその下に追加**/ # SSLCertificateFile /etc/pki/tls/certs/localhost.crt SSLCertificateFile /etc/letsencrypt/live/<FQDN>/cert.pem /**93行目 コメントにしてその下に追加**/ # SSLCertificateKeyFile /etc/pki/tls/private/localhost.key SSLCertificateKeyFile /etc/letsencrypt/live/<FQDN>/privkey.pem /**103行目 追加**/ SSLCertificateChainFile /etc/letsencrypt/live/<FQDN>/chain.pem
/**43 行目 コメント解除して変更**/
DocumentRoot "/var/www/html/<FQDN>"

/**44 行目 コメント解除して変更**/
ServerName <ドメイン名>:443

/**85行目 コメントにしてその下に追加**/
# SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateFile /etc/letsencrypt/live/<FQDN>/cert.pem

/**93行目 コメントにしてその下に追加**/
# SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
SSLCertificateKeyFile /etc/letsencrypt/live/<FQDN>/privkey.pem

/**103行目 追加**/

SSLCertificateChainFile /etc/letsencrypt/live/<FQDN>/chain.pem

5.Apacheを再起動します
# systemctl restart httpd

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# firewall-cmd --add-service=https --permanent
success
# firewall-cmd --reload
success
# firewall-cmd --add-service=https --permanent success # firewall-cmd --reload success
# firewall-cmd --add-service=https --permanent
success
# firewall-cmd --reload
success

6.http通信を https へリダイレクト
.htaccessを/var/www/html/[FQDN]/ 配下に作成
以下の内容を記載します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

 

AlmaLinux 9

Posted by arkgame