Install Laravel 5

Laravel is the most PHPopular framework. Learn how to install and tune Laravel 5 on fortrabbit.

Get ready

Please make sure to have followed our get ready guide before starting here. You will also need a local Laravel installation.

Quick start

Execute the following in your local terminal to start from scratch with a fresh new Laravel installation on fortrabbit (see below on how to add an existing project):

# 1. Use Composer to create a local Laravel project named like your App
$ composer create-project laravel/laravel --prefer-dist {{app-name}}
# this installs Laravel locally and will take a while

# 2. Change into the folder
$ cd {{app-name}}

# 3. Initialize a local Git repo
$ git init .

# 4. Add all files
$ git add -A

# 5. Commit files for the first time
$ git commit -m 'Initial'

# 6. Add fortrabbit as a remote
$ git remote add fortrabbit {{ssh-user}}@deploy.{{region}}.frbit.com:{{app-name}}.git

# 7. Initial push and upstream
$ git push -u fortrabbit master
# this will install Laravel on remote and take another while

# The next deployments will be much faster
# 8. Push from now on only
$ git push

Got an error? Please see the access troubleshooting. Did it work? Cool, when this is done, you can visit your App URL in the browser to see the Laravel welcome screen:

Tune

Until now this is a vanilla Laravel. Now, make it yours.

MySQL

Use App secrets to attain database credentials. Replace all contents from config/database.php in your editor like so:

<?php
// locally: use standard settings
$mysql = [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),
    'database'  => env('DB_DATABASE', 'forge'),
    'username'  => env('DB_USERNAME', 'forge'),
    'password'  => env('DB_PASSWORD', ''),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
    'engine'    => null,
];


// on fortrabbit: construct credentials from App secrets
if (isset($_SERVER['APP_SECRETS'])) {
    $secrets = json_decode(file_get_contents($_SERVER['APP_SECRETS']), true);
    $mysql = [
        'driver'    => 'mysql',
        'host'      => $secrets['MYSQL']['HOST'],
        'port'      => $secrets['MYSQL']['PORT'],
        'database'  => $secrets['MYSQL']['DATABASE'],
        'username'  => $secrets['MYSQL']['USER'],
        'password'  => $secrets['MYSQL']['PASSWORD'],
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ];
}

return [
    'fetch'         => PDO::FETCH_OBJ,
    'default'       => env('DB_CONNECTION', 'mysql'),
    'connections'   => [
        'mysql' => $mysql,
    ],
    'migrations' => 'migrations'
    // possible other code …
];

This example contains environment detection, so the App can run on your local machine with your local database, as well as with the one on fortrabbit.

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);
}

MySQL access from local

Please see the MySQL article on how to access the database remotely from your computer.

Update database with artisan migrate command

You can execute remote commands via SSH, for example:

$ ssh {{ssh-user}}@deploy.{{region}}.frbit.com 'php artisan migrate'
$ ssh {{ssh-user}}@deploy.{{region}}.frbit.com 'php artisan migrate:rollack'
$ ssh {{ssh-user}}@deploy.{{region}}.frbit.com 'php artisan tinker'

If APP_ENV is set to production - which is the default - then Laravel expects --force for migrate commands.

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",
    ],
}

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.

Database Session

Using the database driver is the easiest way to persist sessions cross multiple PHP nodes. Since it requires a migration it is a good exercise.

# Create the session table via artisan command locally
$ php artisan session:table

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

# Run the migration
$ ssh {{ssh-user}}@deploy.{{region}}.frbit.com php artisan migrate --force

Add a new ENV var SESSION_DRIVER with the value database in the Dashboard to make use of the database sessions.

Go to ENV vars for the App: {{app-name}}

Object Storage

fortrabbit Apps have an ephemeral storage. If you require a persistent storage, for user uploads or any other runtime data your App creates, you can use our Object Storage Component. Once you have booked the Component in the Dashboard the credentials will become available via the App secrets.

To make your App access the Object Storage, open up config/filesystems.php and modify it as following:

// construct credentials from App secrets, when running on fortrabbit in production
$secrets = json_decode(file_get_contents($_SERVER['APP_SECRETS']), true);

