이 문서에서는 아래 두 가지 내용을 다룬다.
- Let’s Encrypt 을 통해 무료로 웹사이트에 SSL 적용
- Really Simple SSL 플러그인으로 SSL 자동 변경 적용
본인의 블로그 웹서버는 아래 조합으로 서비스 중이다.
CentOS 7 + APM + WordPress
이 기반으로 이미 구성되어 있다고 가정하고 ssl 통신을 적용하는 방법에 대해 알아본다.
목차
1. 공유기 443 포트 개방
본인 ESXi PC 기반 하이퍼바이저는 공유기 밑에서 운영 중이다.
따라서 Web Server VM에 대한 추가적인 포트 개방이 필요하다.
기존에는 블로그 웹서버에 80(http) 포트만 개방되어 있었다. 추가적으로 443(https) 포트를 함께 개방해 준다.
공유기 설정하는 방법에 대해서는 생략한다.
* 웹서버의 firewalld 에서 https 서비스를 add 해줘야 한다.
# firewall-cmd --permanent --add-service=https # firewall-cmd reload
2. CertBot 설치
모질라 재단의 무료 SSL 프로젝트인 Letsencrypt 으로부터 손쉽게 키를 받기위해 CertBot이라는 프로그램을 설치한다.
서버에 아래 2가지 패키지 설치
# yum install -y certbot python2-certbot-apache
- 만약 설치가 되지 않는다면 아래와 같이 epel repo를 설치 후 다시 설치를 진행한다.
# yum install epel-release
2. DNS 설정
인증서를 받아오기위해 DNS A 레코드가 제대로 설정되어 있어야 한다.
그렇지 않으면 아래와 같은 메시지가 발생될 수 있다.
[root@mws ~]# certbot --apache certonly -d mapoo.net -d www.mapoo.net ~~중간 생략~~ To fix these errors, please make sure that your domain name was entered correctly and the DNS A/AAAA record(s) for that domain contain(s) the right IP address.
- 만약 설정이 제대로 되어 있지 않다면 DNS 설정이 변경이 필요하고, 별 문제 없이 진행되다면 이 단계는 생략.
domain 구입한 호스팅 웹사이트에서 www.mapoo.net 에 대해 수정
== AS-IS == 호스트명 레코드타입 레코드내용 www CNAME XXX.iptime.org A 111.111.111.111 == TO-BE == 호스트명 레코드타입 레코드내용 www A 111.111.111.111 A 111.111.111.111
www 호스트가 CNAME 레코드를 통해 다른 도메인으로 alias 되어 있던 부분을 수정. (본인의 경우)
평소 TTL(캐싱 시간)을 120으로 설정해 뒀다. 따라서 변경 후 곧 적용될 수 있었다.
* DNS 서버에서 CNAME 레코드는 TO-BE 처럼 변경하지 않아도 문제되지는 않는다.
3. 인증키 다운로드
아래와 같이 mapoo.net www.mapoo.net에 대해 인증 진행
[root@mws ~]# certbot --apache certonly -d mapoo.net -d www.mapoo.net Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator apache, Installer apache Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org Obtaining a new certificate Performing the following challenges: http-01 challenge for mapoo.net http-01 challenge for www.mapoo.net Waiting for verification... Cleaning up challenges IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/mapoo.net/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/mapoo.net/privkey.pem Your cert will expire on 2020-05-30. 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
1) 도메인의 소유주(관리자) ? - 메일주소 입력 후 엔터
2) Agree? A 누른 후 - Y 입력 후 엔터
3) 정보성 메일 수신을 원하는지? - N 입력 후 엔터
위 정보를 입력하면 인증된다.
만약 cert를 remove 하고 싶으면,
[root@mws ~]# certbot delete
생성된 키 확인
[root@mws ~]# ls -la /etc/letsencrypt/live/mapoo.net/* -rw-r--r-- 1 root root 692 3월 1 13:47 /etc/letsencrypt/live/mapoo.net/README lrwxrwxrwx 1 root root 33 3월 1 13:47 /etc/letsencrypt/live/mapoo.net/cert.pem -> ../../archive/mapoo.net/cert1.pem lrwxrwxrwx 1 root root 34 3월 1 13:47 /etc/letsencrypt/live/mapoo.net/chain.pem -> ../../archive/mapoo.net/chain1.pem lrwxrwxrwx 1 root root 38 3월 1 13:47 /etc/letsencrypt/live/mapoo.net/fullchain.pem -> ../../archive/mapoo.net/fullchain1.pem lrwxrwxrwx 1 root root 36 3월 1 13:47 /etc/letsencrypt/live/mapoo.net/privkey.pem -> ../../archive/mapoo.net/privkey1.pem
4. apache 설정
ssl.conf 에 인증키 위치 및 VirtualHost 적용
[root@mws ~]# vi /etc/httpd/conf.d/ssl.conf #SSLCertificateFile /etc/pki/tls/certs/localhost.crt (주석 처리) #SSLCertificateKeyFile /etc/pki/tls/private/localhost.key (주석 처리) ## VirtualHost 설정(기존 내용 복붙) <VirtualHost _default_:443> --> 이 사이에 /etc/httpd/conf.d/vhost.conf 에 설정했던 VirtualHost 내용을 추가 </VirtualHost> ## 맨 아래에 아래 3줄 추가 SSLCertificateFile /etc/letsencrypt/live/mapoo.net/cert.pem SSLCertificateKeyFile /etc/letsencrypt/live/mapoo.net/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/mapoo.net/fullchain.pem
SSLCertificateFile, SSLCertificateKeyFile 를 주석처리하지 않을 경우 아래와 같은 에러 메시지가 발생하게 된다.
[root@wellitweb archive]# cat /var/log/httpd/ssl_error_log | grep 'does NOT include' | tail -n1 [Sun Nov 08 17:29:55.108165 2020] [ssl:warn] [pid 28401] AH01909: RSA certificate configured for www.wellit.kr:443 does NOT include an ID which matches the server name
그리고 https가 적용되지 않음.
5. WordPress 환경 설정
관리자 모드 - 설정 - 일반
워드프레스 주소 (URL): https://mapoo.net
사이트 주소 (URL): https://mapoo.net
- 기존 http://mapoo.net 에서 https://mapoo.net으로 변경하는 것임.
6. Apache daemon 재시작
[root@mws ~]# systemctl restart httpd
이제 https://mapoo.net(또는 https://www.mapoo.net) 으로 접근 시 보안웹 통신이 가능하다.
브라우저 주소줄 맨 앞에 좌물쇠가 표시된다.
그러나 아직 http://mapoo.net 접근 시 자동으로 https://mapoo.net으로 리다이렉트 되진 않는다.
아래 방법을 사용하여 리다이렉트 되도록 설정한다.
7. http to https 리다이렉트 설정
http://mapoo.net 접속 시 자동으로 https://mapoo.net 으로 redirect 되도록 설정.
두 가지 방법이 있다.
1) .htaccess 에 redirect 코드 삽입
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] </IfModule>
2) Really Simple SSL 플러그인을 통해
WordPress의 관리자 모드에서
플러그인 - 새로 추가 - Really Simple SSL 검색을 통해 설치 및 활성화
그리고 Really Simple SSL 플러그인의 Setting 부분에서 'Enable 301 .htaccess redirect' 을 on 상태로 변경한다.
Really Simple SSL 플러그인 통한 redirect를 권장한다. 이유는 아래와 같다.
이미 SSL을 적용한 사이트 이지만 해당 사이트의 이미지 콘테츠의 경로, CSS나 js 등에서 주소가 http로 적용되어 있는 경우에 대해서도 별도의 수정 작업 없이 플러그인이 자동으로 https로 인식되게 해준다.- WP Force SSL 플러그인으로 2021년 중반에 변경함.
Really Simple SSL 플러그인이 유료화 되었기 때문으로 기능은 거의 유사하다.
8. 인증상태 검증하기
- SSL Lab
https://www.ssllabs.com/ssltest/analyze
위 사이트에 접속하여 도메인을 입력 후 확인.
9. 인증서 자동 갱신 설정
Letsencrypt 에서 제공하는 무료 ssl의 단점은 인증서 주기가 3개월로 짧다는 것이다.
따라서 발급 후 3개월이 도래하기 전에 갱신이 필요하다.
아래와 같이 2개월(02시)마다 한번씩 갱신되도록 cron에 적용한다.
# crontab -e 0 2 * * */2 /usr/bin/certbot renew --quiet --renew-hook "systemctl reload httpd"
끝