Heroku w/ Subdomains

The easiest way to deploy the frontend that uses the multi-tenant with subdomains strategy is probably on Heroku.

Requirements

  • Tenant Mode set to Multi w/ Subdomain.

  • Custom Domain

  • Heroku Account with billing enabled

Update the frontend configuration

  • frontend/src/config/production.tsx (For React and Vue)

  • frontend/src/environments/environment.production.ts (For Angular)

Replace the backendUrl variable for the corresponding backend server URL.

Don't forget the/api suffix.

Replace the frontend.host for your custom domain.

// Place the URL here with the /api suffix.
// Ex.:`https://domain.com/api`;
const backendUrl = `https://your_production_backend_url/api`;

const frontendUrl = {
 host: 'scaffoldhub-demo.xyz',
 protocol: 'https',
};

Add the server files to your project

Heroku does not have a way of hosting static files, so we must set an ExpressJS server to serve our static files.

frontend/Procfile

frontend/server.js

Replace Line 5 with the one for the corresponding front-end framework you are using.

The server.js needs express and express-history-api-fallback.

Heroku starts the application by running npm start, so you must change the start script to run the ExpressJS server file.

frontend/package.json

Now if you want to start the frontend, you will have to run npm run start:frontend .

Initialize a GitHub repository

Go to the frontend repository and run:

Create a Heroku project

Install the Heroku CLI

Sign in to Heroku

Create a new application

Make sure you are at the frontend repository an run:

You will then receive the app URL. Save it for accessing later.

Deploy the app

Push the code to Heroku and deploy it.

Great! The app is now online.

Adding a wildcard domain

To activate the subdomain capabilities, you'll have to add a wildcard Custom Domain.

More information at https://devcenter.heroku.com/articles/custom-domains#add-a-wildcard-domain.

Configuring the DNS

Add the CNAME record on the custom domain provider DNS.

To discover the CNAME, run:

For more information, read https://devcenter.heroku.com/articles/custom-domains#configuring-dns-for-wildcard-domains.

You must also add a URL Redirect Record to point your root domain to www.

I use Namecheap as the domain provider, so my DNS configuration starts looking like this:

It might take a few minutes, but you will be ready to use your URL without SSL.

It will be accessible via http.

Configuring the SSL certificate

Because of the wildcard, Heroku does not automatically assign a certificate for us. We must create it using CertBot.

Install CertBot: https://certbot.eff.org/.

After installed, run the command below, changing the YOUR_CUSTOM_DOMAIN for your domain.

Follow the instructions. You will have to create a DNS TXT record.

Before hitting next, make sure the TXT variables are configured correctly using a tool like https://mxtoolbox.com/SuperTool.aspx. Make sure you have TXT Lookup selected.

Ok, now you have your certificate saved at /etc/letsencrypt/live/your_custom_domain.

Upgrade your Heroku dynos to Hobby

This is required to upload SSL certificates.

Upload the certificate to Heroku

More information at https://devcenter.heroku.com/articles/ssl#manually-uploading-certificates-and-intermediaries.

Done! Now your application can be accessed via subdomains and with https.

Last updated

Was this helpful?