Install Laravel 6

Laravel is the most PHPopular framework. Learn how to install and tune Laravel 6 on fortrabbit Universal Apps.

Get ready

Please make sure to have followed our get ready guide before starting here.

Quick start

Following the fastest way to start with a fresh installation. Please scroll below for migrating an existing Laravel. Execute the following in your terminal on your local machine:

# 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 with Composer 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. Push changes to fortrabbit
$ git push -u fortrabbit master
# this installs Laravel with Composer on remote and take while

# the next deployments will be much faster
# 8. Push from now on
$ 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:

Setup

Don't stop with a plain vanilla installation. Make it yours! Check out the following topics if you have an existing Laravel installation or if you would like to setup Laravel so that you can run in a local development environment as well as in your fortrabbit App:

Setup for an existing code base

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.

MySQL configuration

If you have chosen Laravel in the Software Preset when creating your App, we will automatically populate the "right" environment variables for the MySQL connection. So, you don't need to set anything! Just keep config/database.php as it is. Here is the source, for reference only:

<?php
return [
    'connections'   => [
        'mysql' => [
             'driver' => 'mysql',
             'host' => env('DB_HOST', '127.0.0.1'),
             'port' => env('DB_PORT', '3306'),
             'database' => env('DB_DATABASE', 'forge'),
             'username' => env('DB_USERNAME', 'forge'),
             'password' => env('DB_PASSWORD', ''),
             'unix_socket' => env('DB_SOCKET', ''),
             'charset' => 'utf8mb4',
             'collation' => 'utf8mb4_unicode_ci',
             'prefix' => '',
             'prefix_indexes' => true,
             'strict' => true,
             'engine' => null,
         ],
    ],
];

The all CAPITAL configs above are what is going to be replaced with contents from the environment variables. For your local development setup you can populate the .env with your local database credentials. See our ENV var article as well. We are also offering an alternative more secure setup using App secrets, see here.

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

Database import and export

There are various use cases to export and import the database. Likely you want to:

  1. Export the database from your old webhosting
  2. Export your local database to import it to the fortrabbit database
  3. Export the remote database from fortrabbit to bring your local installation up-to-date

Read on in the MySQL article on how to do that and other tasks.

Working with artisan migrate

You can either login to SSH and execute artisan on the App. Or you can utilize execute remote commands via SSH locally, for example:

# remote execution
$ ssh {{ssh-user}}@deploy.{{region}}.frbit.com 'php artisan migrate --force'

# login and execute
$ ssh {{ssh-user}}@deploy.{{region}}.frbit.com
$ php artisan migrate --force

Note: 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.

Logging

You can access your logs via SSH or SFTP. Laravel, per default, stores it's log files in storage/logs. You can download them via SFTP from that folder. If you need to "tail" your logs live, you can:

# login via SSH
$ ssh {{ssh-user}}@deploy.{{region}}.frbit.com

# tail the logs (they contain the current date, per default)
$ tail -f storage/logs/laravel-$(date '+%Y-%m-%d').log

Queueing

The Universal Stack does not support long running processes like php artisan queue:work. Please check out the Laravel 6 Professional article on how to integrate those there.

Using Laravel 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

Using Laravel Mix

Laravel Mix compiles JS and CSS to really small and handy files using webpack, also see the Laravel docs on this. You can not on fortrabbit as there is no Node.JS on remote running. So you need to run the built process for production locally first.

# Install node modules (locally)
npm install

# Build assets using Laravel Mix (locally)
npm run prod

For deploying the assets we recommend using rsync. The one-liner below works with the most common scenarios, adjust it to your needs if your setup differs.

# Rsync command to sync the asset in your /public folder 
rsync -av --include='*.js' --include='*.css' --include='mix-manifest.json' --exclude='*'  ./public/ {{app-name}}@deploy.{{region}}.frbit.com:./public/

For your convenience you can define rsync command npm run deploy-assets. Example of package.json:

{
  "scripts": {
    "deploy-assets": "rsync -av ...",
   }
}

Another option might be to combine fortrabbit with GitHub Actions so you can have builds running over at GitHub and deploy everything along with artefacts afterwards. See our blog post.

Working with Redis

Redis can be used in Laravel as a cache or a queue or both. fortrabbit does not offer Redis services on it's own. To use Redis, you will need to book an external hosted Redis service. We have an article for redislabs.

  1. On the fortrabbit side, turn on the Redis extension in the Dashboard under the Apps settings.
  2. Then setup an Account with a Redis provider.
  3. 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(getenv('APP_SECRETS'))) {
    $secrets = json_decode(file_get_contents(getenv('APP_SECRETS')), true);
    $redis = [
        'host'     => $secrets['CUSTOM']['REDIS_HOST'],
        'port'     => $secrets['CUSTOM']['REDIS_PORT'],
        'password' => $secrets['CUSTOM']['REDIS_PASSWORD']
        'persistent' => 1
    ];
}

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 the CACHE_DRIVER environment variable to redis in the Dashboard).

Scheduling

The Laravel scheduler is not supported with the Universal Stack by design. The minimum time frame for standard crons is 10 minutes here, but the Laravel scheduler requires a 1 minute scheduling. Use the Pro Stack in combination with the Workers Component. That way your crons will be outsourced into background processes.

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.

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 ›