勉強したことのメモ

Webエンジニア / プログラマが勉強したことのメモ。

CentOS6.10に無料SSL(Let's Encrypt)を導入

   2024/02/22  サーバー

以前にもVPSにLet's Encryptを導入するという記事を書いたが、テストだけして本番導入はしていなかった。せっかくなので本番導入しようと思い色々調べると以前とは若干方法が変わっているようだった。また、今回はサブドメインに対してもSSLを導入したいので、対応した内容をメモ。

 

SSLインストール作業

サーバにSSH接続し、以下の通り進めていく。

#pythonバージョンを確認
python --version
Python 2.6.6

#python2.7を利用できるようにする
yum -y install centos-release-scl
yum -y install scl-utils python27 python27-scldevel
scl enable python27 bash

#再度pythonバージョンを確認
python --version
Python 2.7.13

#certbot-autoをインストール
yum -y install epel-release
curl https://dl.eff.org/certbot-auto -o /usr/bin/certbot-auto
chmod a+x /usr/bin/certbot-auto
certbot-auto --os-packages-only --non-interactive

#証明書を作成(hogehoge.comとhogehoge@gmail.comは適宜変更する)
certbot-auto certonly --non-interactive --agree-tos --webroot -w /var/www/html -d hogehoge.com --email hogehoge@gmail.com

#証明書ファイルが作成されているか確認(作成されていれば.pemファイルが複数表示される)
ll /etc/letsencrypt/live/hogehoge.com/

 

Apacheの設定作業

サーバにSSH接続し、以下の通り進めていく。

vi /etc/httpd/conf.d/ssl.conf

#以下部分をコメントアウトして追記
#SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateFile /etc/letsencrypt/live/hogehoge.com/fullchain.pem

#以下部分をコメントアウトして追記
#SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
SSLCertificateKeyFile /etc/letsencrypt/live/hogehoge.com/privkey.pem

#Apacheの再起動
service httpd restart

この時点でブラウザからHTTPS接続が確認できる。

 

サブドメインを追加する場合

サーバにSSH接続し、以下の通り進めていく。

#証明書を作成
certbot-auto certonly --non-interactive --agree-tos --webroot -w /var/www/html/sub -d sub.hogehoge.com --email hogehoge@gmail.com

#Apacheの設定ファイルを開く
vi /etc/httpd/conf/httpd.conf

#以下を追記する
<VirtualHost *:443>
    ServerAdmin sub.hogehoge.com
    DocumentRoot /var/www/html/sub
    ServerName sub.hogehoge.com
    ErrorLog logs/error_log
    CustomLog logs/access_log common

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/sub.hogehoge.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/sub.hogehoge.com/privkey.pem
   NameVirtualhost *:443
</VirtualHost>

#Apacheの再起動
service httpd restart

この時点でブラウザからHTTPS接続が確認できる。

 

自動更新設定

#cron設定
vi /etc/crontab

#以下を追記する
30 2 * * * scl enable python27 "/usr/bin/certbot-auto renew" && sleep 120 && /etc/init.d/httpd restart

 

WordPressでSSL化した場合

WordPressに限らずhttpからhttpsに移行した場合、ソース内に絶対パスが入っていると混在アクティブコンテンツがどうのこうのでCSSやJSなどの外部ファイルが読み込めず、表示が崩れる場合がある。自身で作成したシステムであれば一つずつ書き換えていくしかないと思うが、WordPressの場合はプラグインで対応できる。

ダッシュボード上より「Really Simple SSL」で検索してインストールするか、以下公式ページからダウンロードしてインストールする。有効化するだけで反映された。

Really Simple SSL

尚、キャッシュ系プラグインを使用している場合はキャッシュをクリアすること。

 

参考サイト

https://www.101010.fun/entry/lets-encrypt-centos6
https://developer-collaboration.com/2018/09/04/lets-encrypt-apache-centos6/
https://zenlogic.jp/aossl/operation/wp-plugin/

 - サーバー