Setting up Let's Encrypt


(Alan Tan) #1

So you’d like to add https to your Discourse absolutely free, courtesy of our friends at Let’s Encrypt?

:bell: Is everything else on your site ready for HTTPS?

Before you start, please bear in mind that for HTTPS to work properly, every single resource on the page must be HTTPS compatible. Consider your CDN, your social logins, your logo files, any third party JavaScript, images, fonts, or css — these all must be available over HTTPS!

Note: ./discourse-setup will enable Let’s Encrypt. And as of March 2017, you can run it again, and press return a few times and enter your email address ; the script will include the required templates and insert your email address as required. If you installed Discourse a long time ago, you might still have to edit app.yml by hand.

Note: If your Discourse is accessed via some reverse proxy (e.g., Cloudflare) this will not work.

For the next few steps, you will be editing the app.yml file for your Discourse instance:

cd /var/discourse
nano containers/app.yml

1. Add web.ssl.template.yml and web.letsencrypt.ssl.template.yml to templates

:warning: Is Discourse the only website on your server?

If you are already using web.socketed.template.yml, because you host other websites via port 80 on the same server, stop. You should be using a Let’s Encrypt client on the host system; the validation will fail as the client used is unable to bind to the necessary sockets.

templates:
  - "templates/web.template.yml"
  - "templates/web.ssl.template.yml"
  - "templates/web.letsencrypt.ssl.template.yml"

2. Expose port 443

expose:
  - "80:80"
  - "443:443"

3. Add email account to register with Let’s Encrypt

env:
  LETSENCRYPT_ACCOUNT_EMAIL: 'you@example.com'

4. Rebuild your container

./launcher rebuild <container name>

After that completes, load the site in your browser using https:// – it should “just work!” :tada:

5. Adjust your dependencies for HTTPS

  • Check that all shared resources such as images, logos, third party JavaScript dependencies, etc, are all linked via https:// and not http://

  • Check all your social logins and make sure they are pointing to the https:// and not http:// versions of your site’s authentication URLs.

  • If you are using a CDN, you will need to switch your CDN to use https:// and not http:// to pull resources.

  • If your browser does not show any warnings in its f12 console in the security about insecure assets:

    … you are now ready to force HTTPS by going to

    Admin :arrow_right: site settings :arrow_right: force https

How does it work?

The template uses GitHub - Neilpang/acme.sh: A pure Unix shell script implementing ACME client protocol which is

Simplest shell script for LetsEncrypt free Certificate client

Simple and Powerful, you only need 3 minutes to learn.

Pure written in bash, no dependencies to python , acme-tiny or LetsEncrypt official client. Just one script, to issue, renew your certificates automatically.

Probably it’s the smallest&easiest&smartest shell script to automatically issue&renew the free certificates from LetsEncrypt.

web.letsencrypt.ssl.template.yml adds a startup script to your container that

  1. Issues a Let’s Encrypt cert using the standalone mode. It boots a standalone server that listens on port 80 but this happens before nginx is up so port 80 is free.
  2. Installs the cert into the right directory that nginx expects. At the same time, it adds a cron job that runs a daily cert renewal check. This will automatically renew your cert. Nothing happens if cert has not expired. If the certificate does expire, you’ll get an email about it from Let’s Encrypt at the email address you provided during setup.
  3. Switches the script to use the webroot plugin with /var/www/discourse/public as the directory. This will allow us to use nginx as the server that handles domain validation. Zero downtime during cert renewal!

Debugging

Check logs for cert related errors

./launcher logs <container name>

Then look for any errors mentioning letsencrypt or SSL.

Did the cert files get written OK?

ls -l /var/discourse/shared/standalone/ssl on the host server

You should have a certificate .cer and key .key file for your domain, and they should have ~3k filesize.

-rw-r--r-- 1 root root  424 May 18 03:22 dhparams.pem
-rw-r--r-- 1 root root 3823 May 18 04:24 discourse.example.com.cer
-rw-r--r-- 1 root root 3243 May 18 04:24 discourse.example.com.key

Manually reissue the cert

./launcher enter app
sv stop nginx
/usr/sbin/nginx -c /etc/nginx/letsencrypt.conf
LE_WORKING_DIR=/shared/letsencrypt DEBUG=1 /shared/letsencrypt/acme.sh --issue -d example.com -k 4096 -w /var/www/discourse/public
LE_WORKING_DIR=/shared/letsencrypt /shared/letsencrypt/acme.sh --installcert -d example.com --fullchainpath /shared/ssl/example.com.cer --keypath /shared/ssl/example.com.key --reloadcmd "sv reload nginx"
/usr/sbin/nginx -c /etc/nginx/letsencrypt.conf -s stop

Delete the old cert files and try rebuilding again

rm -rf /var/discourse/shared/standalone/ssl
rm -rf /var/discourse/shared/standalone/letsencrypt
./launcher rebuild app

Limitations