return [
    // other code …
    'disks' => [
        // other code …
        's3' => [
            'driver'   => 's3',
            'key'      => $secrets['OBJECT_STORAGE']['KEY'],
            'secret'   => $secrets['OBJECT_STORAGE']['SECRET'],
            'bucket'   => $secrets['OBJECT_STORAGE']['BUCKET'],
            'endpoint' => 'https://'. $secrets['OBJECT_STORAGE']['SERVER'],
            'url'      => 'https://'. $secrets['OBJECT_STORAGE']['HOST'],
            'region'   => $secrets['OBJECT_STORAGE']['REGION'],
        ],
        // other code …
    ],
    // other code …
];

If you want to use the Object Storage with your fortrabbit App and a local storage with your local development setup then replace the "default" value in filesystems.php as well. For example like so:

// other code ….
'default' => env('FS_TYPE', 'local'),
// other code …

Now set FS_TYPE in your local .env file to the value local and the environment variables in the Dashboard to the value s3.

An alternative to our Object Storage Component is Amazon S3 and we have written up a guide to get your started.

Laravel Mix

You can use Mix locally - not on fortrabbit as there is no Node on remote. You can extend the Mix with the webpack-s3-plugin to export your minified assets to the Object Storage. This is how it works. To start, execute in your terminal:

# Get your Object Storage credentials
$ ssh {{app-name}}@deploy.{{region}}.frbit.com secrets OBJECT_STORAGE

Then put the values to your .env file an prefix the keys with OBJECT_STORAGE_. In your webpack.mix.js you load the plugin and configure it with the env vars:

let mix = require('laravel-mix');
let S3Plugin = require('webpack-s3-plugin')

// ...

mix.js('resources/assets/js/app.js', 'public/js')
   .sass('resources/assets/sass/app.scss', 'public/css');

// S3Plugin config
if (process.env.npm_config_env === 'production') {
    mix.webpackConfig({
        plugins: [
            new S3Plugin({
                s3Options: {
                    accessKeyId: process.env.OBJECT_STORAGE_KEY,
                    secretAccessKey: process.env.OBJECT_STORAGE_SECRET,
                    endpoint: process.env.OBJECT_STORAGE_SERVER,
                    region: process.env.OBJECT_STORAGE_REGION
                },
                s3UploadOptions: {
                    Bucket: process.env.OBJECT_STORAGE_BUCKET
                },
                directory: 'public'
            })
        ]
    });
}

Back in your terminal:

# Install package via NPM
$ npm install webpack-s3-plugin --save

# Run the publish task
$ npm run production --env=production

Mind that you need to tell your source code to look for the minified CSS & JS files on the offshore Object Storage.

Logging

Per default Laravel writes all logs to storage/log/... Since you don't have direct file access, you need to configure Laravel to write to the PHP error_log method instead.

In Laravel 5.6

Laravel's new logging.php config allows you to define various log channels. Make sure to add the errorlog channel to the stack or simply set the default channel via ENV var:

LOG_CHANNEL=errorlog

In Laravel 5.1 - 5.5

That's easily done: open boostrap/app.php and add the following just before the return $app statement at the bottom:

$app->configureMonologUsing(function($monolog) {
    // chose the error_log handler
    $handler = new \Monolog\Handler\ErrorLogHandler();
    // time will already be logged -> change default format
    $handler->setFormatter(new \Monolog\Formatter\LineFormatter('%channel%.%level_name%: %message% %context% %extra%'));
    $monolog->pushHandler($handler);
});

You can now use our regular log access to view the stream.

Setting time zone in Laravel

As Eloquent uses PDO, you can use the PDO::MYSQL_ATTR_INIT_COMMAND option. Extend your mysql configuration array in app/config/database.php or your specific environment database.php file:

return [
    // other code …
    'mysql' => [
        // other code …
        'options'   => [
            \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET time_zone = \'+02:00\''
        ]
    ],
    // other code …
];

Memcache

Make sure you enabled the Memcache Component. Then you can use the App Secrets to attain your credentials. Modify the memcached connection in your config/cache.php like so:

