經過 Laradock 學 Docker-HTTPS

經過 Laradock 學 Docker-HTTPS

前言

上一篇文章咱們對 laradock 的配置文件有了一個初步的瞭解,如今咱們基本能夠爲所欲爲的配置網站了,以此做爲咱們的開發環境能夠說是方便快捷。php

鑑於愈來愈多的平臺要求接入網站提供 https 協議的接口。如何讓咱們的網站也支持 https,已經迫在眉睫。html

接下來咱們來經過 laradock 學習下,如何讓咱們的網站支持 https 協議。nginx

實現方式

目前來講,讓網站支持 https 大概有兩種方案,一種是向代理證書的公司購買證書,這種方式能夠得到1年以上的服務,固然時間的長度取決於大家之間合約的長短了。web

另外一種方案是經過 let's encrypt 或 symantec 等公司提供的免費證書,這種方式通常合約期短,每三個月須要從新申請一次。docker

laradock 項目中已經集成了 let's encrypt 公司的證書申請工具,咱們來看看如何實現的。shell

配置文檔

docker-compose.yml

咱們找到配置文件中 certbot 的代碼ubuntu

 certbot:
 build:
 context: ./certbot
 volumes:
 - ./data/certbot/certs/:/var/certs
 - ./certbot/letsencrypt/:/var/www/letsencrypt
 environment:
 - CN="fake.domain.com"
 - EMAIL="fake.email@gmail.com"
 networks:
 - frontend
複製代碼

配置裏說明了容器基於 certbot 這個目錄,而後把 laradock/data/certbot/certs 目錄映射到容器內 /var/certs,把 laradock/certbot/letsencrypt 目錄映射到容器內的 /var/www/letsencryptbash

配合文檔,咱們看一下 certbot 目錄中都有什麼文件app

letsencrypt  (dir)
Dockerfile
run-certbot.sh
複製代碼

這裏有一個 letsencrypt 目錄,這個目錄是幹什麼用的咱們先按住不表,一個 dockerfile 和一個 shell 腳本文件。frontend

Dockerfile

FROM phusion/baseimage:latest

MAINTAINER Mahmoud Zalt <mahmoud@zalt.me>

RUN apt-get update
RUN apt-get install -y letsencrypt

COPY run-certbot.sh /root/certbot/run-certbot.sh

ENTRYPOINT bash -c "bash /root/certbot/run-certbot.sh && sleep infinity"
複製代碼

這是一個基於 ubuntu 的基礎鏡像,安裝了 let's encrypt 的自動化證書下載工具。

先將 run-certbot.sh 複製到容器的目標目錄下 (/root/certbot),並執行。

最後經過 sleep infinity 使得容器保持執行狀態。

run-certbot.sh

#!/bin/bash 
letsencrypt certonly --webroot -w /var/www/letsencrypt -d "$CN" --agree-tos --email "$EMAIL" --non-interactive --text

cp /etc/letsencrypt/archive/"$CN"/cert1.pem /var/certs/cert1.pem
cp /etc/letsencrypt/archive/"$CN"/privkey1.pem /var/certs/privkey1.pem
複製代碼

其實這個文件主要命令就一行

letsencrypt certonly --webroot -w /var/www/letsencrypt -d "$CN" --agree-tos --email "$EMAIL" --non-interactive --text
複製代碼

這行命令的是目的是:在 /var/www/letsencrypt 這個(容器中)目錄下生成 well-known/acme-challenge/ 這兩個文件夾,文件夾中成生一個隨機文件,而後 let's encrypt 的服務端去檢測能不能訪問到這個文件,若是能訪問到就說明申請人擁有該域名的控制權,並將證書下載至 /etc/letsencrypt/archive/"$CN"/ 目錄下;若是不能訪問此臨時文件,則說明申請不擁有該域名或配置不正確。而最後兩行則是把證書複製到指定目錄,以便以後將文件映射到本地目錄。

注意:不管可否成功訪問到臨時文件,工具都會在檢測結束後將此文件刪除。

default.conf (80端口)

因此看到這裏咱們就知道了,咱們還不能夠直接去運行這個容器,由於咱們的 nginx 容器還沒法訪問到 certbot 這個容器中的 /var/www/letsencrypt 這個目錄,咱們看一下 nginx 的站點配置文件是怎麼寫的

server {

    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    server_name localhost;
    root /var/www/public;
    index index.php index.html index.htm;

    location / {
         try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass php-upstream;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fixes timeouts
        fastcgi_read_timeout 600;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    location /.well-known/acme-challenge/ {
        root /var/www/letsencrypt/;
        log_not_found off;
    }
}
複製代碼

咱們看到配置文件的最後寫明瞭 /.well-know/acme-challenge/ 這個目錄指向了 /var/www/letsencrypt/ 這個目錄。這樣的話咱們將 certbot 容器中的 /var/www/letsencrypt 和 nginx 容器中的 /var/www/letsencrypt 映射到本地同一目錄就能夠了,而 nginx 容器是基於共用卷 applications 的,那麼咱們只要把 certbot 容器也改到共用卷下就能夠了,也就是以前咱們按住不表的部分。

修改 docker-compose.yml (certbot)

certbot:
      build:
        context: ./certbot
      volumes_from:
        - applications
      volumes:
        - ./data/certbot/certs/:/var/certs
      environment:
        - CN="fake.domain.com"
        - EMAIL="fake.email@gmail.com"
      networks:
        - frontend
複製代碼

請將配置中fake.domain.com改成你本身的域名,並將fake.email@gmail.com改成你本身的郵箱

如此一來,咱們就能夠嘗試運行一次 certbot 容器了

docker-compose up -d certbot
複製代碼

咱們看下運行結果:

  • If you lose your account credentials, you can recover through e-mails sent to fake.domain.com.

  • Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/fake.domain.com/fullchain.pem. Your cert will expire on 2018-06-03. To obtain a new version of the certificate in the future, simply run Let's Encrypt again.

  • Your account credentials have been saved in your Let's Encrypt configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Let's Encrypt so making regular backups of this folder is ideal.

  • If you like Let's Encrypt, please consider supporting our work by:

    Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le

至此咱們的 certbot 容器就運行成功了,並生成了證書文件。咱們只須要接下來把證書文件映射到 nginx 可訪問到的目錄下並在站點配置文件中增長 443 端口訪問的相關配置就能夠了。

修改 docker-compose.yml (nginx)

增長

- ./data/certbot/certs/:/var/certs
複製代碼

修改 default.conf (443端口)

server {

    listen 443;
    listen [::]:443;

    server_name localhost;
    root /var/www/public;
    index index.php index.html index.htm;
	
	ssl on;
    ssl_certificate /var/certs/fullchain1.pem;
    ssl_certificate_key /var/certs/privkey1.pem;
    ssl_trusted_certificate /var/certs/
    
    location / {
         try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass php-upstream;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fixes timeouts
        fastcgi_read_timeout 600;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}
複製代碼

重啓 nginx

docker-compose build nginx
docker-compose restart nginx
複製代碼

結語

咱們這次的配置是針對單一域名,若是想配置多域名,則在拷貝證書文件時多加一層域名目錄就能夠了。

至此咱們就完成了整個配置,如今訪問你設置好的域名就能看到域名前的小綠鎖了。

最後,不要忘了證書的有效期是 3 個月,記得每兩個月重啓下容器或者配置下 crontab,咱們下期再見吧。

能夠與做者交流

相關文章
相關標籤/搜索