Home » Wiki » How to Install an SSL Certificate on Heroku Server

How to Install an SSL Certificate on Heroku Server

by | SSL Installation Guides

How to Install an SSL Certificate on Heroku Server

Installing an SSL certificate on Heroku SSL

Securing your Heroku app with SSL/TLS encryption is important for protecting sensitive user data and preventing security vulnerabilities. SSL (Secure Sockets Layer) encrypts communications between a user’s browser and your Heroku server so that data cannot be intercepted and read by third parties. In this comprehensive guide, we will cover everything you need to know about how to install SSL Certificate on Heroku Server.

Prerequisites

Before you can install an SSL certificate on Heroku, you will need:

  • A Heroku account
  • The Heroku CLI installed on your local machine
  • Ownership and control of a custom domain name (or subdomain) that points to your Heroku app
  • Access to your domain name registrar’s control panel to configure DNS settings
  • An SSL certificate issued by a trusted Certificate Authority (CA) like Let’s Encrypt, DigiCert, etc.
  • If you don’t already have an SSL certificate, you can get a free one from Let’s Encrypt. Make sure the certificate includes your custom domain name.

A Step-by-Step Guide to Install SSL Certificate on Heroku Server

Installing an SSL certificate on Heroku involves a few key steps:

Step #1: Add the Domain to Your Heroku App

Step #2: Configure DNS Records at Your Registrar

Step #3: Add the SSL Certificate to Heroku

  • Using the Heroku CLI:
  • Using the Heroku Dashboard:

Step #4: Enable Automated Certificate Renewal

Step #5: Redirect HTTP to HTTPS

Step #1: Add the Domain to Your Heroku App

First, you need to add your custom domain name to your Heroku app in the dashboard or via the Heroku CLI:

heroku domains:add www.yourdomain.com

This associates the domain name with your Heroku application.

Step #2: Configure DNS Records at Your Registrar

Next, you need to configure some DNS records at your domain name registrar to point the domain to Heroku servers:

  • Create an A record that points your domain to 75.101.145.87
  • Create an ALIAS or ANAME record for www that points to your Heroku app URL (e.g. myapp.herokuapp.com)

This step tells the internet to route traffic for your domain to your Heroku app.

Step #3: Add the SSL Certificate to Heroku

Once your domain is associated and DNS records are configured, you can add the SSL certificate to your Heroku app:

Option 1 – Using the Heroku CLI:

heroku certs:add server.crt server.key

This uploads the certificate server.crt and private key server.key files to Heroku.

Option 2 – Using the Heroku Dashboard:

  • Go to the ‘Settings’ tab in your Heroku app.
  • Under ‘Domains’ click ‘Add certificate’
  • Upload your certificate and private key files.

Either method will attach the SSL certificate to your Heroku app.

Step #4: Enable Automated Certificate Renewal

Your certificate will need to be renewed every 12 months to keep it valid. Enable Heroku’s automated certificate renewal to automatically renew Let’s Encrypt certificates:

heroku certs:auto:enable

This prevents any certificate expiration issues.

Step #5: Redirect HTTP to HTTPS

Finally, redirect all HTTP traffic to HTTPS to ensure all connections are encrypted:

heroku labs:enable http-status-codes
heroku headers:set Http-Status-Code-Protocol-Enforcement:redirect-to-https

And that covers the complete setup process! Your Heroku app is now serving traffic over secure SSL.

Securing Subdomains

If you used a Wildcard SSL certificate, you can secure additional subdomains simply by adding them to your Heroku app:

heroku domains:add subdomain.yourdomain.com

The wildcard SSL certificate will automatically cover the new subdomain.

For single domain certificates, you’ll need to add and install a new certificate for each additional subdomain you want to support.

Why Use SSL on Heroku?

There are several important reasons to use SSL on your Heroku application:

  • Data Security: SSL encrypts all data transmitted between the server and users’ browsers. This prevents malicious third parties from intercepting and stealing sensitive information like passwords, credit card details, personal info, etc.
  • User Trust: Users expect sites to use HTTPS nowadays. Enabling SSL demonstrates your commitment to security and helps build user trust in your application.
  • SEO Rankings: Google and other search engines give higher rankings to secure HTTPS websites over insecure HTTP sites. Having SSL improves SEO and can help drive more organic traffic to your app.
  • Compliance: Many industry compliance standards like PCI DSS require the use of encryption for handling sensitive data. SSL helps meet compliance requirements.
  • Browser Warnings: Modern browsers display warnings when users try to access HTTP sites as opposed to HTTPS. SSL eliminates these warnings.

Troubleshooting Issues

Here are some common issues and fixes when configuring SSL on Heroku:

  • Site Showing Certificate Warnings or Errors: This is likely caused by an incorrect or missing SSL certificate configuration. Double check that the certificate is installed properly on Heroku and covers the domain being accessed.
  • Insecure Connection Warnings: If users are still getting warnings about insecure connections, make sure you enable the HTTP to HTTPS redirect in Heroku. This forces all traffic over SSL.
  • Certificate Renewal Failures: Verify that automated certificate renewal is enabled in Heroku, so certificates get renewed before they expire. Check for any error messages and resolve any issues.
  • SSL Works Intermittently: Flaky SSL connections can occur if the DNS records are misconfigured or not propagated fully. Validate that the domain A record and ALIAS/ANAME records are set correctly. Give DNS changes time to propagate.
  • Custom Subdomain Not Secured: Subdomains must be explicitly added to the Heroku app’s domain list before they will be covered by a wildcard certificate. Double check each subdomain is configured.
  • Browser Gives HSTS Errors: If users get HSTS errors after enabling SSL, it is likely caused by previous visits over HTTP being cached by the browser. This can be fixed by clearing the browser cache.

Conclusion

In conclusion, installing and configuring SSL certificates on Heroku enables vital encryption and security for your application. By following best practices like using trusted certificates, redirecting HTTP to HTTPS, configuring DNS properly, and renewing certificates automatically, you can ensure your Heroku app is served securely over HTTPS. Troubleshooting certificate issues thoroughly and utilizing tools like OpenSSL helps resolve problems. With SSL properly set up on your custom domain, you can rest easy knowing your Heroku application and sensitive user data is protected through transport layer security. Use this comprehensive guide to fully secure communications to your Heroku app.

FAQs

Do I need a paid Heroku plan to use SSL?

No, all Heroku plans including the free tier support adding SSL certificates.

Can I use a self-signed certificate instead of paying for one?

It is not recommended. Self-signed certificates will trigger browser warnings and errors. You should use a valid certificate signed by a trusted CA.

What happens when my certificate expires?

The certificate will stop working which breaks your site’s HTTPS and triggers browser errors. Enable automated renewals so Heroku automatically renews Let’s Encrypt certificates.

How long do SSL certificates last?

The standard validity period for most certificates is one year. Wildcard certificates typically last 1-2 years before requiring renewal.

Can I use SSL for free on Heroku?

Yes, you can get free SSL certificates from Let’s Encrypt which are trusted by all browsers. The only cost is your time to configure it.

Do I need dedicated IP addresses for SSL on Heroku?

No, Heroku’s SSL implementation works fine with shared IP addresses. You do not need to purchase dedicated IPs.