์ด ๊ธฐ์ฌ์์๋ Docker์์ ์คํ๋๋ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ํ SSL ์ธ์ฆ์๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ๊ณต์ ํ๊ณ ์ถ์ต๋๋ค. ์ธํฐ๋ท์ ๋ฌ์์์ด ๋ถ๋ถ์์๋ ๊ทธ๋ฌํ ํด๊ฒฐ์ฑ ์ ์ฐพ์ง ๋ชปํ์ต๋๋ค.
์์ธํ ๋ด์ฉ์ ์ปท ์๋์ ์์ต๋๋ค.
์ฐ๋ฆฌ๋ docker v.17.05, docker-compose v.1.21, Ubuntu Server 18 ๋ฐ ์์ Let'sEncrypt ํ์ธํธ๋ฅผ ์ฌ์ฉํ์ต๋๋ค. Docker์ ํ๋ก๋์
์ ๋ฐฐํฌํ ํ์๋ ์์ต๋๋ค. ํ์ง๋ง Docker ๊ตฌ์ถ์ ์์ํ๋ฉด ์ค๋จํ๊ธฐ๊ฐ ์ด๋ ค์์ง๋๋ค.
๋ฐ๋ผ์ ๋จผ์ ๊ฐ๋ฐ ๋จ๊ณ์์ ์ฌ์ฉํ๋ ํ์ค ์ค์ ์ ์ ๊ณตํ๊ฒ ์ต๋๋ค. ์ผ๋ฐ์ ์ผ๋ก ํฌํธ 443 ๋ฐ SSL์ด ์๋ ๊ฒฝ์ฐ:
docker-compose.yml
version: '2'
services:
php:
build: ./php-fpm
volumes:
- ./StomUp:/var/www/StomUp
- ./php-fpm/php.ini:/usr/local/etc/php/php.ini
depends_on:
- mysql
container_name: "StomPHP"
web:
image: nginx:latest
ports:
- "80:80"
- "443:443"
volumes:
- ./StomUp:/var/www/StomUp
- ./nginx/main.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
mysql:
image: mysql:5.7
command: mysqld --sql_mode=""
environment:
MYSQL_ROOT_PASSWORD: xxx
ports:
- "3333:3306"
nginx/main.conf
server {
listen 80;
server_name *.stomup.ru stomup.ru;
root /var/www/StomUp/public;
client_max_body_size 5M;
location / {
# try to serve file directly, fallback to index.php
try_files $uri /index.php$is_args$args;
}
location ~ ^/index.php(/|$) {
#fastcgi_pass unix:/var/run/php7.2-fpm.sock;
fastcgi_pass php:9000;
fastcgi_split_path_info ^(.+.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
internal;
}
location ~ .php$ {
return 404;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
๋ค์์ผ๋ก ์ค์ ๋ก SSL์ ๊ตฌํํด์ผ ํฉ๋๋ค. ์์งํ ๋งํด์ ์ปด์กด ๊ณต๋ถํ๋๋ฐ 2์๊ฐ ์ ๋ ๊ฑธ๋ ธ์ด์. ๊ฑฐ๊ธฐ์ ์ ๊ณต๋๋ ๋ชจ๋ ์ต์ ์ ํฅ๋ฏธ ๋กญ์ต๋๋ค. ํ์ง๋ง ํ์ฌ ํ๋ก์ ํธ ๋จ๊ณ์์๋ ์ฐ๋ฆฌ(๊ธฐ์ )๊ฐ ์ ์ํ๊ณ ์์ ์ ์ผ๋ก ๋์ฌ๋ฅผ ์กฐ์ฌ์ผ ํ์ต๋๋ค. SSL Let'sEnctypt ะบ Nginx์ ์ปจํ ์ด๋ ๊ทธ ์ด์์ ์๋๋๋ค.
์ฐ์ ์๋ฒ์ ์ค์นํด๋ดค์ต๋๋ค. certbot
sudo apt-get install certbot
๋ค์์ผ๋ก ๋๋ฉ์ธ์ ๋ํ ์์ผ๋์นด๋ ์ธ์ฆ์๋ฅผ ์์ฑํ์ต๋๋ค.
sudo certbot certonly -d stomup.ru -d *.stomup.ru --manual --preferred-challenges dns
์คํ ํ certbot์ DNS ์ค์ ์ ์ง์ ํด์ผ ํ๋ 2๊ฐ์ TXT ๋ ์ฝ๋๋ฅผ ์ ๊ณตํฉ๋๋ค.
_acme-challenge.stomup.ru TXT {ัะพัะะปััะะพัะพััะนะะฐะผะัะดะฐะปCertBot}
๊ทธ๋ฆฌ๊ณ ์ํฐ๋ฅผ ๋๋ฅด์ธ์.
๊ทธ ํ, certbot์ DNS์ ์ด๋ฌํ ๋ ์ฝ๋๊ฐ ์๋์ง ํ์ธํ๊ณ ์ธ์ฆ์๋ฅผ ์์ฑํฉ๋๋ค.
์ธ์ฆ์๋ฅผ ์ถ๊ฐํ์ง๋ง certbot ์ฐพ์ง ๋ชปํ์ต๋๋ค. 5~10๋ถ ํ์ ๋ช
๋ น์ ๋ค์ ์์ํด ๋ณด์ธ์..
์, ์ฌ๊ธฐ ์ฐ๋ฆฌ๋ 90์ผ ๋์ Let'sEncrypt ์ธ์ฆ์์ ์๋์ค๋ฌ์ด ์์ ์์ด์ง๋ง ์ด์ ์ด๋ฅผ Docker์ ์ ๋ก๋ํด์ผ ํฉ๋๋ค.
์ด๋ฅผ ์ํด ๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ผ๋ก docker-compose.yml์ nginx ์น์ ์์ ๋๋ ํฐ๋ฆฌ๋ฅผ ์ฐ๊ฒฐํฉ๋๋ค.
SSL์ ์ฌ์ฉํ docker-compose.yml ์
version: '2'
services:
php:
build: ./php-fpm
volumes:
- ./StomUp:/var/www/StomUp
- /etc/letsencrypt/live/stomup.ru/:/etc/letsencrypt/live/stomup.ru/
- ./php-fpm/php.ini:/usr/local/etc/php/php.ini
depends_on:
- mysql
container_name: "StomPHP"
web:
image: nginx:latest
ports:
- "80:80"
- "443:443"
volumes:
- ./StomUp:/var/www/StomUp
- /etc/letsencrypt/:/etc/letsencrypt/
- ./nginx/main.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
mysql:
image: mysql:5.7
command: mysqld --sql_mode=""
environment:
MYSQL_ROOT_PASSWORD: xxx
ports:
- "3333:3306"
์ฐ๊ฒฐ๋์๋์? ์ข์ต๋๋ค. ๊ณ์ํ๊ฒ ์ต๋๋ค.
์ด์ ๊ตฌ์ฑ์ ๋ณ๊ฒฝํด์ผ ํฉ๋๋ค. Nginx์ ํจ๊ป ์ผํ๋ค 443 ํญ๊ตฌ์ SSL ์ผ๋ฐ์ ์ผ๋ก:
SSL์ ์ฌ์ฉํ main.conf ๊ตฌ์ฑ ์
#
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name *.stomup.ru stomup.ru;
set $base /var/www/StomUp;
root $base/public;
# SSL
ssl_certificate /etc/letsencrypt/live/stomup.ru/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/stomup.ru/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/stomup.ru/chain.pem;
client_max_body_size 5M;
location / {
# try to serve file directly, fallback to index.php
try_files $uri /index.php$is_args$args;
}
location ~ ^/index.php(/|$) {
#fastcgi_pass unix:/var/run/php7.2-fpm.sock;
fastcgi_pass php:9000;
fastcgi_split_path_info ^(.+.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
internal;
}
location ~ .php$ {
return 404;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
# HTTP redirect
server {
listen 80;
listen [::]:80;
server_name *.stomup.ru stomup.ru;
location / {
return 301 https://stomup.ru$request_uri;
}
}
์ค์ ๋ก ์ด๋ฌํ ์กฐ์ ํ์๋ Docker-compose๊ฐ ์๋ ๋๋ ํฐ๋ฆฌ๋ก ์ด๋ํ์ฌ docker-compose up -d๋ฅผ ์์ฑํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ SSL์ ๊ธฐ๋ฅ์ ํ์ธํฉ๋๋ค. ๋ชจ๋ ๊ฒ์ด ์ด๋ฅํด์ผํฉ๋๋ค.
๊ฐ์ฅ ์ค์ํ ๊ฒ์ Let'sEnctypt ์ธ์ฆ์๊ฐ 90์ผ ๋์ ๋ฐ๊ธ๋๋ฉฐ ๋ค์ ๋ช
๋ น์ ํตํด ์ธ์ฆ์๋ฅผ ๊ฐฑ์ ํด์ผ ํ๋ค๋ ์ ์ ์์ง ์๋ ๊ฒ์
๋๋ค. sudo certbot renew
, ๋ค์ ๋ช
๋ น์ ์ฌ์ฉํ์ฌ ํ๋ก์ ํธ๋ฅผ ๋ค์ ์์ํฉ๋๋ค. docker-compose restart
๋ ๋ค๋ฅธ ์ต์ ์ ์ด ์ํ์ค๋ฅผ crontab์ ์ถ๊ฐํ๋ ๊ฒ์ ๋๋ค.
์ ์๊ฐ์๋ ์ด๊ฒ์ด SSL์ Docker ์น์ฑ์ ์ฐ๊ฒฐํ๋ ๊ฐ์ฅ ์ฌ์ด ๋ฐฉ๋ฒ์ ๋๋ค.
PS ํ
์คํธ์ ์ ์๋ ๋ชจ๋ ์คํฌ๋ฆฝํธ๊ฐ ์ต์ข
์ด ์๋๋ผ๋ ์ ์ ๊ณ ๋ คํ์ญ์์ค. ํ๋ก์ ํธ๋ ํ์ฌ ์ฌ์ธต ๊ฐ๋ฐ ๋จ๊ณ์ ์์ผ๋ฏ๋ก ๊ตฌ์ฑ์ ๋นํํ์ง ๋ง์๊ธฐ ๋ฐ๋๋๋ค. ๊ตฌ์ฑ์ ์ฌ๋ฌ ๋ฒ ์์ ๋ ๊ฒ์
๋๋ค.
์ถ์ฒ : habr.com