HyunJun 기술 블로그

Nginx 무료 SSL 인증서 적용하기 (Let's Encrypt) 본문

Nginx

Nginx 무료 SSL 인증서 적용하기 (Let's Encrypt)

공부 좋아 2024. 7. 5. 16:02
728x90

1.Nginx 설치

sudo apt update
sudo apt install nginx

 

설치 확인

설치 후 Nginx가 정상적으로 작동하는지 확인한다.

sudo systemctl start nginx
sudo systemctl enable nginx

 

2. 도메인 이름 설정

Let's Encrypt 인증서를 발급받으려면 도메인 이름이 필요하다. 도메인이 설정되어 있는지 확인하고, 도메인이 EC2 인스턴스를 가리키도록 DNS 설정한다.

 

도메인을 생성했으면 아래처럼 설정해준다. 필자는 이미 도메인을 쓰고 있어 서브도메인을 사용해서 테스트 해보려고 한다.

 

 

3. Certbot 설치

Certbot은 Let's Encrypt SSL 인증서를 발급받기 위한 클라이언트이다.

sudo apt install certbot python3-certbot-nginx

 

python3-certbot-nginx는 Certbot의 플러그인 중 하나로, Nginx 웹 서버와 통합되어 SSL/TLS 인증서를 쉽게 발급하고 설정할 수 있게 해준다. Let's Encrypt에서 SSL 인증서를 발급받는 과정을 자동화하여, 인증서를 설치하고 갱신하는 것을 단순화한다. 이 플러그인은 Certbot의 핵심 기능과 Nginx 웹 서버의 설정을 자동으로 조정하는 기능을 제공한다.

 

4. Nginx 설정 파일 수정

Nginx 설정 파일을 열어 서버 블록에 도메인 이름을 추가한다. 예를들어 /etc/nginx/sites-available/default 파일을 수정한다.

sudo vi /etc/nginx/sites-available/default

 

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        server_name test.hyunjun.kr www.test.hyunjun.kr;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # 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 PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

 

 

5. Let's Encrypt SSL 인증서 발급

Certbot을 사용하여 Let's Encrypt SSL 인증서를 발급받고 Nginx 설정을 자동으로 수정한다.

sudo certbot --nginx -d your_domain.com -d www.your_domain.com
sudo certbot --nginx -d test.hyunjun.kr -d www.test.hyunjun.kr
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for test.hyunjun.kr and www.test.hyunjun.kr

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/test.hyunjun.kr/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/test.hyunjun.kr/privkey.pem
This certificate expires on 2024-10-03.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

Deploying certificate
Successfully deployed certificate for test.hyunjun.kr to /etc/nginx/sites-enabled/default
Successfully deployed certificate for www.test.hyunjun.kr to /etc/nginx/sites-enabled/default
Congratulations! You have successfully enabled HTTPS on https://test.hyunjun.kr and https://www.test.hyunjun.kr

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

명령어가 실행되면 Certbot은 자동으로 Nginx 설정을 업데이트하고 SSL 인증서를 발급받아 적용한다. 

 

6. SSL 인증서 자동 갱신 설정

Let's Encrypt 인증서는 90일 동안 유효하다. Certbot은 인증서를 자동으로 갱신할 수 있는 크론잡을 제공한다.

sudo certbot renew --dry-run

 

위 명령어로 갱신 테스트를 할 수 있다. 정상적으로 갱신된다면 자동 갱신이 설정되어 있는 것이다.

 

 

7. Nginx 재시작

마지막으로 Nginx를 재시작하여 설정을 적용한다.

sudo systemctl restart nginx

이제 Nginx가 Let's Encrypt SSL 인증서를 사용하여 HTTPS로 HTML 페이지를 서빙하게 된다. 웹 브라우저에서 도메인을 입력하여 HTTPS 연결이 정상적으로 설정되었는지 확인해보자.

 

 

 

마지막으로 이를 본인의 애플리케이션에 연결하려면 /etc/nginx/sites-available/default의 설정을 리버스 프록시로 본인의 앱 쪽으로 변경해 주면 된다. 대략 로케이션 쪽에 아래와 비슷하게 앱 상황에 따라 설정을 넣어주면 된다.

   location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
728x90
Comments