I denne artikel vil jeg dele en metode til at oprette et SSL-certifikat til din webapplikation, der kÞrer pÄ Docker, da jeg ikke kunne finde en lignende lÞsning i den russisktalende del af internettet.

LĂŠs mere nedenfor.
Vi havde docker v.17.05, docker-compose v.1.21, Ubuntu Server 18 og en ren instans af LetâsEncrypt. Det er ikke nĂždvendigt at kĂžre produktion pĂ„ Docker, men nĂ„r du fĂžrst er begyndt at samle Docker, bliver det svĂŠrt at stoppe.
Lad mig starte med de standardindstillinger, vi havde i udviklingsfasen, dvs. uden port 443 og SSL generelt:
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 \/ {
# prĂžv at servere filen direkte, bagefter fallback til 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;
}
Herefter skal vi implementere SSL. Ărligt talt, jeg studerede com-zonen i cirka 2 timer. Alle de foreslĂ„ede lĂžsninger der er interessante, men pĂ„ nuvĂŠrende tidspunkt i projektet havde vi (forretningen) brug for hurtigt og pĂ„lideligt at tilfĂžje SSL LetâsEncrypt pull-requesten pĂ„ GitHub nginx til containeren og ikke mere.
FÞrst installerede vi pÄ serveren certbot
sudo apt-get install certbot
SĂ„ genererede vi wildcard-certifikater til vores domĂŠnet
sudo certbot certonly -d stomup.ru -d *.stomup.ru --manual --preferred-challenges dns
Efter at have kÞrt certbot vil du fÄ 2 TXT-poster, som skal angives i DNS-indstillingerne.
_acme-challenge.stomup.ru TXT {denNĂžgleCertBotGavDig}
Og tryk enter.
Herefter vil certbot kontrollere for disse poster i DNS og oprette certifikater til dig.
Hvis du har tilfĂžjet certifikatet, men certbot det ikke blev fundet â prĂžv at genstarte kommandoen efter 5-10 minutter..
Nu er vi stolte ejere af LetâsEncrypt-certifikatet i 90 dage, men nu skal vi fĂ„ det implementeret i Docker.
For this, we simply link the directories in the docker-compose.yml file under the nginx section.
Example of docker-compose.yml with SSL
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"
Linked? Great â letâs continue:
Now we need to modify the config nginx to work with 443 the port and SSL generelt:
Example of main.conf config with SSL
#
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;
}
}
After these steps, go to the directory with Docker-compose, type docker-compose up -d, and check if SSL is working. Everything should start up.
Just donât forget that the Letâs Encrypt certificate is issued for 90 days, and you will need to renew it using the command sudo certbot renew, and then restart the project with the command docker-compose restart
Alternatively, you can add this sequence to crontab.
In my opinion, this is the simplest way to connect SSL to a Docker web app.
P.S. Please note that all scripts presented in the text are not final; the project is currently in deep development, so I ask you not to criticize the configs â they will change many times.
Kilde: habr.com
