얕고넓은지식/linux

rocky linux 8에...ssl 인증서 https 가능하게 만들어보자 Let's Encrypt SSL 무료 인증서로 Apache

쪽마 2021. 12. 19. 02:39
반응형

앞서...먼저..한방에 설치까지 다 따라하고난뒤에 오면좋다

https://zzokma.tistory.com/1693

 

rocky linux 웹 서버설치 한방에 따라하기

1. 아파치설치 [root@localhost ~]# dnf install httpd -y 2. db설치 [root@localhost ~]# dnf module list mysql Rocky Linux 8 - AppStream Name           Stream           Profi..

zzokma.tistory.com

 

 

[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=http
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=https
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=80
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=443


[root@localhost ~]# setsebool -P httpd_unified 1

이 명령은 SELinux 부울 값을 업데이트하고 -P 플래그 부팅 시간 값을 업데이트하여 시스템 재부팅 시 변경 사항을 유지합니다. 전반적으로, httpd_unified SELinux가 모든 Apache(HTTPD) 프로세스를 동일한 유형으로 처리하도록 지시하는 부울 값입니다.

[root@localhost ~]# dnf install curl -y

를 할텐데...아마...

이미 전체 업데이트했거나 했으면..설치가 되어있을것

아래와 같이 확인이 될것이다...

[root@localhost ~]# dnf install curl -y
Last metadata expiration check: 1:26:19 ago on Sun 19 Dec 2021 12:56:35 AM KST.
Package curl-7.61.1-22.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!

 

 

테스트할 폴더를 생성

[root@localhost ~]# mkdir /var/www/test

 

 

앞서....

필자(?)는..

etc/httpd/conf.d/vhost.conf

파일에 가상호스트를 만들어주었다. 때문에..

아래 내용을 위 파일에 넣어준다

 

 

 

가상호스트를 만들어준다

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName example_domain
    ServerAlias www.example_domain
    DocumentRoot /var/www/test
</VirtualHost>

 

 

 

여기까지가 가상호스트 세팅이라고 보면된다.

 

 

주의

가상호스트로 여러 사이트를 운영할경우

하나를 인증받고

다시 사이트를 추가해서 받아야지

vhost.conf

이파일내애 처음부터 운영하는 모든사이트를 주루룩 나열해서

저장하면

오류뜬다..

 

오류뿜뿜~

 

 

아래부터 무료 인증서를 설치해줄것이다.

Let's Encrypt SSL 무료 인증서로 Apache

[root@localhost ~]# dnf install curl -y
Last metadata expiration check: 1:26:19 ago on Sun 19 Dec 2021 12:56:35 AM KST.
Package curl-7.61.1-22.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@localhost ~]# ^C
[root@localhost ~]# ^C
[root@localhost ~]# dnf install epel-release mod_ssl -y
Last metadata expiration check: 1:31:14 ago on Sun 19 Dec 2021 12:56:35 AM KST.
Package mod_ssl-1:2.4.37-43.module+el8.5.0+714+5ec56ee8.x86_64 is already installed.
Dependencies resolved.
================================================================================
 Package               Architecture    Version            Repository       Size
================================================================================
Installing:
 epel-release          noarch          8-13.el8           extras           23 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 23 k
Installed size: 35 k
Downloading Packages:
epel-release-8-13.el8.noarch.rpm                176 kB/s |  23 kB     00:00
--------------------------------------------------------------------------------
Total                                           9.2 kB/s |  23 kB     00:02
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Installing       : epel-release-8-13.el8.noarch                           1/1
  Running scriptlet: epel-release-8-13.el8.noarch                           1/1
  Verifying        : epel-release-8-13.el8.noarch                           1/1

Installed:
  epel-release-8-13.el8.noarch

Complete!

굿...다음은 인증서를 생성해보자

[root@localhost ~]# dnf install python3-certbot-apache -y
Extra Packages for Enterprise Linux 8 - x86_64  2.7 MB/s |  11 MB     00:03
Extra Packages for Enterprise Linux Modular 8 - 114 kB/s | 980 kB     00:08
Last metadata expiration check: 0:00:02 ago on Sun 19 Dec 2021 02:28:50 AM KST.
Dependencies resolved.
================================================================================
 Package                   Arch   Version                       Repo       Size
================================================================================
Installing:
 python3-certbot-apache    noarch 1.20.0-1.el8                  epel      145 k
