I deployed a Laravel app on Heroku, using Cloudflare for SSL. As a quick note, here’s how I did it.
- Deploy the app on Heroku and make sure everything works fine using the heroku app url.
- Add the domain names to your app in Heroku (in the settings tab for the app). Make sure you add both the root domain and www if you’re using it (example.com, www.example.com). Don’t activate SSL in Heroku.
- Add the site to your Cloudflare account (choose the free plan, when asked).
- Point your domain to Cloudflare by changing the name servers (at the registrars control panel) to the ones Cloudflare gives you when adding the site.
- Wait for the name server changes to go through. It will be notified under the Overview tab on Cloudflare. When this is done you will administer the domain records on Cloudflare instead of your domain regristrar.
- Remove all the DNS records you don’t need, under the DNS tab in Cloudflare. For the next step to work you need to remove the A records for the root domain – since you won’t point it to an IP address, but a domain on Heroku.
- Point Cloudflare to your Heroku app by adding cname records pointing to the Heroku app url.
Like this.Type: CNAME
Name: jymden.com
Domain name: myapp.herokuapp.comType: CNAME
Name: www
Domain name: myapp.herokuapp.com - In Cloudflare, go to the Crypto tab. Set SSL to Full:
- Make sure your Universal SSL certificate is activated. This will happen automatically a little while after adding the site to Cloudflare (up to 24 hours, but usually faster). When it’s activated you’ll see it a bit down in the Crypto tab, like this:
- Prepare your Laravel app to use https by adding this to the boot function AppServiceProvider.php (App/Providers):
public function boot(UrlGenerator $url)
{
if (env('APP_ENV') !== 'local') {
$url->forceSchema('https');
}
}
PHPNOTE: if you’re using Laravel 5.4 or higher it’s forceScheme instead of forceSchema
Also, you need to set your Laravel environment variable APP_ENV to production (or at least something else than local). Do this in the Heroku app settings tab.
Leave a Reply