Ryan's Log

라즈베리파이 - #4 Apache2에 SSL 적용 본문

Raspberry Pi

라즈베리파이 - #4 Apache2에 SSL 적용

Ryan c 2018. 3. 9. 09:08
728x90

[라즈베리파이] 

Apache2에 SSL 적용



얼마전 절친지인이 내게 뜬금없이 선물(이라 말했으나)로 준 Amazon Echo Dot. 

Alexa 그녀가 Amazon Prime Music을 한달간 무료로 술술 틀어주곤 했는데...

그땐 참 좋았는데...

이제 어느덧 한달이 지나 더이상의 Prime Music Play를 기대할 수 없게되서 Alexa Skill로 내 MP3를 플레이 해보고자 마음 먹는다.

내게 뜬금없이 선물을 준 이유가 혹시...


Alexa Skill은 SSL이 적용된 HTTPS만을 허용한다. (당연 OpenSSL 불가)

그래서 내 라즈베리파이 Apache에 무료 SSL 인증서를 Let's Encrypt로 발급받아 적용하기로 한다.


적용할 Raspbian 버전은

> lsb_release -a
No LSB modules are available.
Distributor ID:	Raspbian
Description:	Raspbian GNU/Linux 9.3 (stretch)
Release:	9.3
Codename:	stretch
> sudo apachectl -V
Server version: Apache/2.4.25 (Raspbian)
Server built:   2017-09-19T18:58:57



절차를 미리 정리 하면...

-. 신뢰할수 있는 무료 인증서 Let's Encrypt 설치

-. 인증서 발급(획득)

.- Apache에 적용

.- HTTPS로 접속




Let's Encrypt ?

간단히 정리하면 
SSL이 널리 보편적으로 이용되지 않는 이유는 비용때문이라 믿고 몇몇 기관, 단체가 인증기관을 만들어 무료로 제공하고 있다. 이리도 고마울수가.


letsencrypt를 설치를 위해 먼저 패키지 매니저를 업데이트 한다.
> sudo apt-get update

이제 letsencrypt 설치
> sudo apt install letsencrypt -y



인증서를 발급 받기 위해 Apache 서버를 중지해야 한다. (서비스를 운영중인 서버라면 발급전에 신중하게)
이유는 Let's Encrypt의 인증방식 때문이다.

인증기관은 인증을 받고자 요청한 서버 도메인이 확실한지 여부를 확인 하기위해 HTTP(포트 80)을 이용하여 검사한다. 
즉, letsencrypt가 80포트를 열어 인증기관으로부터의 요청에 응답하며, 인증기관은 이를 신뢰하고 인증서를 발급하는 것이다.

그래서 이미 포트 80을 사용중인 Apache를 중지해야 하는것이다.

Apache2 중지
> sudo service apache2 stop


인증서 발급 
> sudo letsencrypt certonly --standalone -d myhome.iptime.org


인증서 발급을 요청 하면 이메일주소를 묻고, 약관?에 동의(A)해야한다.

그런데 놀랍지 않는가? 무려 iptime DDNS로도 발급을 받을 수 있다.
회사나 집에 iptime 공유기를 사용한다면 DDNS 설정과 80포트에 대한 포트포워딩 만으로 100% 무료로 SSL이 적용된 웹 서비스를 할수 있다.
아, iptime이 있어야 하니 99.9999999% 무료


보통 이 지점에서 실패하는 경우는 요청한 도메인 네임(DNS Name)으로 인증서 발급을 요청한 서버에 접속할 수 없는 경우이다.
말하자면 Apache가 동작중일때 http://myhome.iptime.org 로 웹 서비스가 원활했다면 아마 틀림없이 발급될 것이다.


발급이 되면 아래 정보의 위치에 인증서 파일이 생성된다.
이 위치는 접근이 제한되어 있으며, chmod +5 하여 확인할 수 있다.

> sudo letsencrypt certonly --standalone -d myhome.iptime.org Saving debug log to /var/log/letsencrypt/letsencrypt.log Obtaining a new certificate Performing the following challenges: http-01 challenge for myhome.iptime.org Waiting for verification... Cleaning up challenges Generating key (2048 bits): /etc/letsencrypt/keys/0000_key-certbot.pem Creating CSR: /etc/letsencrypt/csr/0000_csr-certbot.pem IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/myhome.iptime.org/fullchain.pem. Your cert will expire on 2018-06-08. 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


