ICTについて使える情報を発信していきます. 遠回りしたり, 自分のペースで...

このブログを検索

dockerでサクッとhttpsサーバーを作る

コンカイハ, 簡単にSSL証明書を取得でき, HTTPSサーバーを簡単に作れるlinuxserver/letsencrypt dockerを紹介したいと思います.

linuxserver/letsencrypt dockerの使い方

公式の使い方のブログがあるので, 気になる方は是非そちらもご参照ください. (このブログを探すのにも結構手間取りましたw

まずはdocker-compose.ymlファイルを作成し, 次のように書きます.

version: "2"
services:
  letsencrypt:
    image: linuxserver/letsencrypt
    container_name: letsencrypt
    cap_add:
      - NET_ADMIN
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Asia/Tokyo
      - URL=<yourdomain>
      - SUBDOMAINS=www,
      - VALIDATION=http
      - EMAIL= #optional
      - EXTRA_DOMAINS= #optional
      - STAGING=false #optional true or false
    volumes:
      - ./config:/config
    ports:
      - 443:443
      - 80:80 #optional
    restart: unless-stopped

各environmentの値の意味はこちらに書いてありますが, <yourdomain>のところを, SSL証明書を取得したいドメインに変えるだけで良いと思います.
例えばURL=example-aka.comなど.

次に, docker-composeコマンドを使えるようにします. 公式サイトからインストールするか, 手前味噌ですが, dockerでdocker-composeを動かす記事を参考にしていただければと思います.

あとは実際にdocker-compose run letsencrypt, もしくはdocker-compose upを実行します.
証明書の生成に時間がかかりますのでお待ちください☕︎

$ docker-compose run letsencrypt
[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] 01-envfile: executing... 
[cont-init.d] 01-envfile: exited 0.
[cont-init.d] 10-adduser: executing... 
usermod: no changes

-------------------------------------
          _         ()
         | |  ___   _    __
         | | / __| | |  /  \ 
         | | \__ \ | | | () |
         |_| |___/ |_|  \__/


Brought to you by linuxserver.io
-------------------------------------

To support the app dev(s) visit:
Let's Encrypt: https://letsencrypt.org/donate/

To support LSIO projects visit:
https://www.linuxserver.io/donate/

...

New certificate generated; starting nginx
Starting 2019/12/30, GeoIP2 databases require personal license key to download. Please manually download/update the GeoIP2 db and save as /config/geoip2db/GeoLite2-City.mmdb
[cont-init.d] 50-config: exited 0.
[cont-init.d] 60-renew: executing... 
The cert does not expire within the next day. Letting the cron script handle the renewal attempts overnight (2:08am).
[cont-init.d] 60-renew: exited 0.
[cont-init.d] 99-custom-files: executing... 
[custom-init] no custom files found exiting...
[cont-init.d] 99-custom-files: exited 0.
[cont-init.d] done.
[services.d] starting services
[services.d] done.
nginx: [alert] detected a LuaJIT version which is not OpenResty's; many optimizations will be disabled and performance will be compromised (see https://github.com/openresty/luajit2 for OpenResty's LuaJIT or, even better, consider using the OpenResty releases from https://openresty.org/en/download.html)
Server ready

Server readyが表示されたら準備完了です.
実際に https://yourdomain/ にアクセスしてみましょう.
次のページが表示されたら成功です!

$ curl -L example-aka.com
    <html>
        <head>
            <title>Welcome to our server</title>
            <style>
            body{
                font-family: Helvetica, Arial, sans-serif;
            }
            .message{
                width:330px;
                padding:20px 40px;
                margin:0 auto;
                background-color:#f9f9f9;
                border:1px solid #ddd;
            }
            center{
                margin:40px 0;
            }
            h1{
                font-size: 18px;
                line-height: 26px;
            }
            p{
                font-size: 12px;
            }
            </style>
        </head>
        <body>
            <div class="message">
                <h1>Welcome to our server</h1>
                <p>The website is currently being setup under this address.</p>
                <p>For help and support, please contact: <a href="me@example.com">me@example.com</a></p>
            </div>
        </body>
    </html>

アクセスできない場合はFirewall設定など見てみてください.

以上の手順で簡単にHTTPSサーバーを立てることができました!

まとめ

dockerで簡単にHTTPSサーバーをlinuxserver/letsencrypt Dockerを使ってみました. 証明書の自動更新機能もあるらしいのでとても便利ですね.

ただ, 私がこの記事を書いたきっかけは, 「dockerでサブディレクトリ型のhttps対応リバースプロキシが簡単に作れない!」というのを憤慨してのことなので, 次回はそれを記事にしていこうと思います.

サブドメイン型のhttpsリバースプロキシなら, https-portalとかnginx-reverse-proxyとかもあるので, そちらを試す方が良いかもしれません. (全然更新されていないのが気になるけど.

0 コメント:

コメントを投稿

Popular Posts