반응형

NAS를 구축할 때 나는 NextCloud를 사용했다. 

그냥 http로도 사용을 할 수 있지만, 보안성을 조금 더 높이기 위해 https를 사용하기로 한다.


https를 발급받는게 돈이 드는게 아니니까 안 쓸 이유도 없었다. 

(다만 갱신땜에 귀찮다)


그래서 오늘 할 방법은 라즈베리파이(Raspberry Pi)에 Apache에 SSL인증서를 발급받아 사용하는 방법이다.


RaspberryPI SSL발급받기


준비물

1. 발급 받을 수 있는 도메인이 준비 되어 있어야 한다.

2. Ctrl C + V를 할 수 있는 능력


사용중인 Raspbian & Apache 버전

1
2
3
4
5
6
7
8
9
10
> lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 9.6 (stretch)
Release:        9.6
Codename:       stretch
 
> sudo apachectl -V
Server version: Apache/2.4.25 (Raspbian)
Server built:   2018-11-03T18:46:19
cs


적용순서 (도메인이 있다는 가정하에)

1. Let's Encrypt 설치

2. 인증서 발급

3. Apache Config 설정

4. 적용


Let's Encrypt란?

SSL이 보편적으로 사용되도록 몇몇 기관, 단체가 인증기관을 만들어서 무료로 제공하는 툴이다. 무료! 굉장히좋다.


1
2
3
4
> sudo apt-get update
> sudo apt-get upgrade
 
> sudo apt install letsencrypt -y
cs


1번줄 : 패키지 매니저 업데이트

2번줄 : 패키지 매니저 업그레이드

4번줄 : Let's Encrypt설치 


위의 순서대로 따라서 입력을 한다.


※ 중요

 우선 인증서를 발급받기 위해서는 Apache 서버를 중지해야한다. 그 이유는 Let's Encrypt가 인증을 할 때 80번 포트를 사용해서 인증을 시도하기 때문이다. 80번포트를 통하여 인증기관이 검사하고, 이를 신뢰하여 인증서가 발급되는 순서다.

그렇기 때문에 80번 포트를 쓰는 Apache를 중지한다.


1
2
3
> sudo service apache2 stop
 
> netstat -nap | grep '80'
cs


1번줄 Apache2 중지

3번줄 80번포트가 열려있는게 있는지 확인 (아무것도 안나와야 정상)


1
> sudo letsencrypt certonly --standalone -d 여러분의도메인주소(ex.1234.5678.com)
cs


1번줄 인증서 발급

도메인 주소를 여러분의 도메인에 맞게 적어서 코드를 실행시킨다.


뭘 자꾸 누르라고 나온다. Yes나 Accept가 나올텐데 동의해준다. 발급을 위한 조건들이다.

아마 E-mail도 입력을 해야할텐데 입력해준다.


+ IPTIME 공유기 DDNS관련

- 아마도 IPTIME 공유기의 DDNS로는 발급이 안되는 것 같다. 본인도 처음에 그렇게 시도를 했는데, 결과를 확인해보니 이미 너무 많은 iptime.org의 DDNS가 있어 막혔다는 내용이었다. 다른방식으로 도메인을 발급받는게 맘 편하다.


1
2
3
4
5
6
7
8
9
10
11
IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/1234.5678.com/fullchain.pem. Your cert
   will expire on 2019-05-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</p>
cs


발급이 완료되면 위와같이 Congratulations! 라면서 어쩌구 저쩌구 뜬다. 인증서의 유효기간은 90일이기에 나는 2019-05-18까지 사용이 가능하다. 이렇게 Congratulations이라는 문구가 안보이면 어딘가 실팬한 것이다. 내용을 잘 읽어보자.


<인증서 위치>

인증서는 대부분 아래의 위치에 있다. 확인하자. 

아마 접근 제한이 걸려있을텐데, 권한을 줘서 풀어주자

1
> sudo chmod +5

cs

dd

인증서 확인

1
2
3
4
# 인증서파일 File
/etc/letsencrypt/live/여러분의도메인/fullchain.pem 
# 인증서Key Key
/etc/letsencrypt/keys/여러분의도메인/privkey.pem
cs



자, 이제 거의 다왔다. Apache의 Config파일을 수정해서 443포트(https)를 열어주고 인증서의 위치를 지정해 줄 것이다.


우선 Apache의 config 폴더로 이동하자.

1
2
> cd /etc/apache2/sites-enabled
> sudo ln -s ../sites-available/default-ssl.conf ./default-ssl.conf

cs


1번줄 config 파일로 이동

2번줄 sites-enabled → sites-available로  default-ssl.conf 파일 링크


 이제 인증서의 위치를 잡아주면 된다. 

default-ssl.conf 요 파일을 vi로 열어보면 VirtualHost 443이라고 되어있는데, 이게 https를 사용하는 포트 설정이다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<IfModule mod_ssl.c>
        <VirtualHost _default_:443>
                ServerAdmin webmaster@localhost
 
                # 443포트로 접속했을 때 다른 페이지를 보여주고 싶다면 아래를 수정
                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 출력을 위한 라인
                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/여러분의도메인/fullchain.pem
                SSLCertificateKeyFile /etc/letsencrypt/keys/여러분의도메인/privkey.pem
 
                # ... 이하 줄 생략 ... 
cs


흰색 코드들만 잘 보고 편집해주면 된다. 주석으로 달아놨으니 쉽게 수정이 가능할 것이라 생각된다.

이게 끝이다. 이제 Apache를 재시작하자.


1
> sudo service apache2 start
cs


그 다음에 https가 잘 작동하는지 확인을 해보자.

입력 : https://여러분의도메인


이런식으로 적용이 된 것을 볼 수 있다.



인증서는 만료 30일 전부터 갱신이 가능하다. 만료되지 않게 알아서 갱신해주자!

1
> sudo letsencrypt renew
cs



반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기