LoginSignup
2
0

More than 3 years have passed since last update.

Ubuntu + Apache + SSL + デジタル証明書 (3.デジタル証明書編)

Last updated at Posted at 2020-06-14

作業日:2020年6月

Ubuntu + Apache + SSL + デジタル証明書 (1.Apache編)
Ubuntu + Apache + SSL + デジタル証明書 (2.SSL編)
Ubuntu + Apache + SSL + デジタル証明書 (3.デジタル証明書編)←本編です。

デジタル証明書は Let's Encrypt を利用します。
作業をする前に、Apacheをいったん停止しておきます。

$ sudo systemctl stop apache2

add-apt-repository コマンドを使えるように、パッケージをインストールします。

$ sudo apt install software-properties-common

certbotのリポジトリを追加します。

$ sudo add-apt-repository ppa:certbot/certbot

リポジトリが追加されたので、インデックス情報を更新しておきます。

$ sudo apt update

python-certbot-apache パッケージをインストールします。

$ sudo apt install python-certbot-apache

certbotコマンドを実行して証明書を取得し、セットアップします。

$ sudo certbot --apache
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): abc@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 at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A ←Aを入力して同意します。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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: N ←パートナー団体へメールアドレスの提供を同意するか聞いてきます。ここはまぁ、Nとしておきます。
No names were found in your configuration files. Please enter in your domain
name(s) (comma and/or space separated)  (Enter 'c' to cancel): www.example.com ←ドメイン名を入力します。

/etc/apache2/sites-enabled/000-default.conf などに ServerName ディレクティブが事前に記載されていれば、ここでの入力は必要なく、設定したVertualHostの数ぶんだけ自動表示してくれ、そこから選べばいいようになっているはずです。
例えばこんな感じです。

Whitch names would you like to active HTTPS for ?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1.www.example.com
2.xxx.example.com
3.yyy.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers by commons and/or spaces, or leav input blank to select all options shown (Enter 'c' to cancel):
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for www.komagatabashi.com
Enabled Apache rewrite module
Waiting for verification...
Cleaning up challenges
Created an SSL vhost at /etc/apache2/sites-available/000-default-le-ssl.conf
Deploying Certificate to VirtualHost /etc/apache2/sites-available/000-default-le-ssl.conf
Enabling available site: /etc/apache2/sites-available/000-default-le-ssl.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

Webブラウザから'http://'でのアクセスがあった場合にHTTPSにリダイレクトさせるように2を選んでおきます。

Enabled Apache rewrite module
Redirecting vhost in /etc/apache2/sites-enabled/000-default.conf to ssl vhost in /etc/apache2/sites-available/000-default-le-ssl.conf

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

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

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 cert will expire on 2020-09-12. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot 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

これで完了しました。
設定した内容は /etc/apache2/sites-available/000-default-le-ssl.conf に記述されます。

$ cat /etc/apache2/sites-available/000-default-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        ServerName www.example.com

        ServerAdmin abc@example.com
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf


SSLCertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

この状態で、Apacheを起動します。

$ sudo systemctl start apache2

Let's Encrypt の証明書は3ヶ月ごとに更新が必要なので、crontabに登録して自動化しておきます。

$ sudo crontab -e

毎月1日午前3時に更新されるようにcrontabファイルに次のように記述しました。
この時間にはあらかじめApacheを終了させておく必要があります。

0 3 1 * * /usr/bin/certbot renew
2
0
1

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
2
0