Installing dependencies:
 python3-acme              noarch 1.20.0-1.el8                  epel       91 k
 python3-certbot           noarch 1.20.0-1.el8                  epel      408 k
 python3-cffi              x86_64 1.11.5-5.el8                  baseos    237 k
 python3-configargparse    noarch 0.14.0-6.el8                  epel       36 k
 python3-cryptography      x86_64 3.2.1-5.el8                   baseos    558 k
 python3-distro            noarch 1.4.0-2.module+el8.3.0+120+426d8baf
                                                                appstream  36 k
 python3-josepy            noarch 1.9.0-1.el8                   epel      103 k
 python3-parsedatetime     noarch 2.5-1.el8                     epel       79 k
 python3-pyOpenSSL         noarch 19.0.0-1.el8                  appstream 102 k
 python3-pycparser         noarch 2.14-14.el8                   baseos    108 k
 python3-pyrfc3339         noarch 1.1-1.el8                     epel       19 k
 python3-requests-toolbelt noarch 0.9.1-4.el8                   epel       91 k
 python3-zope-component    noarch 4.3.0-8.el8                   epel      313 k
 python3-zope-event        noarch 4.2.0-12.el8                  epel      210 k
 python3-zope-interface    x86_64 4.6.0-1.el8                   epel      158 k
Installing weak dependencies:
 certbot                   noarch 1.20.0-1.el8                  epel       53 k
 python-josepy-doc         noarch 1.9.0-1.el8                   epel       23 k

Transaction Summary
================================================================================
Install  18 Packages

