lunes, 29 de octubre de 2012

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:

  <?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.



No hay comentarios:

Publicar un comentario