We are going to deploy an app making it publicly accessible on the web with a fantastic service named Heroku. Heroku makes it easy to deploy web apps currently located on your own computer to the web, with just a few simple commands. Before you can deploy apps using Heroku you need two things;

  • A Heroku account and
  • Git installed on your machine After creating an account and logging in you’ll see your Heroku dashboard. You will see immediately after login, a notification prompting you to download the Heroku tool belt for your system OS.

Getting Started

With our Heroku account created and Heroku tool belt installed, we can start in on deploying our app.

Step 1: Create app folder in specified path in CLI

I prefer to create my PHP projects in my xampp/htdocs directory. So open your terminal and navigate to your preferred path to store this project.

Step 2: The next thing we need to do is login to heroku via the terminal

This we do by issuing the Heroku login command as shown below

You’ll be asked for your login credentials (email and password) to proceed. After doing this you are set to begin.

Step 3: Create a test PHP file

The first thing we are going to do is to create a test PHP file which we eventually deploy on heroku. Open your text editor and create an index.php file

Step 4: Create composer.json file

We also need to create a composer.json file as every PHP application deployed by Heroku is required to have one. So inside your folder create a composer.json file.

Step 5: Establish git repository

The final requirement before we can deploy is to establish a git repository in our directory. Heroku requires a git repository to work its magic during the deployment process. And we are all set!!!

Step 6: Create Heroku project

We will start by creating our new Heroku application using the “heroku create” command By running the above command our Heroku application was generated and assigned a new auto generated application name. After we deploy our application we will be able to access it using the URL you see here in the terminal [https://….] in our web browser.

Step 7: Deploy App

To deploy the application we simply run a “git push” command, pushing the files up to Heroku. Heroku automatically detects that we’re deploying a PHP application and sets up the environment appropriately. You can copy your apps URL and take a look at it in the browser. Walla!!! That’s how easy it is to deploy an app publicly using Heroku

For the sake of demonstration I’ve prepared another simple PHP web app to deploy using Heroku. You can use any available PHP/MySQL app you have or get the one for this tutorial from GitHub. Once you have downloaded the source or if you have an app you want to use be sure to navigate back into the specified directory on the terminal and continue the steps below. Note: if you are in a new folder don’t forget to “git init” & also “git add .” before you proceed to the other steps below.

Step 8: Commit the files using “git commit” command

Step 9: Push up our files to deploy the app

Using the git push heroku master command

Step 10: Set up database configuration

Since our app is connected to a local database on our machine, Heroku has no access to it. We can fix this by creating a new MySQL database within Heroku. Heroku offers a “ClearDB” addon which we can add to our application using the Heroku addon ads command in other to attach a MySQL database to it. We do this using the command heroku addons:add cleardb:ignite as shown below

Step 11: Import local database tables into Heroku database

For those that downloaded the source you will see a SQL file in the root folder, if you already have a database you want to use, export it from localhost/phymyadmin to get going. Let’s connect to our Heroku database and import it using the command heroku config | grep CLEARDB_DATABASE_URL as shown below in the terminal Now we can import the database export file I previously created (or the one you have) to our CLEARDB database using the command below. Notice that the sql file is stored in my apps root directory.

mysql -u b5bf52783a17e5 -h us-cdbr-iron-east-05.cleardb.net -p heroku_f677d75e
73d39a7 < facebook.sql

NB: The above command will be slightly different from yours, so check with caution. Quick Note: When prompted for password, use the one as obtained in here.

Step 12: Change app’s database connection config file

ClearDB addon makes it super easy to grab all of the relevant connection information. In our database file you should nave something like below

//Get Heroku ClearDB connection information
$cleardb_url      = parse_url(getenv("CLEARDB_DATABASE_URL"));
$cleardb_server   = $cleardb_url["host"];
$cleardb_username = $cleardb_url["user"];
$cleardb_password = $cleardb_url["pass"];
$cleardb_db       = substr($cleardb_url["path"],1);


$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'    => '',
    'hostname' => $cleardb_server,
    'username' => $cleardb_username,
    'password' => $cleardb_password,
    'database' => $cleardb_db,
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

Step 13: Update our composer.json file

Next, we update the composer.json file to include the MySQL extension. My database driver is mysqli so I basically need the mysqli extension as seen below

{
  "require": {
    "ext-mysqli": "*"
  }
}

Then run composer update to load the necessary dependencies We are good to go!!

Step 14: Next we need to add commit as before

git add.

Then

git commit -am "new heroku cleardb configuration"

Finally

git push heroku master

All set, now let’s go to back to our browser and make sure our new database connection is working properly. See mine on https://immense-brushlands-25104.herokuapp.com/social_auth

Conclusion

The running app is just an OAUTH app using codeigniter 3, you should probably test with only the google and facebook sign in buttons. This same exact process we just followed will work for hosting any PHP application using Heroku.

Thanks for your time, and I hope this was helpful.

Like this article? Follow @LK_wapiti on Twitter