카테고리 없음

인증서 발급 받기 위한 mkcert

mkcert 라는 프로그램으로 로컬 환경(내 컴퓨터) 에서 신뢰할 수 있는 인증서를 만들 수 있는 간단한 도구이다. CA(인증기관)의 인증서를 발급받아 자체적으로 관리하는 것은 복잡한 명령, 전문 지식이 필요하게 된다. 

 

mkcert설치

 

sudo apt install linbnss-tools

wget -O mkcert https://github.com/FiloSottile/mkcert/releases/download/v1.4.3/mkcert-v1.4.3-linux-amd64

chmod +x

sudo cp mkcert /usr/local/bin/

 

로컬 환경에 CA 생성

이 명령어로 로컬을 인증된 발급기관으로 추가해야한다.

mkcert -install

로컬 환경에 대한 인증서 생성

다음은 로컬 환경에 대한 인증서를 만들어야 한다.

 

  • 옵션으로 추가한 example.com 도메인에서 사용할 수 있는 인증서 발급 
mkcert -key-file key.pem -cert-file cert.pem example.com *.example.com

 

 

또는

 

  • 옵션으로 추가한 localhost, 127.0.0.1(IPv4), ::1(IPv6)에서 사용할 수 있는 인증서 발급
mkcert -key-file key.pem -cert-file cert.pem localhost 127.0.0.1 ::1

로컬을 확인해보면 cert.pem, key.pem이라는 파일이 생성된 것을 확인할 수 있다.

 

이제 어떻게 적용하는지 알아보자

 

node.js https 모듈 이용

const https = require('https');
const fs = require('fs');

https
  .createServer(
    {
      key: fs.readFileSync(__dirname + '/key.pem', 'utf-8'),
      cert: fs.readFileSync(__dirname + '/cert.pem', 'utf-8'),
    },
    function (req, res) {
      res.write('Congrats! You made https server now :)');
      res.end();
    }
  )
  .listen(3001);

 

express.js를 이용

 

const https = require('https');
const fs = require('fs');
const express = require('express');

const app = express();

https
  .createServer(
    {
      key: fs.readFileSync(__dirname + '/key.pem', 'utf-8'),
      cert: fs.readFileSync(__dirname + '/cert.pem', 'utf-8'),
    },
    app.use('/', (req, res) => {
      res.send('Congrats! You made https server now :)');
    })
  )
  .listen(3001);