[인프라] 서브 도메인 설정하기 1화

in #kr-dev3 years ago

개요

  • 1개 또는 1개 이상 pc(공인 IP) 에서 여러개의 서브도메인을 지정하고자 하는 경우
  • 손쉽게 설정 하고자 하는 경우 ( docker-compose 활용 )
  • ssl 설정 ( https ), 무료 인증서를 설정 ( letsencrypt )

GoogleDomains

다른 도메인 호스팅 업체도 동일(유사) 하게 작업을 해주면 됨

호스트명유형TTL데이터
free-account.wonsama.devA1시간IP주소

이런식으로 호스트명 (서브 도메인명 ) 과 IP 주소를 적어준다.

로컬에서 특정 포트로 호스팅 하는 경우에도 ( ex) port 3000 ) 서브 도메인을 활용하여 proxy 할 수 있다.

docker, docker-compose 설치

nginx 및 certbot 설치

ubuntu 20.04 에서 설치 한 작업 내역임, 아래 링크를 참조하면 된다.

어려운 것은 없고? 뭐 따라하면 금방임.

https://docs.docker.com/engine/install/ubuntu/
https://docs.docker.com/compose/install/

nginx + certbot ( feat docker-compose )

nginx : proxy, ssl, subdomain 지정
certbot : 무료 인증서 발급 처리

개인적으로 docker-compose 를 활용하는 이유는, 빠른 설치, 동일한 환경 구성, 빠른 제거 등을 이유로 든다. 사실 개별 패키지로 설치 하려면 이거저거 걸리는 점들이 넘 많음( 특히 의존성)

docker-compose.yml

version: '3'
services:
  nginx:
    container_name: nginx
    image: nginx:1.15-alpine
    restart: always
    ports:
    - 80:80
    - 443:443
    volumes:
    - ./data/nginx/conf.d:/etc/nginx/conf.d
    - ./data/certbot/conf:/etc/letsencrypt
    - ./data/certbot/www:/var/www/certbot
  certbot:
    container_name: certbot
    image: certbot/certbot:v0.36.0
    restart: always
    volumes:
      - ./data/certbot/conf:/etc/letsencrypt
      - ./data/certbot/www:/var/www/certbot

init-letsencrypt.sh

인증서 발급에 활용, [email protected], free-account.wonsama.dev 등과 같은 정보는 자신 환경에 알맞게 수정

#!/usr/bin/bash

if ! [ -x "$(command -v docker-compose)" ]; then
  echo 'Error: docker-compose is not installed.' >&2
  exit 1
fi

domains=("free-account.wonsama.dev")
rsa_key_size=4096
data_path="./data/certbot"
email="[email protected]" # Adding a valid address is strongly recommended
staging=0 # Set to 1 if you're testing your setup to avoid hitting request limits

if [ -d "$data_path" ]; then
  read -p "Existing data found for $domains. Continue and replace existing certificate? (y/N) " decision
  if [ "$decision" != "Y" ] && [ "$decision" != "y" ]; then
    exit
  fi
fi


if [ ! -e "$data_path/conf/options-ssl-nginx.conf" ] || [ ! -e "$data_path/conf/ssl-dhparams.pem" ]; then
  echo "### Downloading recommended TLS parameters ..."
  mkdir -p "$data_path/conf"
  curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot-nginx/certbot_nginx/_internal/tls_configs/options-ssl-nginx.conf > "$data_path/conf/options-ssl-nginx.conf"
  curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot/certbot/ssl-dhparams.pem > "$data_path/conf/ssl-dhparams.pem"
  echo
fi

echo "### Creating dummy certificate for $domains ..."
path="/etc/letsencrypt/live/$domains"
mkdir -p "$data_path/conf/live/$domains"
docker-compose run --rm --entrypoint "\
  openssl req -x509 -nodes -newkey rsa:$rsa_key_size -days 1\
    -keyout '$path/privkey.pem' \
    -out '$path/fullchain.pem' \
    -subj '/CN=localhost'" certbot
echo


echo "### Starting nginx ..."
docker-compose up --force-recreate -d nginx
echo

echo "### Deleting dummy certificate for $domains ..."
docker-compose run --rm --entrypoint "\
  rm -Rf /etc/letsencrypt/live/$domains && \
  rm -Rf /etc/letsencrypt/archive/$domains && \
  rm -Rf /etc/letsencrypt/renewal/$domains.conf" certbot
echo


echo "### Requesting Let's Encrypt certificate for $domains ..."
#Join $domains to -d args
domain_args=""
for domain in "${domains[@]}"; do
  domain_args="$domain_args -d $domain"
done

# Select appropriate email arg
case "$email" in
  "") email_arg="--register-unsafely-without-email" ;;
  *) email_arg="--email $email" ;;
esac

# Enable staging mode if needed
if [ $staging != "0" ]; then staging_arg="--staging"; fi

docker-compose run --rm --entrypoint "\
  certbot certonly -a webroot -v --debug-challenges -w /var/www/certbot \
    $staging_arg \
    $email_arg \
    $domain_args \
    --rsa-key-size $rsa_key_size \
    --agree-tos \
    --force-renewal" certbot
echo

echo "### Reloading nginx ..."
docker-compose exec nginx nginx -s reload

app.conf

subdomain & ssl 설정 정보

한번에 여러개 설정 가능

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

    server_name free-account.wonsama.dev;

    location /.well-known/acme-challenge/ {
            allow all;
            root /var/www/certbot;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    server_name  free-account.wonsama.dev;
    server_tokens off;

    ssl_certificate /etc/letsencrypt/live/ free-account.wonsama.dev/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/ free-account.wonsama.dev/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        proxy_pass http://공개IP:공걔PORT;
        proxy_set_header    Host                $http_host;
        proxy_set_header    X-Real-IP           $remote_addr;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
    }
}

작업 실행

좀 어려울 수도 있으니 여러번 해보시고 잘 안되면 구글링 하심이 정신건강에 이롭긴 합니다 ㅜㅜ

1. 폴더 생성 및 파일 작성

폴더 생성

L data
__ L nginx
__ __ L conf.d
__ L certbot
__ __ L www
__ __ L conf
  • ./docker-compose.yml 작성
  • ./init-letsencrypt.sh 작성
  • ./data/nginx/conf.d/app.conf 작성

2. 인증서 생성

app.conf 에서 ssl 설정(443포트 포함) 해당 부분을 제거
docker-compose.yml 파일이 있는 경로에서 docker-compose up 수행
chmod +x ./init-letsencrypt.sh 이후 ./init-letsencrypt.sh 을 실행하여 인증서 생성

3. 인증서 반영

app.conf 에서 ssl 설정(443포트 포함) 해당 부분을 추가
docker-compose.yml 파일이 있는 경로에서 docker-compose up -d 수행 (백그라운드 기동)

추후 인증서 기간이 만료 되면 2, 3 항목을 순서대로 실행한다


음 ... 다들 성공하길 바래봅니다.

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63968.82
ETH 2633.99
USDT 1.00
SBD 2.84