How to get automatic SSL for multi tenant applications and websites
Storyblok is the first headless CMS that works for developers & marketers alike.
In this tutorial you will learn how to setup OpenResty/nginx with on the fly Let's Encrypt SSL certificate registration and renewal on a Amazon EC2 instance. Point your domain to the IP address of the server and you never need to worry again about SSL. No cronjobs, just pure power of Nginx and Lua.
Starting a Amazon EC2 instance
First we choose a EC2 instance with Amazon Linux and create it with the default settings.
Copy the IPv4 Public IP from the EC2 instance dashboard and connect to your instance using your private key.
Install OpenResty
We will need to install OpenResty on the remote instance. What is OpenResty®? OpenResty® is a full-fledged web platform that integrates the standard Nginx core, LuaJIT and many carefully written Lua libraries. Lua gives OpenResty/nginx the power to make automatic SSL possible.
On newer Amazon machines I got following error installing OpenResty:
To fix that error you need to edit the repo file with sudo vim /etc/yum.repos.d/openresty.repo
and exchange the $releasever placeholder of the baseurl to "latest" baseurl=https://openresty.org/package/amazon/latest/$basearch
.
Install LuaRocks
LuaRocks is the package manager we need to install the lua-resty-auto-ssl package.
Install ggc
https://github.com/GUI/lua-resty-auto-ssl requires ggc for the installation process so we install it with yum.
Setup a user group
As lua-resty-auto-ssl needs to write to the directory /etc/resty-auto-ssl we'll add the user group www to our ec2-user.
Install lua-resty-auto-ssl
Using the package manger luarocks we install lua-resty-auto-ssl and create the directory where the library will write it's files to.
Generate a self signed fallback certificate
We will need a self signed fallback certificate as a fallback to be able to start nginx.
Edit nginx.conf
After backing up our original nginx.conf we open vim to insert the required configuration for the server.
Insert following content to the nginx.conf
Start OpenResty
As the final step we start OpenResty as a system service.
Change DNS record
To test it out point your domain or subdomain to the IP address of your EC2 instance and open the browser with **https://**subdomain.yourdomain.com.
Debugging
If you get an error or a invalid certificate checkout what's happening tailing the nginx error.log. I had some directory rights issues and found it out by watching the error.log while reloading the website with https.
Log rotation
To enable log rotation for Resty we also need to add a logrotate configuration like following:
Then create the logrotation cronjob
Conclusion
Thanks to Let's Encrypt and OpenResty I have now a server that can provide automatic SSL for all of my domains. The only thing I need to do is to point my domain to the IP address of the server. The Nginx server can act as a proxy to forward the requests to other servers which makes this setup perfect for multi tenant applications where multiple domains point to the same server.
Resource | Link |
---|---|
lua-resty-auto-ssl | https://github.com/GUI/lua-resty-auto-ssl |
OpenResty | http://openresty.org |
Let's Encrypt | https://letsencrypt.org |