Heroku php laravel postgres

We have done several projects using LAMP stack.  Most of the time, we end up using a framework like codeigniter or yii to ease development. The deployment typically happens in a shared hosting environment like godaddy or bigrock by manually copying over files.

In a recent project, we had an opportunity to use heroku for production deployment.  The first question was if heroku supported php.  It is more popular as a platform to deploy ruby applications.  But of late, they have been increasing php support, including facebook's hiphop runtime.

The second question was if we should use a framework and if so, what?  The requirement was only for a set of web services to feed an iOS app.  There were no web UI requirements.  Well! to put it differently - we were constrained to develop a web site using squarespace template. Relevant data for the site needed to be pulled using web services.

yii seemed an overkill, but developing web services by hand would not be trivial either.  A search for simple php frameworks revealed laravel.  This  framework is being actively developed and well documented.  Downloading and getting started by following the quickstart was a breeze.

heroku and laravel work well.  laravel uses composer to set itself up.  heroku uses composer to deploy the app. 

heroku brought with it the constraint of postgresql.  Mysql is not natively supported by heroku and using recommended workarounds is a pain.  So, it had to be postgresql.

Being used to the various mysql commands, it took some time learning and using the postgresql equivalent.  For instance,

MysqlPostgresql
show databases\list
use mydb\c mydb
show tables\dt
describe mytable\d+ mytable

SQL commands are the same though, so once past the above, it was not very different.

Deploying to heroku is a different proposition though.  There is no ftp or scp account to copy over files.  heroku works off a git repository.  Every heroku account comes with a git repo.

Now we already had a git repo for the project.  So, we had to map the heroku git repo to ours.  This is easily achieved by adding a new remote.

git remote add heroku git@heroku.com:{heroku-app-name}.git

Subsequently any code change could be deployed by a simple

git push heroku master

Configuring laravel to talk to postgresql of heroku is well-documented.

Use the heroku environment variable DATABASE_URL

Then make changes in app/config/database.php

$url = parse_url(getenv("DATABASE_URL"));
$host = $url["host"];
$username = $url["user"];
$password = $url["pass"];
$database = substr($url["path"], 1);

...

'pgsql' => array(
    'driver' => 'pgsql',
    'host' => $host,
    'database' => $database,
    'username' => $username,
    'password' => $password,
    'charset' => 'utf8',
    'prefix' => '',
    'schema' => 'public',
),


A couple of things to note:

Heroku always uses the latest release version of php.    This is quite contrary to shared hosting environments where typically we find only older php versions.

Enabling extensions:  we needed to use GD in our app and thus the corresponding extension needed to be enabled.  It is easy to do this using composer.

 "require": {
        ...
        "ext-gd":"*",
        "ext-mbstring":"*",
        "ext-exif":"*",
        ...
    },


The best part of heroku is it is free for development (and even for simple deployments).  The combination of php-laravel-postgresql-heroku is an awesome one!

I found this Series of posts on laravel and heroku quite useful.

Comments

Popular posts from this blog

Opening a safe deposit locker in SBI

Opening a Kannada Word document

Switching to Tatasky make my pack