Managing SSL Certificates with Amazon AWS Lightsail instance with Apache2 webservice
5 minute read
This post is for enabling a SSL certificate for free from Cloudflare.com. Say, you purchased a domain name (e.g., example.com from Godaddy). Now, you want an inexpensive way to host your website. Again, say you hosted the website at Amazon AWS Lightsail (either $3.50/mo or $5.00/mo as of the time of writing) with configuration like: {Platform: Linux/Unix, blueprint:Os only, OS:Ubuntu 22.04}. You then bind a AWS provided static IP with the instance and create a DNS zone with couple of DNS records like: {A records:[Record name:example.com,Route traffic to:1.2.3.4],[Record name:*.example.com,Route traffic to:1.2.3.4]}. Also assign the created DNS zone to the instance. Finally, copy the AWS provided list of Name servers into Godaddy’s Nameservers management page for your purchased domain while removing existing ones. And, you host your website files with Apache2 in your Lightsail instance. Here is a post I wrote on how to configure a site with apache2.
Pre-requisites
- You have done everything I said in the paragraph above.
Steps to enable SSL in your lightsail hosted website
- Signup a free account at Cloudflare.com, if you have no prior accounts there.
- Login at Cloudflare.
Add a siteinto your Cloudflare dashboard and enter the site name:example.comFree Planis the plan you’d select, and clickGet started. The free plan option can be anywhere in theSelect a planpage. So, please be curious enough to find it.- Then, Cloudflare would perform a quick scan to show you current DNS records of your domain. Make a note of the summary, as you will have to come back to re-configure this at a later step.
- Hit the
Continuebutton.
- Cloudflare will offer you few steps to
change your nameservers. For instance,- Determine your registrar via WHOIS
- Login to Godaddy (in my case it was the registrar)
- Remove the existing nameservers (i.e., the ones you got from AWS lightsail)
- Add the Cloudflare provided nameservers.
- Save your changes and wait (couple of minutes; atmost 24 hours). Check the WHOIS lookup again to see if the nameserver updates are completed or not.
- Once Cloudflare determines that your domain’s nameservers are updated accordingly, you’ll receive a
Status activeemail, and also see the expected changes in the WHOIS lookup. - At the Cloudflare dashboard do the following:
DNSpane:- Remove existing
DNS records. Add record: {type:A,Name:*,IPv4 address:1.2.3.4}- This specific
A recordis to enable domain name mapping for services likessh, orpingetc.
- This specific
Add record: {type:A,Name:@,IPv4 address:1.2.3.4}Add record: {type:CNAME,Name:www,IPv4 address:1.2.3.4}
- Remove existing
SSL/TSLpane:- At the
Overviewsub-pane: choose the optionFull (strict) - At the
Origin Serverssub-pane:create a certificate.- Choose option
Generate private key and CSR with Cloudflare. SelectRSA (2048)as the private key type. Make sure the hostnames are selected as:*.example.com,example.com. You have option choose even15 yearsas the validity period for the generated SSL certificate. Then, hit theCreatebutton. - Copy the content of
Origin certificatecontent into a file named,example.com.pem - Copy the content of
Private keycontent into a file named,example.com.key
- Choose option
- At the
Edge Certificatespane:- Configure status of the generated SSL certificate. Don’t worry about the expiry date as it’s going to be auto-renewed every 3 months for 15 years (if you chose that option in the previous step).
Always Use HTTPS: Turn this option toONposition.Automatic HTTPS Rewrites: Turn this option toONposition.
Overviewpane:- (Temporarily) turn on the
Development modeto disable the caching.
- (Temporarily) turn on the
- At the Lightsail console:
Instance>Networkingtab: at theIPv4 Firewallsection add a new rule for theHTTPS: {Application:HTTPS,Protocol:TCP,Port or range:443}- Copy the
example.com.pemandexample.com.keyfiles into/etc/ssl/directory. - Assuming
example.com.confis available at the/etc/apache2/sites-available/directory. If not, make sure the following is configured in the/etc/apache2/sites-available/example.com.conf:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.exammple.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
- Now, create the
example.com-ssl.conffile with SSL configuration:
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/example.com
SSLEngine on
SSLCertificateFile /etc/ssl/example.com.pem
SSLCertificateKeyFile /etc/ssl/example.com.key
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
- Tell ubuntu system to use it by:
sudo a2ensite example.com.confsudo a2ensite example.com-ssl.conf- You may need to remmove the default ones, depending on your use case.
sudo a2enmod ssl- Reboot the
apache2service:sudo systemctl restart apache2
Testing if the SSL/HTTPS is active for your site
- Open a browser and go
example.com- It should automatically go to
https://example.com, and you’d see thelockicon at the browser’s address bar noting the connection is secure. You can inspect the certificate to review the details.
- It should automatically go to
Possible Hiccups while going through the steps above
- Issue 1: 522 timeout received with the website host not responding.
- Solution: Either
apache2is not configured properly to serve anyhttpsrequests, or your lightsail instance’sipv4 firewallis blocking any443port requests which is essential forhttps.
- Solution: Either
- Issue 2: website not loaded / recursive forward error.
- Solution: Please do not add any additional page forwarding rules to switch from
httptohttpsas atCloudflaredashboard we have already configured options likealways use https, etc.
- Solution: Please do not add any additional page forwarding rules to switch from