3 min read

Nginx에 Letsencrypt SSL 인증서 적용하기

Letsencrypt는 무료로 인증가능한 SSL인증서입니다.

무료라는 점 때문에 3개월정도만 SSL을 지원해 줍니다.

그치만 따로 설정을 해주면 3개월마다 자동인증을 되어 매번 귀잖게 3개월마다 재인증을 할 필요가 없습니다.

https://certbot.eff.org/

Letsencrypt 인증서 설명은 위에 링크를 참조 했습니다.

터미널로 들어가신후에 아래와 같이 터미널에 입력해주시기바랍니다.

1
2
wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto
cs

이는 터미널에서 설치파일을 다운받고 권한 설정하는 명령어 입니다.

1
sudo ./certbot-auto –nginx -d <도메인명>
cs

그리고 해당 프로그램을 –nginx라는 옵션을 주고 실행 시켜줍니다.

저 같은 경우는 이미 도메인을 설정해서 위와 같이 뜨네요.

잘 설정하게 되면 위와 같이 출력이 됩니다.

1
2
/etc/letsencrypt/live/kudwafter.moe/fullchain.pem
/etc/letsencrypt/live/kudwafter.moe/privkey.pem
cs

문장 사이에 이런 문장이 있습니다.

이는 공개키와 개인키의 위치이므로 따로 저장해 두세요.

이제 nginx에 따로 설정해 둡니다.

아래는 저희 nginx 설정파일입니다.

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
42
43
44
45
46
server {
        # SSL configuration
        #
        listen 443 ssl default_server;
        listen [::]:443 ssl default_server;
        ssl_certificate /etc/letsencrypt/live/kudwafter.moe/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/kudwafter.moe/privkey.pem;
        #
        # Self signed certs generated by the ssl-cert package
        # Don’t use them in a production server!
        #
        # include snippets/snakeoil.conf;
        root /var/www/html;
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;
        server_name _;
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
        #       fastcgi_pass unix:/var/run/php5-fpm.sock;
        #}
        # deny access to .htaccess files, if Apache’s document root
        # concurs with nginx’s one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}
cs

ssl_certificate은 공개키의 경로를 ssl_certificate_key 개인키의 경로를 입력후 nginx를 재시작후에 자신의 웹사이트에 들어가보세요.

이처럼 정상적으로 입력이 된걸 볼수 있습니다.

인증서 자동갱신은 보통 crontab을 사용합니다.

공식 사이트또한 crontab을 사용하라고 나와 있고요.

저 같은 경우 아래와 같이 사용합니다.

1
15 3 * * * /<경로>/certbot-auto renew
cs