// locally: use standard settings
$servers = [[
    'host' => env('MEMCACHED_HOST', '127.0.0.1'),
    'port' => env('MEMCACHED_PORT', 11211),
    'weight' => 100,
]];

// on fortrabbit: construct credentials from App secrets
if (isset($_SERVER['APP_SECRETS'])) {
    $secrets = json_decode(file_get_contents($_SERVER['APP_SECRETS']), true);
    $servers = [[
        'host' => $secrets['MEMCACHE']['HOST1'],
        'port' => $secrets['MEMCACHE']['PORT1'],
        'weight' => 100
    ]];
    if ($secrets['MEMCACHE']['COUNT'] > 1) {
        $servers []= [
            'host' => $secrets['MEMCACHE']['HOST2'],
            'port' => $secrets['MEMCACHE']['PORT2'],
            'weight' => 100
        ];
    }
}

// other code …

return [
    // other code …
    'stores' => [
        // other code …
        'memcached' => [
            'driver'  => 'memcached',
            'servers' => $servers
        ],
        // other code …
    ],
    // other code …
];

In addition, set the CACHE_DRIVER environment variable so that you can use memcached in your production App on fortrabbit and apc or array on your local machine, via .env file.

Redis

Redis can be used in Laravel as a cache or a queue or both. First integrate with Redis Cloud then configure the redis database connection in config/database.php:

// locally: use standards
$redis = [
    'host'     => env('REDIS_HOST', 'localhost'),
    'password' => env('REDIS_PASSWORD', null),
    'port'     => env('REDIS_PORT', 6379),
    'database' => 0,
];

// on fortrabbit: construct credentials from App secrets
if (isset($_SERVER['APP_SECRETS'])) {
    $secrets = json_decode(file_get_contents($_SERVER['APP_SECRETS']), true);
    $redis = [
        'host'     => $secrets['CUSTOM']['REDIS_HOST'],
        'port'     => $secrets['CUSTOM']['REDIS_PORT'],
        'password' => $secrets['CUSTOM']['REDIS_PASSWORD']
    ];
}

return [
    // other code …
    'redis' => [
        'cluster' => false,
        'default' => $redis
    ],
    // other code …
];

If you plan on using Redis as a cache, then open config/cache.php and set default to redis (or set the CACHE_DRIVER environment variable to redis in the Dashboard). For queue usage see below.

Queue

Laravel supports multiple queue drivers. One which can be used with fortrabbit out of the box is database, which simple uses your database connection as a queue. That's great for small use-cases and tinkering, but if your App handles very many queue messages you should consider Redis.

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_DRIVER environment variable accordingly in the Dashboard.

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

Make sure you have added VladimirYuldashev\LaravelQueueRabbitMQ\LaravelQueueRabbitMQServiceProvider::class to providers in config/app.php.

Lastly set the QUEUE_DRIVER environment variable in the Dashboard to rabbitmq.

Using envoy

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

@servers(['fr' => '{{ssh-user}}@deploy.{{region}}.frbit.com'])

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

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

Then execute locally:

$ envoy run ls
$ envoy run migrate

Sending mail

You can not use sendmail on fortrabbit but Laravel provides a 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.

Using artisan down

artisan down generates the file storage/framework/down, which is then checked from your App's HTTP kernel as middleware — as far as we know. Modifying files via SSH remote execution does only affect the deployment Node, not your live App (i.e. any file changes via SSH remote exec do not affect your App).

There are at least two options to do this:

  1. Add artisan down as a post-install-cmd script in composer.json, then git push (remove the command and push again to bring it back online)
  2. Use a custom middleware and command which uses another source than a file, eg memcache or database

Add an existing project

You can also push your existing Laravel installation to fortrabbit. When you already using Git, you can add fortrabbit as an additional remote, like described above under point 6. When moving from another host to fortrabbit, please also read our migration guide as well.

Further readings

Craft CMS

Install guides

Develop & deploy

Teamwork

Platform

Stacks

Need individual help?
Learn about Company plans ›
Looking for an old article?
See the full list of articles ›
Found an error?
Contribute on GitHub ›