본문 바로가기

Basic/네트워크

아파치와+톰캣에 SSL 적용방법

반응형

 

0) 설치 환경 ( OS: 우분투18.04, 웹서버 : 아파치2, 톰캣 9.0 )

 

1) 아파치 설정(도메인 주소 설정)

 

sudo nano /etc/apache2/sites-available/000-default.conf

 
명령어로 파일을 열어주고 아래와 같이 도메인을 설정해줬다.
 
ServerName 도메인 이름 추가 ( 예 :ServerName dokky.ga )
 

2) letsencrypt 설치

 

$ sudo apt-get update

$ sudo apt-get install letsencrypt

 

3) 아파치 STOP(나는 이 순서를 생략해도 잘 동작됬다.)

 

$ sudo service apache2 stop

SSL 인증서를 받기 위한 명령어를 쳐야 하는데,
이 명령어가 실행될 때 80포트를 사용하기 때문에 80포트를 비워줘야 한다고 한다.
아파치가 기본적으로 80포트에 물려 있기 때문에 아파치를 꺼준다.

 

 

4) 현재 사용 포트 확인(나는 이 순서를 생략해도 잘 동작됬다.)

 

sudo netstat -tnlp|grep 80 : 80포트가 있는지 확인 

sudo netstat -atlpvn : 모든 포트 확인

netstat -ntl

80포트가 꺼지면 나오지 않는다.

 

 

 

 

 

6) 인증서 발급과정

 

아래처럼 프로젝트 루트의 경로를 수동적으로 잡아서 발급을 해줬다.

 

letsencrypt certonly --webroot --webroot-path=웹루트 -d 도메인

 

예 : letsencrypt certonly --webroot --webroot-path=/var/lib/tomcat9/webapps -d dokky.site

 

certonly 옵션을 주는 이유는 certbot보고 인증서 발급만 하고 다른 아파치 설정은 건드리지 말라

 

는 뜻이다.  certonly를 빼고 명령을 실행하면 예상치 못한 일이 생길 수 있으니 주의

 

--

debug : 명령어에 이렇게 debug추가해주면 에러 로그도 나온다.

 

이때 톰캣의 server.xml 에 <!--<Context docBase="MyPortFolio" path="/" reloadable="true" />--> 주석처리해야함

그리고 아래설정을 진행 해준다

 

- 이메일 입력

 

1
2
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): test@gmail.com
cs

 

- 이용 약관 동의 (A:동의)

 

1
2
3
4
5
6
7
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: a
 
cs

 

- Let’s Encrypt의 뉴스레터 수신 여부 (Y:동의 N:거부) - (선택사항) 거부해도 상관없으니 원하는 대로

 

1
2
3
4
5
Would you be willing 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
 
cs

 

7) 발급 완료 메시지

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/dokky.ga/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/dokky.ga/privkey.pem
   Your cert will expire on 2020-02-22. 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
 
 
cs

 

8) 인증서파일 발급 위치

 

완료되면 위와 같이 키가 발급된 위치가 나온다. 

 

경로를 따라가면 인증서 파일 4개가 발급되어있다.

 

cert.pem : 인증서 파일.
chain.pem : 인증서 발급자 파일.
fullchain.pem : cert.pem과 chain.pem을 하나로 합쳐놓은 파일.
privkey.pem : 개인키 파일.

 

1
2
3
/etc/letsencrypt/live/dokky.ga# ls
README  cert.pem  chain.pem  fullchain.pem  privkey.pem
 
 
cs

 

 

9) https 서비스를 위한 가상 호스트를 아파치에 설정

 

SSL 환경설정 파일 열기

sudo nano /etc/apache2/sites-available/default-ssl.conf

 

