sronsemiro.tistory.com/9

 

Ubuntu 18.04.4 LTS, Nginx에 Let's Encrypt 설치 및 HTTPS적용(SSL)

1. Certbot 설치 // add repo $ sudo add-apt-repository ppa:certbot/certbot // install certbot $ sudo apt install python-certbot-nginx 2. HTTPS(SSL)을 설정할, nginx 설정 체크 // check server_name $ su..

sronsemiro.tistory.com

기존 시스템을 유지하고 있었으므로,

Ubuntu 18.04.4 / nginx 로 운영중이다.

 

예전에 작성해두었던, 위 글을 참조해서 오랜만에 SSL을 적용하려고 아래 명령어를 입력했다.

certbot --nginx -d AAA.co.kr -d www.AAA.co.kr

 

그랬더니, 생각보다 오랜 시간이 걸린 후, 아래와 같은 에러 메시지가 나왔다.

Failed authorization procedure. AAA.co.kr (http-01): urn:ietf:params:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://AAA.co.kr/.well-known/acme-challenge/96Ot7ZF9cVMwcycFhodd96TXi97sFQ9OR7Ln2sPpwN4 [183.111.125.112]: "\n<!DOCTYPE html>\n<html class=\"html\" lang=\"ko-KR\" itemscope itemtype=\"http://schema.org/WebPage\">\n<head>\n\t<meta charset=\"UTF-8\">\n", www.AAA.co.kr (http-01): urn:ietf:params:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://AAA.co.kr/.well-known/acme-challenge/YTz6-bfcu9UWltNrFoqkYxrURdasHSTSePse7oH6zyI [183.111.125.112]: "<html>\r\n<head><title>504 Gateway Time-out</title></head>\r\n<body>\r\n<center><h1>504 Gateway Time-out</h1></center>\r\n<hr><center>ng"

IMPORTANT NOTES:
 - The following errors were reported by the server:

   Domain: AAA.co.kr
   Type:   unauthorized
   Detail: Invalid response from
   http://AAA.co.kr/.well-known/acme-challenge/96Ot7ZF9cVMwcycFhodd96TXi97sFQ9OR7Ln2sPpwN4
   [183.111.125.112]: "\n<!DOCTYPE html>\n<html class=\"html\"
   lang=\"ko-KR\" itemscope
   itemtype=\"http://schema.org/WebPage\">\n<head>\n\t<meta
   charset=\"UTF-8\">\n"

   Domain: www.AAA.co.kr
   Type:   unauthorized
   Detail: Invalid response from
   http://AAA.co.kr/.well-known/acme-challenge/YTz6-bfcu9UWltNrFoqkYxrURdasHSTSePse7oH6zyI
   [183.111.125.112]: "<html>\r\n<head><title>504 Gateway
   Time-out</title></head>\r\n<body>\r\n<center><h1>504 Gateway
   Time-out</h1></center>\r\n<hr><center>ng"

   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.

이전이랑 똑같이 했는데,.. 왜 안되지..?

 

이것저것 찾아보니, 발생할 수 있는 이유는 여러가지가 있었다.

1. 해당 주소가 https로 리다이렉트 되고 있는 경우.

2. 해당 URL의 .well-known/acme-challenge/ <=== 이 경로로 접근이 안되는경우

3. 위 에러코드에도 나오듯이 제대로 된 IP주소가 아니거나 DNS 설정이 잘못된 경우 

등등..

 

근데 나는 아무것도 해당되지 않았다.

그래서 어떻게 해야 할까 고민하다가.

 

인증서만 발급받고 나머지 설정은 수동으로 하는 방법을 찾았고, 시도해봤다.

 

letsencrypt certonly --webroot --webroot-path=/var/www/AAA -d AAA.co.kr -d www.AAA.co.kr

certbot은 인증서를 발급받고, 설정까지 자동으로 해주는 반면

letsencrypt 명령어를 사용하면, 수동으로 설정해줄 수 있다. 

(이게 맞나? 정확히는 모르겠다..)

 

--webroot --webroot-path

여기서 --webroot는 인증서를 발급받을 홈페이지 소스가 있는 폴더의 경로를 적어주면 된다.

 

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for AAA.co.kr
http-01 challenge for www.AAA.co.kr
Using the webroot path /var/www/1800_7058_co_kr for all unmatched domains.
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/AAA.co.kr/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/AAA.co.kr/privkey.pem
   Your cert will expire on 2020-12-18. 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

 

명령어를 실행하면, 위와 같이 잘 되었다고 나온다.

그러면, /etc/letsencrypt/archive/AAA.co.kr 경로아래에 인증서들이 발급된 것을 확인할 수 있다.

 

그럼 이제, 이 인증서들을 이용해서, nginx에 설정해주면 된다.

server {
 ## 이 부분은 http로 접속하면, https로 리다이렉트 시켜주는 부분이다 ##
 listen 80;
 root /var/www/AAA;
 server_name AAA.co.kr www.AAA.co.kr;

 return       301 https://$server_name$request_uri;
}

server {
 listen 443 ssl http2;
 root /var/www/sbcompany_co_kr;
 server_name AAA.co.kr www.AAA.co.kr;
 index index.php;

 ## 아래 4줄이 아까 발급받은 인증서를 설정 하는 부분이다. ##
 ssl_certificate /etc/letsencrypt/live/AAA.co.kr/fullchain.pem;
 ssl_certificate_key /etc/letsencrypt/live/AAA.co.kr/privkey.pem;
 include /etc/letsencrypt/options-ssl-nginx.conf;
 ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

 modsecurity on;
 modsecurity_rules_file /etc/nginx/modsec/modsec_includes.conf;
 #modsecurity_rules_file /etc/nginx/modsec/modsecurity.conf;

 location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
     access_log        off;
     log_not_found     off;
     expires           360d;
 }

 access_log /var/log/nginx/sbcompany_co_kr/access.log;

 location / {
 try_files $uri $uri/ /index.php?q=uri&$args;
 }

 location ~ \.php$ {
 include snippets/fastcgi-php.conf;
 fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
 }

 location ~ /xmlrpc.php {
  deny all;
  return 444;
 }
}

