目的
AWS EC2のWordpress(Bitnami)をSSL化したい
Let’s Encryptを使ってSSL化する
環境
ルートドメインはHeroku上の紐づいており、サブドメをAWSのEIPに向けてある(Aレコード)
手順など
1.証明書発行
2.wordpress側の設定
3.Bitnami apacheのrestart
前提
httpsを443ポートで開けている
EC2にSSH接続できる
vim使います
1.証明書発行
SSHでログイン
gitインストール
$ sudo apt-get install git
tmpディレクトリに移動
$ cd /tmp
certbotをクローン
$ git clone https://github.com/certbot/certbot
certbotディレクトリに移動
$ cd certbot
証明書の発行(リクエスト)
./certbot-auto certonly --webroot -w /opt/bitnami/apps/wordpress/htdocs/ -d ドメイン名など --email xxxxx@gmail.comなど -n --agree-tos --debug
最終的に以下のようなメッセージが出れば成功です。
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/ドメイン名/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/ドメイン名/privkey.pem
Your cert will expire on 2020-05-xx. To obtain a new or tweaked
version of this certificate in the future, simply run certbot-auto
again. To non-interactively renew *all* of your certificates, run
"certbot-auto 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
このままだと90日の期限があるので、先にcronで自動更新させます。
$ ./certbot-auto renew --dry-run
$ sudo crontab -e
すると
こんな感じのメッセージが出るので3を選択します。2の方が簡単とは出てますが。
Select an editor. To change later, run 'select-editor'.
1. /bin/ed
2. /bin/nano <---- easiest
3. /usr/bin/vim.basic
4. /usr/bin/vim.tiny
Choose 1-4 [2]: 3
vimで以下を貼り付けます。
30 4 * * * /tmp/certbot/certbot-auto renew --post-hook "sudo /opt/bitnami/ctlscript.sh restart apache" >> /var/log/letsencrypt/renew.log
2.Wordpress側の設定
発行しただけでは通らないので、wordpress側で設定を行います。
bitnami.confの編集
$ sudo vi /home/bitnami/stack/apache2/conf/bitnami/bitnami.conf
デフォルトの部分をコメントアウトして発行したものを読み込ませます
<VirtualHost _default_:443>
DocumentRoot "/opt/bitnami/apache2/htdocs"
SSLEngine on
# SSLCertificateFile "/opt/bitnami/apache2/conf/server.crt"
# SSLCertificateKeyFile "/opt/bitnami/apache2/conf/server.key"
SSLCertificateFile "/etc/letsencrypt/live/ドメイン名/cert.pem"
SSLCertificateKeyFile "/etc/letsencrypt/live/ドメイン名/privkey.pem"
SSLCertificateChainFile "/etc/letsencrypt/live/ドメイン名/fullchain.pem"
<Directory "/opt/bitnami/apache2/htdocs">
Options Indexes FollowSymLinks
AllowOverride All
httpをhttpsにリダイレクトするように設定します。
$ cd ~/apps/wordpress/conf
httpd-prefix.confを編集します。
$ vi httpd-prefix.conf
RewriteCondとRewriteRuleを変更します。
RewriteEngine On
# RewriteCond "%{HTTP_HOST}" ^ec2-([0-9]{1,3})-([0-9]{1,3})-([0-9]{1,3})-([0-9]{1,3})\..*\.amazonaws.com(:[0-9]*)?$
# RewriteRule "^/?(.*)" "%{REQUEST_SCHEME}://%1.%2.%3.%4%5/$1" [L,R=302,NE]
RewriteCond %{HTTPS} !=on
RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [R,L]
3.Apachのrestart
$ sudo /opt/bitnami/ctlscript.sh restart apache
これで完了
参考記事
https://docs.bitnami.com/aws/faq/administration/control-services/
https://docs.bitnami.com/aws/faq/administration/control-services/
https://blog.office-aship.info/bitnami-wordpress-lets-encrypt-https/