Advertisement
PHP

Quick Tip: Deploy PHP to Heroku in Seconds

by

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:dev # 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.

Related Posts
  • Code
    Web Development
    How to Use New Relic With PHP & WordPressRelic retina preview
    Today we will look at how to monitor a PHP application using New Relic. More specifically we will set up a basic WordPress installation and get some performance data about it, in the New Relic dashboards.Read More…
  • Code
    Web Development
    Easily Deploy Redis Backed Web Apps With DockerDocker wide retina preview
    Learn how to setup and deploy a Redis powered Python web app using Docker.Read More…
  • Code
    WordPress
    An Introduction To Deploying WordPress with MinaImage400
    As a PHP application, WordPress is usually deployed by a very old method: uploading files via FTP. We have some deployment tools, but they often requires some type of Ruby skill. For example, one popular, powerful tool is Capistrano, but it's also very heavy with many Ruby/Rails related features. I also think that it's little bit tricky to install Capistrano for a PHP developer without any Ruby knowledge. So what options do we have as WordPress developers? In this tutorial, I will introduce you Mina: A small, light tool aims to fast deployment and server automation.Read More…
  • Code
    Web Development
    Laravel Unwrapped: Session, Auth and CacheLaravel wide retina preview
    Join me as we learn how to use Laravel's component-based system, Illuminate. Additionally, we'll see how to utilize service providers, Laravel's manager system, the Session, Auth, and Cache components, and the Store, Guard, and Repository libraries.Read More…
  • Code
    PHP
    Setting Up a Local Mirror for Composer Packages With SatisComposer retina preview
    Installing all your PHP libraries with Composer is a great way to save time. But larger projects automatically tested and run at each commit to your software version control (SVC) system will take a long time to install all the required packages from the Internet. You want to run your tests as soon as possible through your continuous integration (CI) system so that you have fast feedback and quick reactions on failure. In this tutorial we will set up a local mirror to proxy all your packages required in your project's composer.json file. This will make our CI work much faster, install the packages over the local network or even hosted on the same machine, and make sure we have the specific versions of the packages always available.Read More…
  • Code
    PHP
    Deploying a Laravel Application Using CapistranoLaravel 4 auth retina preview
    So you've just built a fancy web application and you're planning to put it online. This can be done in many ways. In this article I'd like to cover one approach to deploy your backend system to your production server. We'll go through the following steps by example of a Laravel application but this can be applied to any other language or technology.Read More…