인증서 파일을 아래 위치에 저장했으며 6/8일까지 사용할수 있다고 써있다.
/etc/letsencrypt/live/myhome.iptime.org/fullchain.pem

인증Key 파일은 
/etc/letsencrypt/keys/0000_key-certbot.pem
또는 
/etc/letsencrypt/live/myhome.iptime.org/privkey.pem



이제 Apache2에 HTTPS인 포트 443을 열고 인증서의 위치를 지정한다.
실제 443을 위한 설정파일은 이미 ./sites-available에 default-ssl.conf로 만들어져 있다.
./sites-enabled에 링크(symbolic link)만 하면 바로 적용될 뿐

우선 default-ssl.conf를 ./sites-enabled로 링크한다.
> cd /etc/apache2/sites-enabled
> sudo ln -s ../sites-available/default-ssl.conf ./default-ssl.conf


이후 default-ssl.conf를 열어 SSLCertificateFile과 SSLCertificateKeyFile을 Let's Encrypt에서 받은 인증서의 위치로 변경한다.
> sudo vi /etc/apache2/sites-enabled/default-ssl.conf

아래는 default-ssl.conf의 일부이며, 하이라이트 한 부분만 수정하면 된다.
<IfModule mod_ssl.c>
        <VirtualHost _default_:443>
                ServerAdmin webmaster@localhost

                DocumentRoot /var/www/html
                JkMount /* ajp13_worker

                # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
                # error, crit, alert, emerg.
                # It is also possible to configure the loglevel for particular
                # modules, e.g.
                #LogLevel info ssl:warn

                ErrorLog ${APACHE_LOG_DIR}/error.log
                CustomLog ${APACHE_LOG_DIR}/access.log combined

                # For most configuration files from conf-available/, which are
                # enabled or disabled at a global level, it is possible to
                # include a line for only one particular virtual host. For example the
                # following line enables the CGI configuration for this host only
                # after it has been globally disabled with "a2disconf".
                #Include conf-available/serve-cgi-bin.conf

                #   SSL Engine Switch:
                #   Enable/Disable SSL for this virtual host.
                SSLEngine on

                #   A self-signed (snakeoil) certificate can be created by installing
                #   the ssl-cert package. See
                #   /usr/share/doc/apache2/README.Debian.gz for more info.
                #   If both key and certificate are stored in the same file, only the
                #   SSLCertificateFile directive is needed.
                SSLCertificateFile /etc/letsencrypt/live/myhome.iptime.org/fullchain.pem
                SSLCertificateKeyFile /etc/letsencrypt/live/myhome.iptime.org/privkey.pem

                #   Server Certificate Chain:
                #   Point SSLCertificateChainFile at a file containing the
                #   concatenation of PEM encoded CA certificates which form the
                #   certificate chain for the server certificate. Alternatively
                #   the referenced file can be the same as SSLCertificateFile
                #   when the CA certificates are directly appended to the server
                #   certificate for convinience.
                #SSLCertificateChainFile /etc/apache2/ssl.crt/server-ca.crt


Apache Tomcat Connector를 사용중인 경우는 JkMount 설정도 해준다.


이제 서버를 (재)시작 하면 끝
> sudo service apache2 start


SSL이 정상 작동하는지 확인한다.
https://myhome.iptime.org

Let's Encrypt의 인증서는 90일의 유효기간이 있다. 

주기적으로 해당 도메인 이름으로 서비스 하는지를 일정 기간마다 확인하겠다는 것이렸다.


인증서 갱신은 만료 30일 전부터 가능하다고 하니, 때가 되면 다음 명령으로 인증서를 갱신한다.

> sudo letsencrypt renew

> sudo letsencrypt renew Saving debug log to /var/log/letsencrypt/letsencrypt.log ------------------------------------------------------------------------------- Processing /etc/letsencrypt/renewal/myhome.iptime.org.conf ------------------------------------------------------------------------------- Cert not yet due for renewal The following certs are not due for renewal yet: /etc/letsencrypt/live/myhome.iptime.org/fullchain.pem (skipped) No renewals were attempted.


현재는 아직 인증서를 갱신할 수 있는 시기가 아니란다.
나중에 생각나면 하지 뭐!! 



728x90