Ubuntu에서 Let's Encrypt(Certbot)으로 SSL인증서 발급하기
ubuntu 20.04 LTS
기존 Certbot 제거
$ sudo apt remove certbot
Certbot 설치
$ sudo snap install --classic certbot
snap은 ubuntu 16.04.4, 18.04, 20.04 버전을 사용한다면 기본적으로 설치되어 있다.
Frontend
SSL 인증서 발급 후 웹서버 설정 업데이트
nginx가 설치되어 있다고 가정한다.
$ sudo certbot --nginx
nginx 설정 파일 (/etc/nginx/sites-available/default)
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/ubuntu/S06P31C103/Frontend/build;
# index index.html index.htm index.nginx-debian.html;
index index.html;
server_name _;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
server_name withus.ssafy.io; # managed by Certbot
location / {
root /home/ubuntu/S06P31C103/Frontend/build;
index index.html;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/withus.ssafy.io/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/withus.ssafy.io/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = withus.ssafy.io) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 ;
listen [::]:80 ;
server_name withus.ssafy.io;
return 404; # managed by Certbot
}
root 페이지는 잘 동작하지만 하위 페이지에서 404 Not Found nginx/1.16.0과 같은 에러가 발생할 경우
위의 설정파일처럼 location 안의 try_files를 변경 해 주어야 한다.
nginx 실행 명령어
# nginx 실행
$ sudo service nginx start
# nginx 재 실행
$ sudo service nginx restart
# nginx 상태 보기
$ sudo service nginx status
Backend (Springboot + Gradle)
certbot을 이용해 발급받은 키(.pem)를 openssl을 이용해 .p12파일로 변경
$ cd
# 생성된 .p12 파일을 저장할 경로를 미리 복사하기 위해 springboot 프로젝트의 src/main/resource 폴더로 이동
$ cd /project/src/main/resources
# pwd의 결과를 복사해둔다
$ pwd
/home/ubuntu/project/src/main/resources
$ cd
# live 폴더에 접근하기 위해 관리자 권한으로 변경
$ sudo su
# key가 존재하는 디렉토리로 이동
$ cd /etc/letsencrypt/live/<인증서 발급 시 설정한 도메인 폴더>/
# pem파일을 이용해 p12파일 생성 명령어 입력 시 비밀번호를 설정한다.
$ openssl pkcs12 -export -inkey privkey.pem -in cert.pem -out /<resources 폴더경로>/keystore.p12
# 만약 pem 파일을 백업해두고 싶다면 아래 command를 입력한다.
$ cp -r * <resources 폴더 경로>
# resources 폴더로 돌아가 잘 생성되었는지 확인해본다.
# 관리자권한 종료
$ exit
application.yml 설정
server:
ssl :
key-store: classpath:keystore.p12
key-store-password: <설정한 비밀번호>
key-store-type: PKCS12
이제 실행하면 잘 backend의 프로토콜이 https로 변경된다.
Postman으로 테스트
postman의 settings에 들어가 위와 같이 SSL certificate verification 을 off 로 변경 하고 http => https로 변경해 요청을 보내면 응답을 확인 할 수 있다.
(필요 시) Git에 인증서를 올리려고 할 경우
그냥 시도할 경우 대충 아래와 같은 에러가 발생한다
$ git add .
error: open("~~~/keystore.p12"): Permission denied
error: unable to index file ~~~/keystore.p12
fatal: adding files failed
# sudo로 실행하려고 할 경우
$ sudo git add .
fatal: unsafe repository('/home/ubuntu/project/' is owned by someone else)
To add an exception for this directory, call:
git config --global --add safe.directory /home/ubuntu/project/
# 대강 위와같은 에러가 발생한다. 하지만 safe.directory에 추가하더라도 add가 되지 않는다.
keystore.p12에 권한 부여
# resource 폴더 내에서
$ sudo chmod 755 keystore.p12
위 명령어를 실행하면 git add . 명령어가 잘 동작한다.