LoginSignup
4
2

More than 3 years have passed since last update.

CentOS7 環境設定 (Apache2.4 + Let's Encrypt)

Last updated at Posted at 2021-01-28

はじめに

この手の記事は、数多あるが私の環境の備忘録として書いています。
以前書いた
CentOS7 環境設定 (Apache2.4 EventMPM + php-fpm + php7.4)
https://qiita.com/daichi_pd/items/1d9b4c8bef2579abc670
の続きとお考えください。

使っているサーバーは、さくらVPSです。
下記の設定は基本的にroot権限で行ってください。

Apacheの設定

Apacheのバーチャルホストの設定を行います。

以下は、下記の設定例として書いていきます。
ドメイン:example.com, www.example.com
ドキュメントルート:/var/www/html/public

ドメインでのアクセス設定

まずは、設定ファイルを作成します。

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

viでもvimでも、好きなエディタで設定ファイルを開いて設定をしてください。
環境に併せて編集をお願いします。

/etc/httpd/conf.d/example.com.conf
<VirtualHost *:80>
  DocumentRoot "/var/www/html/public"
  ServerName example.com
  ServerAlias www.example.com
  <Directory "/var/www/html/public">
    allow from all
    Options None
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost *:443>
  DocumentRoot "/var/www/html/public"
  ServerName example.com
  ServerAlias www.example.com
  <Directory "/var/www/html/public">
    allow from all
    Options None
    Require all granted
  </Directory>
</VirtualHost>

設定が終わったら、Apacheを再起動しておきます。

systemctl restart httpd

Let's Encryptの設定

certbotをインストールする

yum -y install certbot python-certbot-apache

certbotで証明書を取得する

certbot certonly --webroot -w /var/www/html/public -d www.example.com

メールアドレスを聞かれます。
公開などはされませんので、確実に連絡が取れるメールアドレスにしましょう。
SSLの有効期限が切れそうなときに、ここで指定したメールアドレスに通知してくれます。

:
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): example@example.com

利用規約への同意を聞かれます。

:
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. Do you agree?
(Y)es/(N)o: y

なんかキャンペーン情報などのお知らせを送っても良いか聞いてきます。
とりあえず、Yesでいいのではないでしょうか。

:
Would you be willing, once your first certificate is successfully issued, 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

問題なく設定できていれば、証明書が発行されます

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

ApacheにSSLの設定をする

/etc/httpd/conf.d/example.com.conf
<VirtualHost *:80>
  DocumentRoot "/var/www/html/public"
  ServerName example.com
  ServerAlias www.example.com
  <Directory "/var/www/html/public">
    allow from all
    Options None
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost *:443>
  DocumentRoot "/var/www/html/public"
  ServerName example.com
  ServerAlias www.example.com

  SSLEngine on
  SSLCertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/www.example.com/chain.pem

  <Directory "/var/www/html/public">
    allow from all
    Options None
    Require all granted
  </Directory>
</VirtualHost>

Apacheを再起動します

systemctl restart httpd

Cronを使って定期的にSSLを更新する

Let's Encryptの有効期限は3ヵ月しかありませんので、定期的に更新する必要があります。
自動で更新するためにCronに設定します。
毎月15日、午前5時に実行するようにしておきます。

crontab -e
crontab
00 05 15 * * certbot renew --post-hook "systemctl reload httpd"

これで、作業は以上です。
enjoy!

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2