Total download size: 2.7 M
Installed size: 11 M
Downloading Packages:
(1/18): python3-cffi-1.11.5-5.el8.x86_64.rpm    1.1 MB/s | 237 kB     00:00
(2/18): python3-cryptography-3.2.1-5.el8.x86_64 8.8 MB/s | 558 kB     00:00
(3/18): python3-pycparser-2.14-14.el8.noarch.rp 1.1 MB/s | 108 kB     00:00
(4/18): python3-pyOpenSSL-19.0.0-1.el8.noarch.r 110 kB/s | 102 kB     00:00
(5/18): python3-distro-1.4.0-2.module+el8.3.0+1  39 kB/s |  36 kB     00:00
(6/18): python-josepy-doc-1.9.0-1.el8.noarch.rp  22 kB/s |  23 kB     00:01
(7/18): certbot-1.20.0-1.el8.noarch.rpm          32 kB/s |  53 kB     00:01
(8/18): python3-acme-1.20.0-1.el8.noarch.rpm     59 kB/s |  91 kB     00:01
(9/18): python3-certbot-apache-1.20.0-1.el8.noa 220 kB/s | 145 kB     00:00
(10/18): python3-configargparse-0.14.0-6.el8.no  76 kB/s |  36 kB     00:00
(11/18): python3-josepy-1.9.0-1.el8.noarch.rpm  286 kB/s | 103 kB     00:00
(12/18): python3-certbot-1.20.0-1.el8.noarch.rp 319 kB/s | 408 kB     00:01
(13/18): python3-parsedatetime-2.5-1.el8.noarch 171 kB/s |  79 kB     00:00
(14/18): python3-pyrfc3339-1.1-1.el8.noarch.rpm  51 kB/s |  19 kB     00:00
(15/18): python3-requests-toolbelt-0.9.1-4.el8. 281 kB/s |  91 kB     00:00
(16/18): python3-zope-event-4.2.0-12.el8.noarch 553 kB/s | 210 kB     00:00
(17/18): python3-zope-interface-4.6.0-1.el8.x86 504 kB/s | 158 kB     00:00
(18/18): python3-zope-component-4.3.0-8.el8.noa 382 kB/s | 313 kB     00:00
--------------------------------------------------------------------------------
Total                                           288 kB/s | 2.7 MB     00:09
Extra Packages for Enterprise Linux 8 - x86_64  1.6 MB/s | 1.6 kB     00:00
Importing GPG key 0x2F86D6A1:
 Userid     : "Fedora EPEL (8) <epel@fedoraproject.org>"
 Fingerprint: 94E2 79EB 8D8F 25B2 1810 ADF1 21EA 45AB 2F86 D6A1
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Installing       : python3-zope-event-4.2.0-12.el8.noarch                1/18
  Installing       : python3-zope-interface-4.6.0-1.el8.x86_64             2/18
  Installing       : python3-pyrfc3339-1.1-1.el8.noarch                    3/18
  Installing       : python3-zope-component-4.3.0-8.el8.noarch             4/18
  Installing       : python3-requests-toolbelt-0.9.1-4.el8.noarch          5/18
  Installing       : python3-parsedatetime-2.5-1.el8.noarch                6/18
  Installing       : python3-configargparse-0.14.0-6.el8.noarch            7/18
  Installing       : python-josepy-doc-1.9.0-1.el8.noarch                  8/18
  Installing       : python3-pycparser-2.14-14.el8.noarch                  9/18
  Installing       : python3-cffi-1.11.5-5.el8.x86_64                     10/18
  Installing       : python3-cryptography-3.2.1-5.el8.x86_64              11/18
  Installing       : python3-pyOpenSSL-19.0.0-1.el8.noarch                12/18
  Installing       : python3-josepy-1.9.0-1.el8.noarch                    13/18
  Installing       : python3-acme-1.20.0-1.el8.noarch                     14/18
  Installing       : python3-distro-1.4.0-2.module+el8.3.0+120+426d8baf   15/18
  Installing       : python3-certbot-1.20.0-1.el8.noarch                  16/18
  Installing       : certbot-1.20.0-1.el8.noarch                          17/18
  Running scriptlet: certbot-1.20.0-1.el8.noarch                          17/18
  Installing       : python3-certbot-apache-1.20.0-1.el8.noarch           18/18
  Running scriptlet: python3-certbot-apache-1.20.0-1.el8.noarch           18/18
  Verifying        : python3-distro-1.4.0-2.module+el8.3.0+120+426d8baf    1/18
  Verifying        : python3-pyOpenSSL-19.0.0-1.el8.noarch                 2/18
  Verifying        : python3-cffi-1.11.5-5.el8.x86_64                      3/18
  Verifying        : python3-cryptography-3.2.1-5.el8.x86_64               4/18
  Verifying        : python3-pycparser-2.14-14.el8.noarch                  5/18
  Verifying        : certbot-1.20.0-1.el8.noarch                           6/18
  Verifying        : python-josepy-doc-1.9.0-1.el8.noarch                  7/18
  Verifying        : python3-acme-1.20.0-1.el8.noarch                      8/18
  Verifying        : python3-certbot-1.20.0-1.el8.noarch                   9/18
  Verifying        : python3-certbot-apache-1.20.0-1.el8.noarch           10/18
  Verifying        : python3-configargparse-0.14.0-6.el8.noarch           11/18
  Verifying        : python3-josepy-1.9.0-1.el8.noarch                    12/18
  Verifying        : python3-parsedatetime-2.5-1.el8.noarch               13/18
  Verifying        : python3-pyrfc3339-1.1-1.el8.noarch                   14/18
  Verifying        : python3-requests-toolbelt-0.9.1-4.el8.noarch         15/18
  Verifying        : python3-zope-component-4.3.0-8.el8.noarch            16/18
  Verifying        : python3-zope-event-4.2.0-12.el8.noarch               17/18
  Verifying        : python3-zope-interface-4.6.0-1.el8.x86_64            18/18

Installed:
  certbot-1.20.0-1.el8.noarch
  python-josepy-doc-1.9.0-1.el8.noarch
  python3-acme-1.20.0-1.el8.noarch
  python3-certbot-1.20.0-1.el8.noarch
  python3-certbot-apache-1.20.0-1.el8.noarch
  python3-cffi-1.11.5-5.el8.x86_64
  python3-configargparse-0.14.0-6.el8.noarch
  python3-cryptography-3.2.1-5.el8.x86_64
  python3-distro-1.4.0-2.module+el8.3.0+120+426d8baf.noarch
  python3-josepy-1.9.0-1.el8.noarch
  python3-parsedatetime-2.5-1.el8.noarch
  python3-pyOpenSSL-19.0.0-1.el8.noarch
  python3-pycparser-2.14-14.el8.noarch
  python3-pyrfc3339-1.1-1.el8.noarch
  python3-requests-toolbelt-0.9.1-4.el8.noarch
  python3-zope-component-4.3.0-8.el8.noarch
  python3-zope-event-4.2.0-12.el8.noarch
  python3-zope-interface-4.6.0-1.el8.x86_64

