DEV Community

kyorohiro (kiyohiro kawamura)
kyorohiro (kiyohiro kawamura)

Posted on

Get SSL Cert At Let's Encrypt

In the previous document I created an Http Server without SSL.

In this time, I explain to how to get ssl cert from let's encrypt.

Install certbot

I used certbot to communicate Let'sEncrypt.

$ apt-get install certbot -y
Enter fullscreen mode Exit fullscreen mode

HttpServer For Certbot

Certbot's generate a file for acme-challege under '${WebRoot}/.well-known/acme-challenge/' folder.

and get request 'http://${HOST}/.well-known/acme-challenge/xxx'

  • bin/main.dart

Write a Code at Dart.

import 'dart:io' as io;

const String cerbotWebRootPath = "/var/www/html";

void main(List<String> arguments) async {
  try {
    print("start bind");
    var httpServer = await io.HttpServer.bind("", 80);
    await for (var request in httpServer) {
      try {
        print("receive requested ${request.uri}");
        if (request.uri.path.startsWith("/.well-known/")) {
          var acmeChallengeFilePath = "" +
              cerbotWebRootPath +
              request.uri.path.replaceAll(RegExp("\\?.*"), "");
          acmeChallengeFilePath = acmeChallengeFilePath.replaceAll("/..", "/");
          var acmeChallengeFile = io.File(acmeChallengeFilePath);
          var acmeChallengeData = await acmeChallengeFile.readAsString();
      } catch (e, s) {
  } catch (e, s) {
Enter fullscreen mode Exit fullscreen mode

update server

$ dart2native ./bin/main.dart
$ mv bin/main.exe /opt/main.exe
$ systemctl restart darthelloserver 
Enter fullscreen mode Exit fullscreen mode

and, execute certbot at webroot mode

$ certbot certonly --webroot -w /var/www/html -d -m
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for
Using the webroot path /var/www/html for all unmatched domains.
Waiting for verification...
Cleaning up challenges

 - Congratulations! Your certificate and chain have been saved at:
   Your key file has been saved at:
   Your cert will expire on 2021-07-02. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:
   Donating to EFF:          
Enter fullscreen mode Exit fullscreen mode

all code

Top comments (1)

kyorohiro profile image
kyorohiro (kiyohiro kawamura)

if you are interesting about certbot in action.
check here . so minimum acme implements.