Skip to content

Instantly share code, notes, and snippets.

@calimaborges
Last active July 3, 2019 12:42
Show Gist options
  • Save calimaborges/5476c68369fafd259f2d to your computer and use it in GitHub Desktop.
Save calimaborges/5476c68369fafd259f2d to your computer and use it in GitHub Desktop.
Laravel + Heroku

Laravel

Useful doc


Create a new project

Create Laravel project

laravel new hello-heroku

Enter project folder

cd hello-heroku

Login to Heroku

heroku login

Create Heroku project

heroku create

Configure Heroku information

Create Procfile

web: vendor/bin/heroku-php-apache2 public/

Start web dyno

Check if there is a dyno already

heroku ps

If not then start the web dyno

heroku ps:scale web=1

Remove package.json and gulpfile.js

rm package.json gulpfile.js

I don't like to mixup langages. But if you do, you should set buildpack for Heroku to use PHP or else it will use node.js because of the package.json file. To set the buildpack use:

heroku buildpacks:set https://github.com/heroku/heroku-buildpack-php

Configure project environment variables

Configure local environment variable

cp .env.example .env

Configure Heroku environment variable

heroku config:set APP_KEY=RandomStringKey! (Use php artisan key:generate to get one)`

Configure project Log

Heroku needs log to be sent to stderr. To do this set the env variable.

heroku config:set APP_LOG=errorlog

Configure session file to use memcached

https://devcenter.heroku.com/articles/php-sessions

Configure database on Heroku

https://mattstauffer.co/blog/laravel-on-heroku-using-a-postgresql-database

Add postgres addon

heroku addons:create heroku-postgresql:hobby-dev

Change Laravel's config file

# file config/database.php
<?php

$heroku_db_url = parse_url(env('DATABASE_URL', "postgres://forge:forge@localhost:5432/forge"));
.
.
.
'connections' => [

        'sqlite' => [
            'driver'   => 'sqlite',
            'database' => storage_path('database.sqlite'),
            'prefix'   => '',
        ],
        
        'sqlite_testing' => [
            'driver'   => 'sqlite',
            'database' => ':memory:',
            'prefix'   => '',
        ],

        'pg-heroku' => [
            'driver'   => 'pgsql',
            'host'     => $heroku_db_url['host'],
            'database' => substr($heroku_db_url['path'], 1),
            'username' => $heroku_db_url['user'],
            'password' => $heroku_db_url['pass'],
            'charset'  => 'utf8',
            'prefix'   => '',
            'schema'   => 'public',
        ],
.
.
.

Setup Heroku to use the right database

heroku config:set DB_CONNECTION=pg-heroku

Execute your migrations

heroku run php artisan migrate
@erlangparasu
Copy link

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment