Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

github
php
heroku
communitycreator
application

How to deploy a GitHub PHP app to Heroku

Abel Lifaefi Mbula

You’ve been building your app and hosting your source code on GitHub. Now you want to ship it to the world so that people can use it. You have plenty of solutions to do so.

In this shot, we will learn how to deploy an app to Heroku, which is a popular platform as a service (PaaS).

Prerequisites

You will need the following:

  • An account on Heroku.
  • You have a PHP app on GitHub. If not, you can clone this to follow along.

Getting started with Heroku

Here are the steps you need to follow to work with Heroku:

  • Create a new application.
  • Choose a deployment method.
  • Extend features with the help of add-ons such as data storage, monitoring, etcetera.

Let’s get started.

Create a new application

To start, click the “New” button and then click “Create new app” to create a new application. You’ll be given a form. Fill it and click the “Create app” button once finished.

Create a new app on Heroku
Create a new app on Heroku

Choose a deployment method

Heroku offers developers many deployment methods. In this course, we’re going to use the GitHub method because it makes the development and maintenance process smooth. You can focus on new features using the git workflow.

Click “GitHub” in the deployment method. Next, type the name of your project repository. Ours is called is crud-php-mysql. Click the “Connect” button.

Connect with GitHub
Connect with GitHub

Note: If you have more than one branch, you must specify the main deploy branch.

Your repository is now connected to Heroku.

In the “Automatic deploys” section, click “Enable Automatic deploys”. Once you make a change in your code and push it to GitHub, Heroku will automatically deploy it.

Automatic deployment
Automatic deployment

Let’s manually deploy a branch by clicking “Deploy branch” under the “Manual deploy” section.

Manual deployment
Manual deployment

Click the “View” button to see our application.

If you try to click other links, you’ll get errors because they require a database connection. Let’s see how to fix these issues.

Add the MySQL engine to our application

Heroku does not have a database system already set by default. Because our application depends on a database, we have to install a Heroku database “addon”.

From the dashboard of our newly created app, click “Resources”. In the search bar, type “MySQL” and select “ClearDBMySQL”.

Heroku ClearDB MySQL
Heroku ClearDB MySQL

Make sure that the free version, Ignite - Free, is selected, and click the “Provision” button.

Heroku Provision
Heroku Provision

Once done with the provision, you can go to the setting of your app to get the database information. Click the “Reveal Config” button, and you’ll be given the CLEARDB_DATABASE_URL which looks like this:

widget

Test remote data from CLI

Let’s try to connect to the database from the CLI.

Test Heroku database from CLI
Test Heroku database from CLI

As you can see in the image above, we’ve successfully connected, and we can even see the database.

Populate the remote database

In model/config.php, let’s add information about the remote database.

$host       = "us-cdbr-east-02.cleardb.com";
$username   = "your remote db username";
$password   = "your secrete";
$dbname     = "your db name";

if ($_SERVER['HTTP_HOST'] == 'localhost') {
  $host       = "localhost";
  $username   = "root";
  $password   = "";
  $dbname     = "tracker";
}

$dsn        = "mysql:host=$host;dbname=$dbname";
$options    = array(
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);

Make sure you use the information concerning your remote database. Notice that we’ve used an if statement so that we’re able to use the same file for both the localhost and remote database configuration.

In data/install.php, add this before you start executing queries:

if ($_SERVER['HTTP_HOST'] != 'localhost') {
        $sql_db = 'USE ' . $dbname . ';';
    }

For the remote database, we don’t need to create, but we can use the one the provided to us.

Save the new changes, commit, and push to your remote GitHub repository.

We can now run our install script.

Go to the browser and use this link to run the install script. You may need to change the base URL to match yours.

If you don’t have an error in your code, you’ll see a successful message, just as we did for localhost.

Your site is now live with the database set.

RELATED TAGS

github
php
heroku
communitycreator
application
RELATED COURSES

View all Courses

Keep Exploring