ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • EC2 가비아 - 도메인 등록 + Nginx & CertBot
    Project/TravelFeelDog 2023. 10. 16. 18:33

     

    Https 의 통신을 위하여  Certbot 을 사용한 권한 증명을 받는 구성을 합니다.

     

    3 번째로 시스템을  구성 하며 기억을 위하여 글을 작성해 봅니다.

     

    사전 준비 

     

    1. AWS 의 EC2 를 구성합니다.

    2. 인바운드 아웃 바운드를 구성하고 탄력적 IP 를 할당받습니다.

    3. 퍼블릭 IPV4 주소를 확인합니다.

     

     

    가비아 준비 

     

    가비아의 경우 37 프로 이벤트를 진행하는 중으로 1년 도메인 구매시 16500 원입니다.

     

    AWS 의 도메인 서비스 Route53을  사용하여도 좋습니다.  

     

    + 2023 년 연초에 가비아는 디도스 공격을 받아 도메인 서비스 이용이 반나절 중단된 상황이 있었습니다.

     

     

    구매한 가비아의 DNS 설정을 합니다.

     

    체크 표시후 DNS 설정으로 이동합니다.

     

     

    레코드 3 개를 등록해줍니다.

     

     

     

    호스트가 @ 인 경우     

    http://sunghyun98:8080

     

    호스트가 api 인 경우 

    http://api.sunghyun98:8080 

     

     

    호스트가 www 인 경우      

     

    http://www.sunghyun98:8080

     

    웹 페이지를 추가적으로 배포하면 설정값을 변경합니다.

    (WEB - WAS - DB 3tier 아키텍처) 

     

     

    여기까지 진행 했으면  배포한 EC2의 인바운드 설정을 잘했으면 다음과 같이 나옵니다.

     

    api 서브 도메인이 아닌경우도 접속이 잘됩니다 굿

     

    CertBot & NGINX 

     

    Nginx 를 설치한 이후 CertBot 을 사용하여 Https 의 필요한 인증서를 받고 수동으로 경로들을 설정하는 방식이 있지만.

     

    Certbot 의 자동 nginx 설정을 해주는 플러그인을 활용해 줍시다.

     

    저의 우분투 버전은 22 입니다.

    ubuntu@ip-172-31-20-37:~$ lsb_release -a
    No LSB modules are available.
    Distributor ID:	Ubuntu
    Description:	Ubuntu 22.04.2 LTS
    Release:	22.04
    Codename:	jammy

     

     

    1.  Nginx 설치

    sudo apt update
    sudo apt install nginx

     

    2. Nginx 서비스 시작

    sudo systemctl start nginx

     

    3. Certbot과 Nginx 플러그인을 설치합니다.

    sudo apt install software-properties-common
    sudo add-apt-repository ppa:certbot/certbot
    sudo apt update
    sudo apt install certbot python3-certbot-nginx

     

     

     

    4. 인증서 발급 : 

    sudo certbot --nginx

     

    여러 도메인을 등록할때  , 를 사용하거나 공백으로 구분합니다.

    api.sunghyun98.com sunghyun98.com www.sunghyun98.com

     

     인바운드가 80 , 443 포트는 필수로 열려있어야합니다.  http, https 의 기본 포트

     

     

    설치한 Nginx를 확인합니다.

     

    cd /etc/nginx

     

    ubuntu@ip-172-31-20-37:/etc/nginx$ ls
    conf.d        fastcgi_params  koi-win     modules-available  nginx.conf    scgi_params      sites-enabled  uwsgi_params
    fastcgi.conf  koi-utf         mime.types  modules-enabled    proxy_params  sites-available  snippets       win-utf

     

     

    sites-available 을 확인합니다.

     

    자동으로 설정이 된것을 확인 할 수 있습니다 

     

    server {
    
            # 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 www.sunghyun98.com sunghyun98.com api.sunghyun98.com; # managed by Certbot
    
    
            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;
            #}
    
    
        listen [::]:443 ssl ipv6only=on; # managed by Certbot
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/api.sunghyun98.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/api.sunghyun98.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

     

     

       경로 변경

     

    등록한 dns 로 https 접속이 가능한것을 확인했습니다.

     

     

     

    이제는 Nginx의 server 설정의 코드를 변경하여 , 현제 8080 포트로 돌아가는 스프링 부트의 서버로 포트 포워딩을 하거나 

     

    8081 의 개발 전용 서버로 리버스 프록시를 구축하는등의 추가적인 설정을 진행합니다.

     

     

    server {
    
       if ($host = www.sunghyun98.com) {
            return 301 https://$host$request_uri;
        }
    
       if ($host = sunghyun98.com) {
            return 301 https://$host$request_uri;
        }
    
        if ($host = api.sunghyun98.com) {
            return 301 https://$host$request_uri;
        }
    
          listen 80 ;
          listen [::]:80 ;
        server_name www.sunghyun98.com sunghyun98.com api.sunghyun98.com;
    
        return 404;
    
    }
    
    
    
    #for Release
    server {
    
            # SSL configuration
            #
            # listen 443 ssl
    
          server_name www.sunghyun98.com sunghyun98.com api.sunghyun98.com;
    
            set $port 8080;
    
    
            if ($host = api.sunghyun98.com) {
                    #set $port 8080;
            }
            
            if ($host = sunghyun98.com) {
                    #set $port 8080;
            }
            
            if ($host = www.sunghyun98.com) {
                    #set $port 8080;
            }
    
    
    
          location / {
                    proxy_set_header Host $http_host;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header X-Forwarded-Proto $scheme;
                    proxy_set_header Upgrade $http_upgrade;
                    proxy_set_header Connection "upgrade";
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_pass http://127.0.0.1:$port;
    
                    proxy_connect_timeout 300;
                    proxy_send_timeout 300;
                    proxy_read_timeout 300;
    
                    proxy_redirect off;
                    proxy_buffer_size          128k;
                    proxy_buffers              4 256k;
                    proxy_busy_buffers_size    256k;
                    }
    
        listen [::]:443 ssl ipv6only=on; 
        listen 443 ssl;
        ssl_certificate /etc/letsencrypt/live/api.sunghyun98.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/api.sunghyun98.com/privkey.pem;
        include /etc/letsencrypt/options-ssl-nginx.conf;
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
    
    }

     

    설정 값을 테스트하고 재시동 해줍니다.

    
    sudo nginx -t
    
    
    sudo systemctl restart nginx

     

    완성

Designed by Tistory.