Complete!

 

 

뭐를 많이 설치한다..

긴장하지말고 다음으로...

[root@localhost ~]# certbot --apache --agree-tos --redirect --hsts --staple-ocsp --email aaa@zzokma.tistory.com -d zzokma.tistory.com

 

 

아래 명령어로 실제 갱신이 아니라 잘 갱신되는지, 명령에 오류가 나진 않는지 등을 테스트.

[root@localhost ~]# certbot renew --dry-run

실제로 갱신하기는...

--dry-run만 빼고 실행

[root@localhost ~]# certbot renew





인증서 만료일 확인
Certbot으로 부터 발급받은 인증서들에 대한 정보를 표시

[root@localhost ~]# certbot certificates

 

 

 

그럼끝~!

이아니고...

크론탭에 자동으로 갱신하게 설정해주자

먼저...크론탭이뭐냐?

자동으로 리눅스 내의 파일을 실행할수있게해주는

오토라보면된다.

자주쓰는 명령 3개

Crontab 보기
[root@localhost ~]# crontab -l

Crontab 편집
[root@localhost ~]# crontab -e

Crontab 실행 로그
[root@localhost ~]# view /var/log/syslog

 

 

 

 

위 그림처럼 분, 시, 일, 월, 요일, 명령 순서로 기재하면 됩니다.
예를 들어, /home/user/run.sh를 실행하고 싶다면

- 매 시 10분에 
10 * * * * /home/test/test.sh

- 10분 마다 주기적으로 
*/10 * * * * /home/test/test.sh

- 토요일 새벽 3시에
0 3 * * 6 /home/test/test.sh

참고: 요일은 0~7 숫자중 하나를 지정할 수 있는데, 1은 월요일이고 0과 7은 둘다 일요일

실행할 명령어는 한 줄에 여러개 등록 가능

각 단위는 스페이스바 한칸으로 구분

 

Let’s Encrypt측에서는 갱신시도가 실패할 것을 고려하여 

하루 두번씩 갱신 명령어를 실행시키라고 권장.

그래서 시간스케줄을 하루 두 번으로 하기위해서

처음 두개, 분 단위는 9로 시간단위는 1,7으로설정

03시 0분과 5시 0분에 같은 명령어를 실행 해주는 것

ex : 0 3,5 * * * /usr/bin/certbot renew --renew-hook="systemctl restart httpd"

추가 설명

요일은 0~7 까지 사용 (0:일요일,~ 6:토요일)
월요일, 수요일 하고자 할때  1,3
월요일에서 금요일 하고자 할때 1-5


example1] 매주 토요일 12:00에 oracle 계정의 디렉터리를 백업하자.
00 12 * * 6 /home/oracle/backup.sh

example2] 매주 수요일 오전 9:30에 /var/log에 있는 모든 로그 파일을 삭제하라.
30 09 * * 3 rm -f /var/log/*

example3] 2개월 간격으로 새벽 4시에 백업하라.
00 04 * 1-12/2 * /home/oracle/backup.sh

example4] 매일 오전 4시와 12시에 backup.sh 스크립트를 실행하자.
00 04,12 * * * /home/oracle/backup.sh > /dev/null 2>&1

example5] 매일 오전 3시에서 8시까지 매시마다 backup.sh를 실행하라.
00 3-8 * * * backup.sh

example5] 3개월마다 수요일에서 금요일까지 11:30분에 backup.sh를 실행하라
30 11 * 1-12/3 3-5 backup.sh

 

 

[root@localhost ~]# vi /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,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

00 00 */1 * * /usr/sbin/certbot-auto renew


0 3,5 * 1-12/3 * /usr/bin/certbot renew --renew-hook="systemctl restart httpd"

 

여기까지하면...

http 접속시 https 로 전환되어 인증받는것까지 완료




핵중요


일단 무조건 www도 해줘야하고 없는것도 해줘야함...dns는 안되고...내부서버에서 .htaccess 이용해서 해줘도되지만..
.htaccess 이건 암만해도 된다는데 안되고...
그리고 .htaccess 를 이용해서 리다이렉션해줘도...기본적으로 www도 인증을 받아야함..
결론은...
그녕 무조건 www 붙은거 안붙은거 둘다 인증받아야 원활하게 이용가능

 

 

 

 

반응형