Laravel tuning

Configure Laravel and run on fortrabbit.

# Get ready

You have already started your Laravel on fortrabbit journey with the setup and continued over deployment. This is the final article to address most common configuration options to successfully run Laravel based applications on fortrabbit.

# Emoji support

Laravel uses the utf8mb4 character set by default, which includes support for storing "emojis 🔥" in the database. You need to manually configure the default string length generated by migrations in order for MySQL to create indexes for them in your AppServiceProvider:

use Illuminate\Support\Facades\Schema;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
  Schema::defaultStringLength(191);
}
php

# Manually running artisan migrate

While it's recommended to run database migrations automated each time you deploy, you can also do that manually. Log in by SSH and execute artisan:

# login and execute
$ ssh {{app-env-id}}@ssh.{{region}}.frbit.app
$ php artisan migrate --force
shell

Note: If APP_ENV is set to production - which is the default - the --force flag for migrate commands will override the confirmation prompt. You can also add this command to your composer.json to have it run automatically every time you push changes.

"scripts": {
  "post-install-cmd": [
    "php artisan migrate --no-interaction --force",
  ],
}
composer.json
json

With that in place, any time you deploy your code, database changes will be applied immediately. If no database changes are required, nothing happens, so it is safe to run all the time. Just make sure to test your upgrades and migrations locally first.

# Logging

Logs for Laravel are stored in storage/logs per default. Our software preset for Laravel however pre-configures log output to stderr and stdout so the logs become available in the dashboard - see the logging article.

If for some reason you can not access the logs in the dashboard, see if you can access these via SSH or SFTP.

See Laravel logging help for more.

# User sessions

There are various session drivers available in Laravel. Whichever driver you end up using, will need to be specified in the environment variables. Add a new ENV var SESSION_DRIVER in the dashboard and give it the appropriate value. Since fortrabbit environments have persistent storage, you are able to use the default file driver for sessions.

To use the database driver, follow these steps:

# Create a migration for the session table  - locally
$ php artisan session:table

# Apply the migration - locally
$ php artisan migrate

# Add, commit and push the migration file
$ git add .
$ git commit -m "session migration"
$ git push

# Run the migration on the App via SSH remote execution
$ ssh {{app-env-id}}@ssh.{{region}}.frbit.app php artisan migrate --force
shell

# Queueing

Laravel supports multiple queue drivers. One which can be used with fortrabbit out of the box is database, which simply uses your database connection as a queue:

# Create a migration for the jobs table locally
php artisan queue:table

# Apply the migration locally
$ php artisan migrate

# Add, commit and push the migration file
$ git add .
$ git commit -m "queue migration"
$ git push

# Run the migration on the App via SSH remote execution
$ ssh {{app-env-id}}@ssh.{{region}}.frbit.app php artisan migrate --force
shell

That's great for small use-cases and tinkering, but if your application handles very many queue messages you should consider using the key-value store.

Once you've decided the queue you want to use, just open config/queue.php and set default to either redis, database, sqs - or even better: set the QUEUE_CONNECTION environment variable accordingly in the dashboard.

To run php artisan queue:work in the background, spin up a new worker job and define the artisan command as a job.

Laravel offers two commands to process queues: queue:work and queue:listen. We recommend using queue:work, and not using queue:listen. This is because the queue:listen command boots the Laravel framework for each iteration, whereas queue:work boots the framework once and runs as a daemon. Using queue:work offers high memory and performance gains in comparison with queue:listen.

# Using artisan down

artisan down generates the file storage/framework/down, which is then checked from your App's HTTP kernel with the CheckForMaintenanceMode middleware. You can run the command run the command via SSH or during deployment.

# Using Laravel Envoy

Easy. Here is an Envoy.blade.php example:

@servers(['fr' => '{{app-env-id}}@ssh.{{region}}.frbit.app'])

@task('ls', ['on' => 'fr'])
  ls -lha
@endtask

@task('migrate', ['on' => 'fr'])
  php artisan migrate
@endtask
php

Then execute locally:

envoy run ls
envoy run migrate
shell

# Sending mail

You can not use sendmail on fortrabbit but Laravel provides an API over the popular SwiftMailer library. The mail configuration file is app/config/mail.php, and contains options allowing you to change your SMTP host, port, and credentials, as well as set a global form address for all messages delivered by the library.

Found a tpyo?Edit