Let’s Encrypt를 사용해서 https(SSL) 접속 설정하기
지금 사이트를 http로 운영 중이었는데 브라우저에서 최초 접속을 디폴트로 https 접속을 시도해서 귀찮기도 하고, 웹페이지 노출도 https를 우선한다고 해서 Let’s Encrypt를 사용해서 https(SSL) 접속을 설정하고 자동 갱신까지 설정해 보겠습니다.
- 패키지 업데이트 및 필요한 패키지(Certbot 및 플러그인) 설치
-- cat /var/run/reboot-required가 출력되면 reboot 합니다. ubuntu@vm4wordpress:~$ sudo apt update ubuntu@vm4wordpress:~$ sudo apt upgrade -- Nginx를 사용 중인 경우 ubuntu@vm4wordpress:~$ sudo apt install certbot python3-certbot-nginx -- Apache를 사용 중인 경우 ubuntu@vm4wordpress:~$ sudo apt install certbot python3-certbot-apache
- SSL 인증서 발급 및 자동 설정: Certbot은 서버 설정을 분석하여 SSL 인증서를 발급받고, 웹 서버 설정 파일(Conf)까지 자동으로 수정해 줍니다.
-- Nginx 기준 (도메인을 -d 옵션 뒤에 입력) ---- 이메일 등록을 원하지 않는다면 --register-unsafely-without-email 옵션 추가 ubuntu@vm4wordpress:~$ sudo certbot --nginx -d yesxyz.kr -d www.yesxyz.kr Saving debug log to /var/log/letsencrypt/letsencrypt.log Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): example@email.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.6-August-18-2025.pdf. You must agree in order to register with the ACME server. Do you agree? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: Y - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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: N Account registered. Requesting a certificate for yesxyz.kr and www.yesxyz.kr Successfully received certificate. Certificate is saved at: /etc/letsencrypt/live/yesxyz.kr/fullchain.pem Key is saved at: /etc/letsencrypt/live/yesxyz.kr/privkey.pem This certificate expires on 2026-06-16. These files will be updated when the certificate renews. Certbot has set up a scheduled task to automatically renew this certificate in the background. Deploying certificate Successfully deployed certificate for yesxyz.kr to /etc/nginx/sites-enabled/default Successfully deployed certificate for www.yesxyz.kr to /etc/nginx/sites-enabled/default Congratulations! You have successfully enabled HTTPS on https://yesxyz.kr and https://www.yesxyz.kr - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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/nginx/sites-enabled/default 파일의 내용 중 return 301(HTTP 상태 코드)은 접속한 호스트를 지정한 주소로 영구 이동하라는 코드입니다 ubuntu@vm4wordpress:~$ vi /etc/nginx/sites-enabled/default if ($host = yesxyz.kr) { return 301 https://$host$request_uri; } # managed by Certbot if ($host = www.yesxyz.kr) { return 301 https://$host$request_uri; } # managed by Certbot - 워드프레스의 관리 페이지에 접속하여 워드프레스 주소와 사이트 주소를 http에서 https로 변경(메뉴: 설정 -> 일반)
- (선택사항)HTTPS 환경으로 최적화
인증서를 설치해도 브라우저 주소창에 “주의 요함”이나 “회색 자물쇠”가 뜨는 경우가 있습니다. 이는 HTML은 HTTPS인데, 그 안에 포함된 이미지나 스크립트 주소가 여전히 http://로 남아있기 때문입니다.- Really Simple SSL 플러그인 사용
Really Simple SSL 플러그인은 페이지가 로드될 때 모든 http:// 링크를 실시간으로 https://로 치환하여 “안전함(녹색 자물쇠)” 상태를 강제로 유지합니다. - wp-cli를 사용하여 http://를 https://으로 치환
-- wp-cli 설치 및 권한 부여 ubuntu@vm4wordpress:~$ curl -o wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar ubuntu@vm4wordpress:~$ chmod +x wp; sudo mv wp /usr/local/bin/ ubuntu@vm4wordpress:~$ wp --info OS: Linux 6.17.0-1009-oracle #9~24.04.1-Ubuntu SMP Sat Mar 7 01:19:30 UTC 2026 x86_64 Shell: /bin/bash PHP binary: /usr/bin/php8.3 PHP version: 8.3.6 php.ini used: /etc/php/8.3/cli/php.ini MySQL binary: MySQL version: SQL modes: WP-CLI root dir: phar://wp-cli.phar/vendor/wp-cli/wp-cli WP-CLI vendor dir: phar://wp-cli.phar/vendor WP_CLI phar path: phar:///usr/local/bin/wp WP-CLI packages dir: WP-CLI cache dir: /home/ubuntu/.wp-cli/cache WP-CLI global config: WP-CLI project config: WP-CLI version: 2.12.0 -- wordpress 설치 디렉토리로 이동 후 wp 명령어 실행 ---- --dry-run 옵션을 사용해서 변경 대상 확인 ubuntu@vm4wordpress:/var/www/yesxyz$ wp search-replace 'http://yesxyz.kr' 'https://yesxyz.kr' --dry-run --precise .... | wp_users | display_name | 0 | PHP | +------------------+-----------------------+--------------+------+ Success: 4821 replacements to be made. -- --dry-run 옵션을 제거한 후 wp 명령어를 실행해서 'http://yesxyz.kr'를 'https://yesxyz.kr'로 변경 ubuntu@vm4wordpress:/var/www/yesxyz$ wp search-replace 'http://yesxyz.kr' 'https://yesxyz.kr' --precise | wp_users | display_name | 0 | PHP | +------------------+-----------------------+--------------+------+ Success: Made 4822 replacements.
- Really Simple SSL 플러그인 사용
- crontab을 사용하여 인증서 자동 갱신 등록
-- certbot renew는 실행된다고 무조건 갱신하는 게 아니라, 만료가 30일 이내로 남았을 때만 실제로 작업을 수행합니다. ---- 하루에 두 번 실행(3시, 15시) ---- --post-hook "systemctl reload nginx" : 인증서가 실제로 갱신되었을 때만 Nginx를 재실행 ---- /var/log/certbot-out.log 2>&1 : 오류 메시지까지 /var/log/certbot-out.log 파일에 기록 ubuntu@vm4wordpress:~$ sudo crontab -e 00 03,15 * * * certbot renew --post-hook "systemctl reload nginx" >> /var/log/certbot-out.log 2>&1