nginx에 해당 사이트의 site설정에 위 4줄을 추가해주면 된다.

끝~~

 

서버환경

Ubuntu 18.04.4 LTS

Nginx 1.19.1

 

기존 운영 도메인

1. AAA.com

2. BBB.Com (SSL 적용완료)

3. a.CCC.com

4 b.CCC.com (SSL 적용완료)

..등등 약 30개 정도 사이트

 

위 환경의 서버에서, Letsencrypt를 이용해서, 추가로 x.CCC.com 사이트에 SSL를 설정하고 있었다.

그런데, 자꾸 인증과정에서 에러가 발생했고,

추가적으로, https://asdf.CCC.com 과 같이 현재 서버에는 설정되어 있지 않은, 주소로 접속을 시도하면, 

기존에 SSL이 설정되어 있던, https://BBB.com 으로 접속이 되는 현상이 있었다.

 

이게, 기존부터 이렇게 되었던 건지,

아니면, 어떤 이유로 발생 했는지는 모르겠다.

 

여기저기 찾아보다가, 

qastack.kr/server/578648/properly-setting-up-a-default-nginx-server-for-https

 

위의 링크를 찾게 되었고, 해결했다.

 

이유는 바로, default 파일이 없어서 발생했던거다.

또한, default 파일도, http로 접속했을때와, https로 접속했을때 둘다를 설정해주어야 한다.

 

나는 그래서 아래와 같이 설정했다.

server {
  listen 80 default_server;
  listen [::]:80 default_server;
  listen 443 default_server;
  listen [::]:443 default_server;

  root /var/www/html;
  index index.html;

  server_name _;

  ssl_certificate /etc/nginx/ssl/nginx.crt;
  ssl_certificate_key /etc/nginx/ssl/nginx.key;

  location / {
    try_files $uri $uri/ =404;
    }
}

 

이렇게 설정후, nginx를 재시작 또는 reload를 하면,

위의 설명했던 현상이 발생하지 않게 된다.

 

또, default를 설정하지 않았을 때,

없는 주소로, https 를 이용해서 접속하게 되면,

https://BBB.com이 접속되는데, 

아마 그 이유는 sites-available 안에 파일 중,

SSL이 설정된 가장 첫번째 파일의 주소로 접속되는 것 같다. (알파벳 순)

 

1. Certbot 설치

// add repo
$ sudo add-apt-repository ppa:certbot/certbot

// install certbot
$ sudo apt install python-certbot-nginx

 

2. HTTPS(SSL)을 설정할, nginx 설정 체크

// check server_name
$ sudo vi /etc/nginx/sites-available/example.com

// server_name example.com www.example.com;

 

3. nginx relaod

Certbot이 nginx 서버 블록을 확인하게 하기 위해, nginx reload

// reload nginx
$ sudo system reload nginx

 

 

4. 우분투 방화벽 (ufw)에서 https 허용

방확벽을 사용하지 않는다면, 따로 설정할 필요는 없지만,

기존에 방화벽을 사용하고 있다면, 아래 링크의 3단계를 참고해서 방화벽 설정을 진행한다.

 

참고 : https://velog.io/@pinot/Ubuntu-18.04%EC%97%90%EC%84%9C-Lets-Encrypt%EB%A5%BC-%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC-Nginx%EC%97%90-SSL%EC%9D%84-%EC%A0%81%EC%9A%A9%ED%95%98%EB%8A%94-%EB%B0%A9%EB%B2%95

 

번역) Ubuntu 18.04에서 Let's Encrypt를 사용하여 Nginx에 SSL을 적용하는 방법

우분투 환경에서 nginx에 certbot 클라이언트를 사용하여 자동적으로 SSL을 적용하는 방법에 대하여 알아보도록 합시다.

velog.io

 

5. HTTPS (SSL) 인증서 설치

$ sudo certbot --nginx -d example.com -d www.example.com

위의 명령어를 실행하면, 몇가지 과정을 진행 후, 아래와 같은 화면이 나온다.

Output
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):

// 1번은 http, https를 따로따로 접속할 수 있게 한다.
// 2번 설정은 http접속을 강제로 https로 모두 변경하므로 https접속만 할 수 있게 된다.

나는 2번을 선택했다.

원하는 번호를 선택하고, 엔터를 누르면 Nginx가 자동으로 재시작되고, 인증서가 저장된 위치를 알려주고, certbot은 종료된다.

 

6. Let's Encrypt HTTPS(SSL) 갱신

$ sudo certbot renew --dry-run

아주 간단하게, 위 명령어만 입력하면 certbot으로 진행한, HTTPS (SSL)을 모두 갱신할 수 있다.

 

Ubuntu Cron을 이용해서, 위의 갱신 명령어를 스케쥴링으로 하는 방법은 다음 포스팅에서...

 

 

참고 : https://velog.io/@pinot/Ubuntu-18.04%EC%97%90%EC%84%9C-Lets-Encrypt%EB%A5%BC-%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC-Nginx%EC%97%90-SSL%EC%9D%84-%EC%A0%81%EC%9A%A9%ED%95%98%EB%8A%94-%EB%B0%A9%EB%B2%95

+ Recent posts