(15)AWS(아마존 웹서비스) 홈페이지 만들기 – CentOS7(Linux7) snap 사용 무료SSL증명서(Let’s Encrypt)작성 스케줄러(cron)에 등록하기

웹서비스구축

과거 certbot-auto사용하여 SSL증명서 Let’s Encrypt를 작성 , 스케줄러(cron)에 등록하는 다음 글의 방법은 더 이상 유효하지 않게 되었습니다.

눈깨비 정보창고 - 분기탱천
마우스 영역 지정 이미지 캡처를 문자 인식 및 번역하기 버튼 클릭 만으로  을 실행 해 주는 소프트웨어를 작성 했습니다. 자세한 도움말은 여기를 참조 하십시오. 웹 서비스 구축 눈깨비가 본업으로 하고 있는 기술적인 ...

새로운 certbotsnap을 사용하도록 변경되어 있습니다. 그래서 이 글에서는 github버전의 certbot를 snap버전의 certbot로 마이그레이션하는 방법을 소개하겠습니다.

“내 소유의 홈페이지 사이트 만들기” 전체 설치 과정에서 이 글의 위치는 다음과 같습니다.

  • AWS 유저등록 -> EC2 인스턴스 등록 -> EC2 인스턴스 확인 ->PHP7설치 -> Apache HTTP Server설치 -> firewalld,SELinux정지 -> PHP.ini수정 -> Apache HTTP + PHP 연결 -> MySQL설치 -> WordPress용 DB작성 -> WordPress설치 -> 도메인 구입 -> Public IP구입 -> Public 도메인,IP의 DNS등록 -> 홈페이지 URL 변경 -> ssl증명서 작성(https용) -> ssl증명서 갱신 스케줄 등록 -> ssh접속 타임아웃 변경-> 가장메모리 증설 -> 타임존 변경

참고로 이 글은 AWS 웹 서비스 환경 작성을 위한 글 목록의 일부이지만 제공 업체(Azuru,GCP등)가 다르더라도 Linux 7 운영 체제 라면 이 글의 내용을 적용 할 수 있습니다.

실행 환경

이 글의 실행 환경은 다음과 같습니다.

$ cat redhat-release
CentOS Linux release 7.7.1908 (Core)
$ python --version
Python 2.7.5

주의) python3으로 버전 업을 했다면 OS의 패키지 관리자 yum을 실행할 때 오류가 발생 할 수 있습니다.

$ python --version
Python 3.6.8
$ yum list installed
  File "/usr/bin/yum", line 30
    except KeyboardInterrupt, e:
                            ^
SyntaxError: invalid syntax

이 경우 sudo update-alternatives –config python으로 Python 2.7.5로 변경 후에 실행 해야 합니다.

이전 certbot 패키지 제거하기

지금까지 certbot 패키지를 설치한 적이 없다면 이 장은 실행 하지 않습니다..

yum명령으로 기존 패키지 제거

yum명령으로 install했던 Certbot 패키지는 yum remove명령을 사용해 제거합니다.

$ sudo yum remove certbot

수동 제거 방법

눈깨비 사이트처럼  certbot-auto 스크립트를 이용하여 certbot를 설정 했다면 다음과 같은 절차로 삭제 합니다.

1. 설정 확인

crontab의 현재 설정을 확인 합니다.

$ cat /etc/crontab
... 중간생략 ...
00 03 * * 2 root /usr/bin/certbot-auto renew --post-hook "systemctl restart httpd.service"

2. 스케줄러 삭제

/etc/crontab에서 다음과 같이 certbot-auto행을 삭제합니다.

$ sudo sed -i '/certbot-auto/d' /etc/crontab
$ cat  /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week(0-6) (Sunday=0 or 7) OR sun,mon,tue ...
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

3. 스크립크 삭제

certbot-auto 스크립트를 삭제 합니다.

$ sudo rm /usr/bin/certbot-auto

4.관련 리소스 삭제

certbot-auto를 실행해서 설치된 리소스를 삭제 합니다.

sudo rm -rf /opt/eff.org

snapd 설치하기

EPEL 패키지 저장소(Repository) 추가 또는 갱신하기

다음 명령을 사용하여 EPEL 패키지 저장소(Repository)를 Linux7 운영 체제 시스템에 추가합니다.

$ sudo yum install epel-release

이전에 이미 추가 한 적 이 있다면 최신 상태임을 확인하기 위해 epel-release를 갱신합니다. 아래 결과는 갱신할 내용이 없을 때 출력 되는 메시지입니다.

$ sudo yum update epel-release
...중간생략...
No packages marked for update

snapd 패키지 설치

여기서 한 가지 주의 해야 할 점으로 다음 과 같이 ‘yum repolist enabled’ 명령을 실행해서 epel패키지 저장소(Repository)가 유효 상태임을 확인 해야 합니다. epel이 비활성 상태인 경우 snapd 설치 명령이 달라집니다.

