/* php and mySQL in heroku */

I have had several requests from readers on how to upload your site (php and mysql) to heroku. I leave a step by step guide to do so.

Create app and upload files

The first step is to initialize git inside the folder of your PHP website:

$ git init

then create an heroku php app and rename it:

$ heroku create --stack cedar
$ heroku rename your_custom_app_name

now we need a database. what take us to the next part.

Creating your database

To create our database we need to add an Add-On to the heroku account called ClearDB. We can do so with the terminal:

$ heroku addons:add cleardb:ignite

This would add a database (free) to your heroku account.

Migrating an existing database

If you have a local database that you wish to migrate to heroku, you can do it with a tool called MySQL Workbench.

We need to obtain the access for the ClearDB database:

$ heroku config

This will return an URL with this structure:

mysql://[user]:[pass]@[server]/[database_name]?reconnect=true

You could use MySQL Workbench to connect to the ClearDB server and upload the data from your local database.

Config PHP DB connection

In order to connect your app to the ClearDB database in heroku you need to set the correct variables in your php dbconnection file from the URL that you obtain in the past step. Like this:

<?php
    $url=parse_url(getenv("CLEARDB_DATABASE_URL"));

    $server = $url["host"];
    $username = $url["user"];
    $password = $url["pass"];
    $db = substr($url["path"],1);

    mysql_connect($server, $username, $password);


    mysql_select_db($db);
?>

Upload everything

Now it's time to upload your files and check if everything went ok with this commands:

$ git add .
$ git commit -m "upload everything"
$ git push heroku master

Finish

And that's all. Now check if everything it's ok in the server. thanks for reading and any comment is wellcome under this text.

Posted by @gamanox

Please help me with a click in the ad!

comments powered by Disqus