1. Code
  2. PHP

Quick Tip: Deploy PHP to Heroku in Seconds

Scroll to top

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:

1
2
<?php
3
# This function reads your DATABASE_URL configuration automatically set by Heroku

4
# the return value is a string that will work with pg_connect

5
function pg_connection_string() {
6
  // we will fill this out next

7
}
8
9
# Establish db connection

10
$db = pg_connect(pg_connection_string());
11
if (!$db) {
12
	echo "Database connection error."
13
	exit;
14
}
15
16
$result = pg_query($db, "SELECT statement goes here");
17
?>

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:

1
2
	> git init
3
	> git add .
4
	> heroku create
5
	...

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

1
2
	> git push heroku master
3
	> heroku addons:add heroku-postgresql:dev # this will return something like the following

4
	Adding heroku-postgresql on intense-harbor-6679... done, v8 (free)
5
	Attached as HEROKU_POSTGRESQL_PINK
6
	Database has been created and is available
7
	> heroku pg:credentials COLOR
8
	"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).

1
2
<?php
3
# This function reads your DATABASE_URL configuration automatically set by Heroku

4
# the return value is a string that will work with pg_connect

5
function pg_connection_string() {
6
  return "dbname=abcdefg host=****.amazonaws.com port=5432 user=**** password=**** sslmode=require";
7
}
8
9
# Establish db connection

10
$db = pg_connect(pg_connection_string());
11
if (!$db) {
12
	echo "Database connection error."
13
	exit;
14
}
15
16
$result = pg_query($db, "SELECT statement goes here");
17
?>

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.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.