Quick Tip: Deploy PHP to Heroku in Seconds

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:

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:

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

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

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:

Comments

Related Articles