May, 09 2020

Laravel Lessons: Hosting, Deployments & Error Tracking

Finally, we are at the end of this blog series. After you have built a web application using Laravel framework, the next thing is for you to do is deploy the app on a web server so that people can access it. Thats the focus of this lesson.

I have written two articles on how to deploy a Laravel application. The first is for deploying on a VPS and the second id for deploying on a shared hosting server.

- Zero Downtime Deployment With Laravel Envoy and BitBucket

- How To Deploy A Laravel Project On Linux Shared Hosting In 5 Minutes

But there is more to deploying a Laravel application. Lets get started.

Environment Variable

Lets start with our .env variables. In the first lesson, I explained some of these variables and how they are supposed to be changed when the application environment changes.

When you are deploying to a production environment these variables should be something like this:

APP_NAME=LiteMailer
APP_ENV=production
APP_KEY=random_strings_as_app_key
APP_DEBUG=false
APP_URL=http://litemailer.test

LOG_CHANNEL=stack 
...

Error Tracking & Monitoring

Most users who see bugs on your website won't even bother to let you know. You need something to monitor and track errors on your web application and the good news is that Laravel have a lot of them(Flare, Sentry, Bugsnag etc).

Error tacking and monitoring softwares informs you of errors that may occur when users start using your application. They provide you with details of the error, the user involved, the stack traces etc. This will improve your finding and fixing bugs as you don't have to wait for users to report errors, they are already there for you as they happen.

Among these three error monitoring and tracking software services, Sentry is free and thats what we are going to use. Lets add it to our LiteMailer app.

Sentry Installation

Open your Laravel project directory on terminal and run the composer require command.

composer require sentry/sentry-laravel

Now that we have the sentry SDK in our project, lets add the sentry error catching code to our LiteMailer app. Go to app/Exceptions/Handler.php file and add this code inside the report() method :

<?php
//app/Exceptions/Handler.php
...

public function report(Throwable $exception)
{
   if (app()->bound('sentry') && $this->shouldReport($exception)) 
   {
       app('sentry')->captureException($exception);
   }
   parent::report($exception);
}

Now head over to sentry.io and create an account. After that you need to create a project and add your sentry DNS to your .env file. Like this:

...
SENTRY_LARAVEL_DSN=https://52f1d0dacfa54c4691d26b7714212bf1@o281527.ingest.sentry.io/5231088

Now we have an error tracker on our app, ready to report any error experienced by our users.

Thats all for now, at least for a beginner, you will learn other things like automated testing, server backup as you continue your journey into your development career.

Questions

You can drop your question on the comment section, send me a DM on twitter or send an email.

Join my inner circle newsletter

Be the first to hear about anything I publish, launch, or think is helpful for you. Subscribe here

Hey, have you tried Litehost lately ?

Litehost is a web hosting platform for PHP & Laravel developers with Composer, Git, PHP & CLI pre-installed. Try it now