bosnadev

№ 2 · 2014-09-16 · 2 min · laravel

Deploy your Laravel 4 application on Heroku

Deploying a Laravel 4 application to Heroku with git, the Heroku Toolbelt and a one-line Procfile that serves the public/ folder with Apache.

written before the past year · entry 2 of 31 · 10 in the past year

This article is out of date
Written for Laravel 4 and the Heroku Toolbelt in 2014.

Heroku is a popular PaaS that allows you to deploy your application’s in no time. But, learning how to do that can be confusing, especially for PHP developers. In this tutorial I’ll cover basic steps which will help you to deploy your Laravel 4 application on Heroku. First let’s download Laravel:

terminal1 line
composer create-project laravel/laravel --prefer-dist laravel

After that, enter in laravel directory:

terminal1 line
cd laravel

and initialize new git repository:

terminal1 line
git init

Now, create .gitignore file and put everything you want to ignore from git tracking. You can use Laravel’s default .gitignore file, just remove composer.lock from it:

.gitignore8 lines
/bootstrap/compiled.php/vendorcomposer.phar.env.*.php.env.php.DS_StoreThumbs.db/.idea

And make a commit:

terminal2 lines
git add -Agit commit -m 'Initial commit'

I assume that you have installed the Heroku Toolbelt, if you don’t have Heroku Toolbelt installed then just look at the install instructions and then continue with this tutorial.

Now, you need to login:

terminal1 line
heroku login

you need to enter your credentials and we’re ready to go. Let’s create our app:

terminal1 line
heroku create shop-up --region eu --stack cedar --buildpack https://github.com/heroku/heroku-buildpack-php

heroku create command is used to create application called shop-up, with region parameter you can select region, stack parameter allows you to select on which stack you want to deploy your application and finally with buildpack parameter we can specify which buildpack we want to use for our application. In this case I used Heroku’s default PHP buildpack, and you can use whatever you want.

After executing this command we get following message:

Creating shop-up... done, region is euBUILDPACK_URL=https://github.com/heroku/heroku-buildpack-phphttp://shop-up.herokuapp.com/ | git@heroku.com:shop-up.gitGit remote heroku added

Now, you just need to create Heroku Procfile. You need to tell Apache2 web server that document root of your application is public/. Procfile should be placed in the root folder of your application. We just need to add one line to it:

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

Commmit changes and push you application to heroku repository:

terminal3 lines
git add Procfilegit commit -m 'added Procfile'git push heroku master

After minute or two, you should get a message that tells you that your application is now deployed to Heroku:

       Generating optimized autoload files       Generating optimized class loader-----> Preparing runtime environment...-----> Discovering process types       Procfile declares types -> web-----> Compressing... done, 70.2MB-----> Launching... done, v11       http://shop-up.herokuapp.com/ deployed to Heroku

And that’s it. Here is list of some useful commands that you may need:

terminal5 lines
heroku run php artisan <some-artisan-command>heroku run composer <some-composer-command>heroku apps # List of your Heroku applicationsheroku config --app <your-app> # List of your config/environment variables for specified applicationheroku addons --app <your-app> # List of add-ons you have enabled on your application

In the next tutorial I’ll show you how you can add and use PostgreSQL database on your Heroku instance. Stay tuned.

Related

A Brief Introduction to Laravel EnvoylaravelLaravel Filters: Route access depending on the user grouplaravelInstall Elasticsearch on Laravel Homesteadlaravel