Gazelle
2021年03月20日更新 7181 Views

ApacheでSSL設定を行う【CentOS7/Let's Encrypt】

CentOS 7とApache 2.4、Lets Encryptを用いてSSL設定を行ったのでそのまとめ。

ApacheのVirtual Hostを最低限設定

最初に最低限の設定を/etc/httpd/conf.d/vhost.confに対して行い、SSL化したいサイトをHTTPでアクセスできる状態にしておく。vhost.confは最初作成されていないので作成する。https://httpd.apache.org/docs/2.4/ja/vhosts/examples.html
を参考に設定して欲しい。

# 余計なアクセスを遮断する
<VirtualHost _default_:*>
    DocumentRoot /www/default
</VirtualHost>

# dist以下にhtmlファイルをおいておく
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html/example/dist/
</VirtualHost>

これでまずはHTTPのアクセスが行える。

Let's EncryptでHTTPS化する

Certbotの指示に従っていく。

epelを加えるepelはExtra Packages for Enterprize Linuxの略でこれを入れることでinstallできるyum packageが増えると考えてもらえば良い。

$ sudo yum install epel-release

snapdパッケージをインストール+事前準備+最新versionをインストールしているかのチェック。

$ sudo yum install snapd

$ sudo systemctl enable --now snapd.socket
$ sudo ln -s /var/lib/snapd/snap /snap

$ sudo snap install core; sudo snap refresh core

以前のcertbotを入れていたら、uninstallしておく。取り敢えず、インストールしているかどうか怪しい場合は一回叩いておけばよい。

$ sudo yum remove certbot
No match for argument: certbot
No packages marked for removal.
Dependencies resolved.
Nothing to do.
Complete!

次にcertbotをインストールする

$ sudo snap install --classic certbot

certbot稼働を確実にするためにsymlinkを張る

$ sudo ln -s /snap/bin/certbot /usr/bin/certbot

certbot起動

$ sudo certbot --apache
...
Could not find ssl_module; not installing certificate.

何故かエラーが出た。

mod_sslが無いとのことなのでインストール

$ sudo yum -y install mod_ssl

これで/etc/httpd/conf.modules.d以下に00-ssl.confというファイルが出来上がる。また、中途半端に失敗してしまったので、conf.d以下に生成されるssl.confは削除しておく。

再度certbot起動すると成功。

$ sudo certbot --apache
Created an SSL vhost at /etc/httpd/conf.d/vhost-le-ssl.conf
Deploying Certificate to VirtualHost /etc/httpd/conf.d/vhost-le-ssl.conf
Error while running apachectl graceful.

Job for httpd.service failed because a fatal signal was delivered causing the control process to dump core.
See "systemctl status httpd.service" and "journalctl -xe" for details.

Unable to restart apache using ['apachectl', 'graceful']
Redirecting vhost in /etc/httpd/conf.d/vhost.conf to ssl vhost in /etc/httpd/conf.d/vhost-le-ssl.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

これでホームページにアクセスすると、https化されている事が確認できる。

sslの更新は自動的にセットされているようで、私の場合は下記で確認できた。

systemctl list-timers
NEXT                         LEFT          LAST                         PASSED    UNIT                         ACTIVATES
Sat 2021-03-20 20:21:49 JST  21min left    Sat 2021-03-20 19:21:46 JST  38min ago dnf-makecache.timer          dnf-makecac>Sun 2021-03-21 00:00:00 JST  3h 59min left Sat 2021-03-20 00:00:03 JST  20h ago   unbound-anchor.timer         unbound-anc>Sun 2021-03-21 00:54:46 JST  4h 54min left Sat 2021-03-20 00:54:46 JST  19h ago   systemd-tmpfiles-clean.timer systemd-tmp>Sun 2021-03-21 05:03:00 JST  9h left       n/a        
                  n/a       snap.certbot.renew.timer     snap.certbo>
4 timers listed

実際に更新時に正しくcertが更新されるかはdry-runを実行して成功しているかを確認しておけばよい。

sudo certbot renew --dry-run

Congratuations, all simulated renewals succeededと表示されれば設定がうまく行っているであろう。

(過去の方法) Let's EncryptでHTTPS化する

下記の記事は2020年8月時点の記事である。昔の設定方法であるが備忘録として残しておく。

Certbotの指示に従っていく。

$ sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ sudo yum install certbot python2-certbot-apache

必要なパッケージをインストールしたら、certbotを起動する

$ sudo certbot --apache

途中HTTPS化をどのURLに対して行うべきか?ということを聞かれるので答える。

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: example.com
2: www.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for example.com
Waiting for verification...
Cleaning up challenges
Created an SSL vhost at /etc/httpd/conf.d/vhost-le-ssl.conf
Deploying Certificate to VirtualHost /etc/httpd/conf.d/vhost-le-ssl.conf
Redirecting vhost in /etc/httpd/conf.d/vhost.conf to ssl vhost in /etc/httpd/conf.d/vhost-le-ssl.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

基本的にはURLが短い1が良いだろう。vhost-le-ssl.confというファイルが作成されている事がわかる。

最後にhttps化の期限が来たら自動的に更新してくれるようにする。

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

これにより定期的に更新を見張っていてくれる。etc/crontabを開くと正しく設定されていることがわかる。

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

0 0,12 * * * root python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew -q

結果を確認

vhost.conf

<VirtualHost _default_:*>
DocumentRoot /www/default
</VirtualHost>

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html/example/dist/
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Redirect設定がされていることが分かる。

次にvhost-le-ssl.conf

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html/example/dist/
SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
</VirtualHost>
</IfModule>

あとはsystemctl restart httpdで再起動すればhttpsでアクセスできる

関連記事

CentOS7環境においてnginxとrails6、PUMAアプリケーションサーバを組み合わせ、RailsアプリケーションをProductionモードで動作させる手順を説明します。
2020年03月15日
Railsアプリの独自ドメイン取得からSSL設定まで一貫して行います。環境はCentOS7、サーバはNginx、SSL化には無料のLet's Encryptを使用。
2020年03月18日
さくらVPSでコマンドが何故か重くなった時に、コンソールのCPU使用時間やtopコマンドでのCPU使用率を調査して原因を特定し、不要プロセスを終了させようという話。
2020年04月03日
ApacheでCache-Controlのmax-ageを設定して再訪問時の高速化を図る
2020年08月17日
ホームへ戻る