Though the Let’s Encrypt certificate is valid and safe for encrypting data, it does not certify that the site is owned by a particular organization. Firefox will complain that the certificate “does not supply ownership information.” This is a limitation of how Let’s Encrypt works. For more information see: Adding Ownership Information? - Help - Let’s Encrypt Community Support.


Trouble getting Let's Encrypt to work
SEO - Google optimisation through webmaster
Setting up Let's Encrypt for multisite
How should I enable letsencrypt while discourse is beside other websites
Add to homescreen banner on Android
Setting up SSL with my domain name and Discourse instance
German 1&1-hosting user experience?
Setting up Let’s Encrypt with Multiple Domains
Cant setting SLL (Let's Encrypt) for Discourse
Issue installing on subdomain
Trouble getting Let's Encrypt to work
Is https an important thing for discourse website?
Advanced Setup Only: Allowing SSL / HTTPS for your Discourse Docker setup
Site throws a blank screen after trying to enable LetsEncrypt
Favicon failing to load - false positive with 1.8.0.beta9+20?
Configuring certbot for discourse https
Letsencrypt Autorenew?
Failed to bootstrap: Failure with receiving network data
Error when installing ssl Let's Encrypt
How to adjust dependencies for https?
Can not rebuild my app
Discourse has stopped working after LetsEncrypt is enabled
Minimum needed to get LetsEncrypt working on a GCE instance
Its a real struggle to move my team across from Slack, they miss the mobile notifications. how did you solve this?
Enabling Cloudfront free SSL for discourse subdomain hosted on Lightsail
Problem with my SSL certificate
A domain that I don't own redirects to my website
A domain that I don't own redirects to my website
A domain that I don't own redirects to my website
Enabled HTTPS, how do i disable it
Cant setting SLL (Let's Encrypt) for Discourse
[PAID] setup ssl - Let's encrypt
Moving from HTTP to HTTPS with LetsEncrypt on a subdomain
Error after moving from HTTP to HTTPS
How to renew Let's Encrypt?
My forum is opened with another link
My forum is opened with another link
My discourse has either been hacked or catfished?
Let’s Encrypt + non-www > www
How to Set Up SSL in Discourse
Setting up Let's Encrypt in discourse-setup is too fragile
Discourse has stopped opening
Clicking links is stuck in click tracking, shows ERR_FAILED
Missing file(discourse.conf) when launching after lets encrypt update?
Missing file(discourse.conf) when launching after lets encrypt update?
My forum goes offline after removing https
Can discourse be installed in private mode
Is it possible to verify both www and non-www version of my site using LetsEncrypt (using ./discourse-setup file)?
Replacement for whitelist-iframe
Free Cloudflare + Discourse
CNAME or A dns record?
CNAME or A dns record?
Error preview link with https
[SOLVED] 443 address already in use? Letencrypt
Empty .cer file with letsencrypt
Setting up Let’s Encrypt with Multiple Domains
Not able to access site after letsencrypt cert expiry and rebuild due to IPV6
Problem with my SSL certificate
Discourse-saml: There was an error authorizing your account
Setting up Let’s Encrypt with Multiple Domains
Email not sending out after installation. I need help please
Discourse not starting up: nginx: unable to open supervise/ok: file does not exist
Completely automated SSL certificate generation
#96

When discourse is setup as a root domain (e.g. without “www”), the certificate is issued to the root domain only. Is there a way of configuring this so that both the root and the www versions work - e.g. so that we don’t get certificate errors on www.example.com when discourse is setup for example.com.

I believe Neil’s script allows for this using the -d option to specify multiple hosts for the certificate (so a SAN is used in the cert), so possible that we need to cater for this in the web.letsencrypt.ssl.template.yml somehow. Maybe by adding another variable into app.yml (e.g. DISCOURSE_ALT_HOSTNAME: ‘www.example.com’), and then pick this up in the script in web.letsencrypt.ssl.template.yml.

Meanwhile, can we manually run commands to do this without anything being overwritten on restart? Or is it okay to edit the conf file for the domain and set a value for Le_Alt ? If we alter the value for Le_Alt, how do we then force a reissue and install of the cert?

Thanks.


(Alan Tan) #97

Adding multiple domains has been done before. I’ll like to bake this into the template though, we should probably take in an additional env variable and add that into the issue command. Hopefully someone from the community can take this? :stuck_out_tongue:


#98

@tgxworld Thanks for the link. If I make these changes, I’m assuming that the script won’t update the cert until renewal because it will see a valid certificate already installed. How do I force it to update?

Thanks.


(Alan Tan) #99

Run the issue command manually with FORCE=1 :slight_smile:


#100

@tgxworld Thanks for the tip.

I didn’t implement the file changes in app.yml, partly because I wanted to keep the build as standard as much as possible.

However, using the info from that post and your tip on FORCE=1, I did the following (posted here just in case someone else wants to do the same):
./launcher enter app vi /etc/runit/1.d/install_ssl_cert sv stop nginx FORCE=1 /etc/runit/1.d/install_ssl_cert sv start nginx

In the edit of install_ssl_cert, I changed the first line (after #!/bin/bash) to:
LE_WORKING_DIR="/shared/letsencrypt" /shared/letsencrypt/le.sh issue no example.com www.example.com 4096

Checked the certificate after install and it is now complete with and without “www”.

Thanks for your help.


Setting up Let’s Encrypt with Multiple Domains
(ljpp) #155

Additional note to noobs running CloudFlare DNS:

You need to enable Full SSL in CloudFlare settings, or your site won’t work.

Installation wen’t smoothly and running this now on my small side project. Giving it some more time to mature, before enabling on my big site.

Once again, great job guys!


(Philip Colmer) #274

Is it possible to (easily) change when the script agrees to renew the script?

At the moment, it looks like the cert renewal is about 9 days before it expires. In the meantime, Let’s Encrypt has sent me two reminder emails saying that the cert is going to expire.

I’d prefer to have the cert renewed as soon as it possibly can so that I only get the LE reminder emails if, for some reason, the renewal process has failed and I therefore need to investigate further.

Thanks.


(Greg) #314

On my Discourse server, the auto-renew cron job seems to be broken. I’ve grabbed a shell:

./launcher enter app

and then tried to run a renewal myself:

"/shared/letsencrypt"/acme.sh --home "/shared/letsencrypt" --renew-all
[Fri Sep  8 10:50:23 UTC 2017] Renew: 'community.hestiapi.com'
[Fri Sep  8 10:50:23 UTC 2017] Single domain='community.hestiapi.com'
[Fri Sep  8 10:50:23 UTC 2017] Getting domain auth token for each domain
[Fri Sep  8 10:50:23 UTC 2017] Getting webroot for domain='community.hestiapi.com'
[Fri Sep  8 10:50:23 UTC 2017] Getting new-authz for domain='community.hestiapi.com'
[Fri Sep  8 10:50:24 UTC 2017] The new-authz request is ok.
[Fri Sep  8 10:50:25 UTC 2017] Verifying:community.hestiapi.com
[Fri Sep  8 10:50:30 UTC 2017] community.hestiapi.com:Verify error:Invalid response from http://community.hestiapi.com/.well-known/acme-challenge/90IFUOXXSZSmX3O_qjSS-ijnnyFJXMC6ZWYNm-UnuSE: 
[Fri Sep  8 10:50:30 UTC 2017] Please check log file for more details: /shared/letsencrypt/acme.sh.log

Looking in the log (which is pretty huge, but I can paste if needed) it appears to be writing the token, and then failing anyway.

My cert expires tonight (but it’s not a huge deal, we only have a few users), so any advice is appreciated!


(Greg) #315

Of course, as soon as I post, I find the issue. My site only passes 443 to Discourse as other things run on 80 - and of course the renewal has to happen on 80… as soon as I added a vhost on 80 to handle .well-known, all was fine. Thanks anyway!


(Mauro) #324

Do I need to add any 301 redirects to let Google know about the move from http to https? Or it happens automatically?


(Stephen Chung) #325

AFAIK it happens auto-magically. Discourse automatically takes care of the 301 redirects.

That’s why it is No-Brains-Required™. :grinning:


(Jay Pfaffman) #327

I just set up a site with Let’s Encrypt and Firefox is whining that it “does not supply ownership information”.

Screenshot from 2017-10-05 09-32-03

Screenshot from 2017-10-05 09-35-27

But, there’s nothing that can be done, as explained here: Adding Ownership Information? - Help - Let’s Encrypt Community Support


(Pad Pors) #328

we were using this set up in order to have https. but then as we turned off cloudflare, we got Your connection is not secure error. and an expiration date for the ssl certificate.

questions

  1. is the output effect of this howto document the same as this one: Allowing SSL / HTTPS for your Discourse Docker setup? I mean both of them seem to allow for https.

  2. what is about the expiration date of a ssl? nothing about an expiration date is reported in this howto.


(Justin Mullis) #329

I followed this guide after setting up a new discourse “one-click” droplet on Digital Ocean. Just wanted to say thanks, it worked perfectly. Pretty awesome that uncommenting a few lines and restarting gives you SSL for free!


(Ryan Nix) #330

Thanks, Greg. Turns out I only had 443 open to the world as well. Once I opened up 80, voila, I was able to renew the Let’s Encrypt Cert.


(Tumi) #331

Hello, what about cloudflare ? It is better for ssl (and not only ) than lets encrypt ?


(Matt Palmer) #332

No, using Cloudflare for SSL, without also setting up SSL on your server, is terribad – it lulls users into a false sense of security, whilst funnelling your site’s unencrypted traffic through a small number of very attractive targets (Cloudflare data centres).


(Tumi) #333

Thanks for info. I was read about hackers vs cloudflare at february 2017. So i understand. Anyway. There is any pros for using the Cloduflare or not really. If i understand:
SSL - lets encrypt is ok , or any paid ssl
DNS - my own register (like ovh, godaddy) or premium (for 10 dolars) DNS ? This is worth it ?
Anythink else should i get or good to know?


#334

If we are using Cloudflare, can’t we use Let’s Encrypt? Is not there a way?