Quick Tip: Deploy PHP to Heroku in Seconds

Quick Tip: Deploy PHP to Heroku in Seconds

Tutorial Details
  • Difficulty: Beginner
  • Completion Time: 5 Minutes

We’ve raved about the brilliance of Heroku before, mostly around the fact that it makes launching a Rails or Node app rather simple without having to configure your own server. But what if you want the same kind of freedom and speed of deployment with PHP? Fortunately, Heroku has quietly offered support for PHP for quite some time.


Make Sure You Have the Heroku Toolbelt

For Heroku deployment, you need the provided command line toolbelt. Follow the instructions on the same page; they’ll walk you through setting up the Heroku command line toolbelt with your Heroku account.


Ready, Set, Deploy

First, create an index.php file within your application’s directory, and type the following code:

<?php
# This function reads your DATABASE_URL configuration automatically set by Heroku
# the return value is a string that will work with pg_connect
function pg_connection_string() {
  // we will fill this out next
}

# Establish db connection
$db = pg_connect(pg_connection_string());
if (!$db) {
	echo "Database connection error."
	exit;
}

$result = pg_query($db, "SELECT statement goes here");
?>

This code uses pg_connect to connect to your automatically created Heroku Postgres database. We don’t have the connection information yet; we’ll have to wait until after we create our Heroku repository. Let’s do that now. From your project directory, run the following commands:

	> git init
	> git add .
	> heroku create
	...

This automatically creates your project and adds the repository as the “heroku” branch. Now run the following commands to deploy the project:

	> git push heroku master
	> heroku addons:add heroku-postgresql:ronin # this will return something like the following
	Adding heroku-postgresql on intense-harbor-6679... done, v8 (free)
	Attached as HEROKU_POSTGRESQL_PINK
	Database has been created and is available
	> heroku pg:credentials COLOR
	"dbname=abcdefg host=****.amazonaws.com port=5432 user=**** password=**** sslmode=require"

This final command should return a credentials string that you can use in your index.php file (or anywhere you need a database connection).

<?php
# This function reads your DATABASE_URL configuration automatically set by Heroku
# the return value is a string that will work with pg_connect
function pg_connection_string() {
  return "dbname=abcdefg host=****.amazonaws.com port=5432 user=**** password=**** sslmode=require";
}

# Establish db connection
$db = pg_connect(pg_connection_string());
if (!$db) {
	echo "Database connection error."
	exit;
}

$result = pg_query($db, "SELECT statement goes here");
?>

To view your index.php on Heroku, run heroku open, which simply opens the project in your browser.


Conclusion

That’s it! There’s plenty more that you can learn about Heroku, but this will get you deployed and connected to a database in less than 5 minutes.

Tags: heroku
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.codeconquest.com/ Charles @ CodeConquest.com

    Hi Jonathan. Thanks for the quick tip – I didn’t know Heroku supported PHP.

  • aldoutrera

    I’m surprised about this aswell. I mean, is not on the homepage, is not even listed on the languages list on the dev center.

    Thanks for sharing.

  • http://www.facebook.com/evantbyrne Evan Byrne

    Heroku is overpriced even for harder to deploy programs like Django. Using Heroku to host a PHP app is just a waste of money.

    • http://www.butenas.com/ Ignas B.

      depends on the size of app, but in most cases must agree it is a waste.

  • sarabjeet singh

    Really like that you are providing such information on PHP MYSQl with JAVASCRIPT ,being enrolled at http://www.wiziq.com/course/5871-php-mysql-with-basic-javascript-integrated-course i really thank you for providing such information it was helpful.

  • Burningfuses

    You need to commit before pushing to Heroku.
    Your code should be:

    > git init
    > git add .
    > (needed) git commit -m “commit msg”
    > heroku create

    • ancanta

      thx!!

  • http://www.wordpressguru.com.au/ Wordpress Developer Sydney

    Hi Jonathan,
    Is there any article about Heroku, more like an intro and what it does somewhere in the nettus site.
    Please recommend.

  • Brian Kessler

    You arsehole!

    Why did you include a ***RONIN*** database in your tutorial?!?!

    You almost cost me $200 per month!!!

    Fortunately, someone at Heruko was paying attention and asked why I installed a Ronin database.

    As many who are readying this won’t be ready for production yet, the ***DEV*** database would be a more appropriate choice to include within your tutorial.

    Information on database pricing here:

    https://www.heroku.com/pricing/#0-0