아래 부분들을 추가해준다.

 

 
2
3
4
5
6
7
ServerName dokky.ga ServerAdmin 이메일 ServerAlias dokky.ga #DocumentRoot /var/www/html DocumentRoot /var/lib/tomcat9/webapps/
JkMount /* tomcat1
 
SSLEngine on
 
SSLCertificateFile “/etc/letsencrypt/live/[도메인주소]/cert.pem”
 
SSLCertificateKeyFile “/etc/letsencrypt/live/[도메인주소]/privkey.pem”
 
SSLCertificateChainFile “/etc/letsencrypt/live/[도메인주소]r/chain.pem”
cs

 

이처럼 아파치 설정에 인증서의 위치를 잡아주면 아파치 서버로 요청이 들어올시 SSL이 적용되어 인증서가 확인이 된다.

현재 ROOT 라는 이름으로 프로젝트 배포하기 때문에 아래와 같이 경로 변경했음 DocumentRoot /var/lib/tomcat9/webapps/ROOT/

10) Apache 웹서버의 SSL 가상호스트 사이트 설정 활성화

 

#a2ensite default-ssl.conf (비활성: a2dissite default-ssl.conf)

 

1
2
3
Enabling site default-ssl.
To activate the new configuration, you need to run:
  systemctl reload apache2
cs

11) Apache 웹서버의 SSL 모듈 활성화

 

sudo a2enmod ssl( 비활성 : sudo a2dismod ssl )

 

12) 방화벽 설정

 

 - 호스팅 사이트 방화벽 확인

aws 사이트에서 방화벽이 열려있는지 확인한다  443
 
 

 

 
영체제에도 방화벽이 있다. 기본적으로 꺼져있으나 방화벽을 켠 다음 원하는 포트만 열겠다.
 
운영체제 방화벽에 대해 신경쓰고싶지 않으면 이부분은 건들지 않으면 된다.
 
나는 이 순서를 생략해도 잘 동작됬다.
 
1
2
3
4
5
6
7
8
9
10
# 방화벽 상태보기
$ sudo ufw status verbose
 
# 방화벽 열기
$ sudo ufw allow http
$ sudo ufw allow https
$ sudo ufw allow ssh
 
# 방화벽 활성화
$ sudo ufw enable
cs
- sudo ufw allow 8009/tcp : 8009연동 포트도 열어주었다. 아파치와 톰캣을 연동관련 포트이다.
- 만약 톰캣만으로 ssl을 적용한다면 8443도 추가
 
나는 이 순서를 생략해도 잘 동작됬다.
 

 

 

 

혹시 위처럼 안열려있는 포트가 있다면 열어주자

 

- sudo ufw allow 포트번호/tcp

13) 톰캣에 SSL적용하기(나는 이 순서를 생략해도 잘 동작됬다.)
 
만약 아파치를 웹서버로 사용안하고 오직 톰캣만 사용한다면 아래와 같은 
 
처리가 필요하다. 하지만 아파치와 톰캣을 연동해두었다면 아파치에만 ssl을 
 
적용해두어도 동적처리또한 ssl이 적용되는것같다.
 
Let’s encrypt 의 경우 일반적인 apache / nginx 에서 사용이 가능한 pem 파일을 생성 한다.
 
생성된 pem 인증서를 tomcat에서 사용하고 싶다면 pem을 jks로의 변환을 해서 
 
org.apache.coyote.http11.Http11NioProtocol 프로토콜로 사용을 하거나
 
apr 및 tomcat-native 설치하여 org.apache.coyote.http11.Http11AprProtocol 을 사용할 수 있다.
 
나는 jks로 변환을 해보았다. 
 
13-0) 먼저 pem파일들이 있는 디렉토리로 이동한다.
 
13-1) pem 파일을 통합해 pkcs12형식으로 변환한다
 
$ openssl pkcs12 -export -in cert.pem -inkey privkey.pem -out cert_and_key.p12 -name tomcat -CAfile chain.pem -caname root -passin pass:password  -passout pass:password 
 
13-2) pkcs12형식을 jks로 변환한다.
 
$ keytool -importkeystore -deststorepass password -destkeypass password -destkeystore cert_and_key.jks -srckeystore cert_and_key.p12 -srcstoretype PKCS12 -srcstorepass password -alias tomcat
 
13-3) 키스토어에 임포트 해준다.
 
$ keytool -import -trustcacerts -alias tomcattomcat -file chain.pem -keystore cert_and_key.jks
 
 
14) 톰캣 설정 server.xml(나는 이 순서를 생략해도 잘 동작됬다.)
 
만들어준 jks를 server.xml에 설정해준다.
 
 

 

 

1
2
3
4
<Connector protocol="org.apache.coyote.http11.Http11NioProtocol" port="8443"
           maxThreads="150" scheme="https" secure="true" SSLEnabled="true"
           keystoreFile="/etc/letsencrypt/live/dokky.ga/cert_and_key.jks"
           keystorePass="password" clientAuth="false" sslProtocol="TLS" />
cs
 
15) HTTP를 HTTPS로 리다이렉트 성정
 

sudo nano /etc/apache2/sites-available/000-default.conf : 아파치 설정파일 수정

 

HTTP 80포트로 들어오는 요청을 https로 리다이렉트 시켜준다

 

1
2
3
4
5
6
7
 
<VirtualHost *:80>
 
        RedirectPermanent / https://dokky.ga/
 
</VirtualHost>
 
cs

 

 

 

 

 

 

 

15) 아파치, 톰캣 재시작

 

systemctl restart apache2 && systemctl restart tomcat9

 

sudo service apache2 restart

 

sudo service tomcat9 restart

 
16) 인증서를 삭제후 재발급할때 아파치가 안돌아가고 80포트가 리슨이 안된상태라면
 
ssl설정에가서 ssl엔진을 off시켜줘야 돌아감!
 
IMPORTANT NOTES:
 - The following errors were reported by the server:
 
   Domain: dokky.ga
   Type:   connection
   Detail: Fetching
   http://dokky.ga/.well-known/acme-challenge/Kd85EYrHHFqx7EpO70GR2FusseWGlXJQGj7fgdvr31Q:
   Connection refused
 
   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. Additionally, please check that
   your computer has a publicly routable IP address and that no
   firewalls are preventing the server from communicating with the
   client. If you're using the webroot plugin, you should also verify
   that you are serving files from the webroot path you provided.

 

 

참고 블로그

 

https://all-record.tistory.com/190

 

https://www.comodossl.co.kr/certificate/ssl-installation-guides/Tomcat-csr-crt.aspx

 

https://hiseon.me/server/apache-ssl-setting/

 

https://hiseon.me/server/letsencrypt-ssl-certificate/

 

https://medium.com/@deokyoubkim/letsencrypt%EB%A5%BC-%EC%9D%B4%EC%9A%A9%ED%95%98%EC%97%AC-renew-%EC%8B%9C-404-not-found%EC%98%A4%EB%A5%98%EB%B0%9C%EC%83%9D-ede1fdfaa08c

 

https://devlog.jwgo.kr/2019/04/16/get-ssl-cert-from-letsencrypt/

 

https://medium.com/@panicrooms0/ubuntu-apache2-lets-encrypt%EB%A5%BC-%EC%9D%B4%EC%9A%A9%ED%95%98%EC%97%AC-ssl-https-%EC%82%AC%EC%9D%B4%ED%8A%B8-%EA%B5%AC%EC%B6%95-%EC%A0%81%EC%9A%A9%ED%95%98%EA%B8%B0-5a627c2439f

 

https://osasf.net/discussion/628/lets-encrypt-%EB%AC%B4%EB%A3%8C-ssl-%EB%B3%B4%EC%95%88-%EC%9D%B8%EC%A6%9D%EC%84%9C-%EC%84%A4%EC%B9%98-%EB%B0%A9%EB%B2%95-ubuntu-server-os-%EA%B8%B0%EC%A4%80

 

https://m.blog.naver.com/PostView.nhn?blogId=ameeam&logNo=220950594646&proxyReferer=https%3A%2F%2Fwww.google.com%2F

 

https://devlog.jwgo.kr/2019/04/16/get-ssl-cert-from-letsencrypt/

 

https://xinet.kr/?p=1674

 

https://jimnong.tistory.com/742

 

http://www.kwangsiklee.com/2016/12/%EC%8A%A4%ED%94%84%EB%A7%81-%EB%B6%80%ED%8A%B8%EB%A1%9C-letsencrypt%EB%A5%BC-%EC%9D%B4%EC%9A%A9%ED%95%98%EC%97%AC-%EB%AC%B4%EB%A3%8C-ssl-%EC%98%AC%EB%A6%AC%EA%B8%B0/

 

https://www.enteroa.com/2018/05/16/lets-encrypt-%EC%97%90%EC%84%9C-%EC%83%9D%EC%84%B1%EB%90%9C-pem-%EC%9D%B8%EC%A6%9D%EC%84%9C%EB%A5%BC-tomcat%EC%97%90%EC%84%9C-%EC%82%AC%EC%9A%A9-%ED%95%98%EA%B8%B0/

 

https://onecellboy.tistory.com/341

 

반응형

'Basic > 네트워크' 카테고리의 다른 글

구글로그인  (0) 2019.12.08
OAuth 2.0  (0) 2019.12.08
TCP/IP 의 개념  (0) 2019.11.19
SSL의 구체적인 동작방법  (0) 2019.09.27
SSL의 핵심 동작방법  (0) 2019.09.27