$ yum repolist enabled
...중간생략...
base/7/x86_64 CentOS-7 - Base 10,072
extras/7/x86_64 CentOS-7 - Extras 453
ius/x86_64 IUS for Enterprise Linux 7 - x86_64 460
!epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64   13,543
...중간생략...

epel패키지 저장소(Repository)가 유효 상태인 경우

다음 명령을 수행하여 snapd 패키지를 설치 합니다.

$ sudo yum install snapd

epel패키지 저장소(Repository)가 비활성 상태인 경우

다음 명령을 수행하여 snapd 패키지를 설치 합니다. 눈깨비 사이트는 이것으로 실행했습니다.

$ sudo yum install snapd --enablerepo=epel
...중간생략...
Installed:
  snapd.x86_64 0:2.49-1.el7
Dependency Installed:
  bash-completion.noarch 1:2.1-8.el7
...중간생략...
  squashfuse-libs.x86_64 0:0.1.102-1.el7
Dependency Updated:
  selinux-policy.noarch 0:3.13.1-268.el7_9.2
  selinux-policy-targeted.noarch 0:3.13.1-268.el7_9.2
Complete!

systemd 활성화

일단 설치 후 snap 메인 통신 소켓을 관리하는 systemd를 활성화 해야 합니다.

$ sudo systemctl enable --now snapd.socket
Created symlink from /etc/systemd/system/sockets.target.wants/snapd.socket to /usr/lib/systemd/system/snapd.socket.

기존의 snap 활성화

기존의 snap 지원을 활성화하려면 다음을 수행하여 /var/lib/snapd/snap 와 /snap 사이의 symbolic링크를 만듭니다.

$ sudo ln -s /var/lib/snapd/snap /snap

snapd 버전 확인

다음 명령을 단계적으로 수행하여 최신 버전의 snapd를 사용하고 있는지 확인합니다.

$ sudo snap install core
2021-03-02T17:06:59+09:00 INFO Waiting for automatic snapd restart...
core 16-2.48.2.1 from Canonical✓ installed

$ sudo snap refresh core
snap "core" has no updates available

certbot 설치

다음 명령을 실행하여 Certbot을 설치합니다.

$ sudo snap install --classic certbot
certbot 1.12.0 from Certbot Project (certbot-eff✓) installed

$ ls -la /snap/bin/certbot
lrwxrwxrwx. 1 root root 13 Mar  2 17:14 /snap/bin/certbot -> /usr/bin/snap

certbot명령 사용 준비

certbot 명령을 시스템에서 실행 할 수 있도록 준비 합니다.

$ sudo ln -s /snap/bin/certbot /usr/bin/certbot

$ ls -la  /usr/bin/certbot
lrwxrwxrwx. 1 root root 17 Mar  2 17:16 /usr/bin/certbot -> /snap/bin/certbot

다음 명령을 실행하여 버전을 확인합니다.

$ sudo certbot --version 
certbot 1.12.0

인증서를 생성하기

Apache 구성을 자동으로 변경 하려면 다음 명령을 실행하십시오.

$ sudo certbot --apache
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache

Which names would you like to activate HTTPS for?
----------------------------------------
1: tech.zinnunkebi.com
----------------------------------------
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel):
Cert not yet due for renewal

You have an existing certificate that has exactly the same domains or certificate name you requested and isn't close to expiry.
(ref: /etc/letsencrypt/renewal/tech.zinnunkebi.com.conf)

What would you like to do?
----------------------------------------
1: Attempt to reinstall this existing certificate
2: Renew & replace the certificate (may be subject to CA rate limits)
----------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):

Apache 구성을 직접 변경 하려면 다음 명령을 실행하십시오.

$ sudo certbot certonly --apache
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache

Which names would you like to activate HTTPS for?
----------------------------------------
1: tech.zinnunkebi.com
----------------------------------------
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1
Cert not yet due for renewal

You have an existing certificate that has exactly the same domains or certificate name you requested and isn't close to expiry.
(ref: /etc/letsencrypt/renewal/tech.zinnunkebi.com.conf)

What would you like to do?
----------------------------------------
1: Keep the existing certificate for now
2: Renew & replace the certificate (may be subject to CA rate limits)
----------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 1

cron 스케줄러 등록 전 자동 업데이트 모의 테스트

–dry-run” 옵션을 사용한 다음 명령으로 자동 업데이트에 등록하기 전에 모의 테스트를 실행 할 수 있습니다.

