본문 바로가기

개발관련

Ubuntu + Nginx + Cafe24 SSL 적용(Django, Vue)

알아야할 지식 키워드

  • ubuntu
  • nginx
  • vi
  • cafe24
  • ssl
  • django
  • vue

 

실행

 

 

1. 먼저 카페24 ssl 인증서를 신청한다.

신청이 완료되면 위와 같은 세팅으로 개인키, 인증서, 중개자인증서, 체인인증서를 모두 다운 받는다.

 

 

 

2. 다운받은 인증서를 SSL 인증을 적용할 서버로 옮긴다.

 

방법을 모르면 ubuntu scp를 검색

 

 

3. 인증서 합치기

 

서버의 적당한 위치에 인증서를 복사 후 인증서를 합친다.

 

cat ssl.crt chain_all_ssl.crt > domain.pem

 

domain.pem은 본인의 도메인 주소를 입력한다.

위 명령어를 입력하면 두개의 인증서를 붙여주는데 들어가보면 중간에

 

-----end certificate----- -----begin certificate -----

 

이렇게 줄바꿈이 안되어 있다. 해당 부분을

 

-----end certificate-----

-----begin certificate -----

 

이렇게 만들어 준다.

 

우분투 파일 수정 방법을 모르겠으면 ubuntu vim을 검색해 본다.

 

 

 

4. 암호 풀기

 

SSL 인증서는 구매시 암호를 입력하게 되어 있다. 해당 암호를 풀어야 한다.

 

openssl rsa -in ssl.key -out new_ssl.key

 

명령어를 입력하면 암호를 물어볼것이다. 구매시 입력한 암호를 입력하면 new_ssl.key 파일이 생성된다.

 

 

 

5. nginx 에 적용하기

 

마지막으로 nginx 설정 파일을 연다. 구글 검색 키워드는 nginx conf 파일로 검색

 

 

Vue 적용 예시

server {
        client_max_body_size 50M;

        listen 80 default_server;
        server_name domain.com;
        return 301 https://$host$request_uri;

        index index.html;
}

server {
        client_max_body_size 50M;

        listen 443 ssl;

        server_name domain.com;

        ssl on;
        ssl_certificate /home/ubuntu/app/ssl/domain.com.pem;
        ssl_certificate_key  /home/ubuntu/app/ssl/new_ssl.key;

        ssl_session_timeout 5m;

        root  /home/ubuntu/app/dist;

        location / {
            try_files $uri $uri/ /index.html;
        }
}

vue의 경우 npm serve build 명령을 먼저 실행해서 dist 경로를 만들어 둬야 한다.

해당 경로를 root에 연결 하면 됨

 

 

 

Django + uwsgi 적용 예시

server {
    listen 80 default_server;
    server_name domain.com;

    client_max_body_size 50M;

    return 308 https://$host$request_uri;

    index index.html;
}

server {
    listen 443 ssl;

    server_name domain.com;
    large_client_header_buffers 4 16k;

    location = /favicon.ico { access_log off; log_not_found off; }

    ssl on;
    ssl_certificate /home/ubuntu/app/ssl/domain.com.pem;
    ssl_certificate_key  /home/ubuntu/app/ssl/new_ssl.key;
    
    charset utf-8;
    client_max_body_size 50M;
    ssl_session_timeout 5m;

    location / {
        uwsgi_pass  unix:///tmp/mysite.sock;
        include     uwsgi_params;
    }
}

django의 경우 일반적으로 uwsgi를 적용했을것이기 때문에 위와 같이 적용한다.

 

 

이렇게 적용했으면 nginx를 재시작 한다.

 

sudo service nginx restart

 

당연한 이이기지만 서버의 443 포트를 열어야 한다. 포트를 안열면 타임아웃 오류가 발생

이제 웹 페이지에 접속해보면 SSL이 적용되었다.