$ sudo certbot renew --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log
----------------------------------------
Processing /etc/letsencrypt/renewal/tech.zinnunkebi.com.conf
----------------------------------------
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator webroot, Installer None
Simulating renewal of an existing certificate for tech.zinnunkebi.com
Performing the following challenges:
http-01 challenge for tech.zinnunkebi.com
Using the webroot path /var/www/wordpress for all unmatched domains.
Waiting for verification...
Cleaning up challenges
---------------------------------------
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/tech.zinnunkebi.com/fullchain.pem
----------------------------------------
----------------------------------------
Processing /etc/letsencrypt/renewal/zinnunkebi.com.conf
----------------------------------------
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator webroot, Installer None
Simulating renewal of an existing certificate for zinnunkebi.com and tech.zinnunkebi.com
Performing the following challenges:
http-01 challenge for zinnunkebi.com
http-01 challenge for tech.zinnunkebi.com
Using the webroot path /var/www/wordpress for all unmatched domains.
Waiting for verification...
Cleaning up challenges
----------------------------------------
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/zinnunkebi.com/fullchain.pem
----------------------------------------
----------------------------------------
Congratulations, all simulated renewals succeeded:
  /etc/letsencrypt/live/tech.zinnunkebi.com/fullchain.pem (success)
  /etc/letsencrypt/live/zinnunkebi.com/fullchain.pem (success)
----------------------------------------
Running post-hook command: systemctl restart httpd.service

유효 기간이 만료되기 전에 자동으로 업데이트를 하고 싶다면 다음 3개 설정 중 하나에 스케줄을 등록해야 이용 할 수 있습니다.

  • /etc/crontab
  • /etc/cron.*/*
  • systemctl list-timers

다음은 유효 기간 만료까지 30일이상 남았을 경우 certbot renew 실행 예 입니다.  유효 기간 만료까지 30일 이상 남아 있으면 skipped 되어 인증서를 갱신하지 않습니다.

$ sudo certbot renew
Saving debug log to /var/log/letsencrypt/letsencrypt.log
----------------------------------------
Processing /etc/letsencrypt/renewal/tech.zinnunkebi.com.conf
----------------------------------------
Cert not yet due for renewal
----------------------------------------
Processing /etc/letsencrypt/renewal/zinnunkebi.com.conf
----------------------------------------
Cert not yet due for renewal
----------------------------------------
The following certificates are not due for renewal yet:
  /etc/letsencrypt/live/tech.zinnunkebi.com/fullchain.pem expires on 2021-04-19 (skipped)
  /etc/letsencrypt/live/zinnunkebi.com/fullchain.pem expires on 2021-04-19 (skipped)
No renewals were attempted.
----------------------------------------

cron 스케줄러 등록 / 수정 하기

다음과 같은 /etc/crontab의 추가는 매주 화요일 AM 3:00에 증명서 자동 갱신 명령을 실행 합니다. 증명서는 유효 기간 만료까지 30일을 남지 않을 경우에만 갱신 됩니다.

$ sudo vi /etc/crontab
... 중간 생략 ...
00 03 * * 2 root /usr/bin/certbot renew --post-hook "systemctl restart httpd.service

$ cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue ...
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
00 03 * * 2 root /usr/bin/certbot renew --post-hook "systemctl restart httpd.service"

cron스케줄러 등록 후 내용을 반영하기 위해서는 crond 서비스를 재 기동합니다.

$ sudo systemctl restart crond.service

자동 암화화접속 변환(HTTP를 HTTPS로 리다이렉트)

강제적으로 보안 통신(암호화)으로 전환하려면 리다이렉트 방식을 사용하여 사용자의 http 접속 요청을 https 접속으로 바꿔야 합니다. 이 설정으로 웹사이트의 url이 http://로 접속하더라도 https://로 재접속되어 보안 경고가 보이지 않게 됩니다. 다음과 같이 https가 아닐 경우 https에 301 리다이렉트를 지시하도록 .htaccess를 작성합니다.

$ sudo vi /var/www/wordpress/.htaccess
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

AWS 웹 서비스 환경 구축 전체 목록

이 글은 AWS 웹 서비스 환경 작성을 위한 전체 절차의 한 부분입니다. 전체 목록은 다음을 참조하십시오.

AWS EC2 웹 서비스(Wordpress) 구축 전체 글 목록
EC2(Elastic Compute Cloud) 웹 서비스 구축 전체 글 워드프레스 설치 , 환경 설정 전체 글

다음 글

(16)AWS(아마존 웹서비스) 홈페이지 만들기 - ssh접속 갑자기 끊기는 현상 "타임 아웃" 방지하기
클라이언트 컴퓨터에서 ssh 프로토콜 소프트웨어(예:PuTTY.exe) 를 사용하여 서버에 접속해서 일정 시간 아무것도 하지 않고 방치한다면 "ssh 클라이언트 프로그램" 과 "sshd 서비스" 중 어느 한 곳에서 ...
제목과 URL을 복사했습니다