Deploying a PHP and MySQL Web App with Heroku

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.

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.

Wikipedia, pyttsx and speech recognition API’s [part 2] This is a continuation of our series on building a virtual assistant in python 3.x. From the previous article we highlighted all the core features of the application, like how to use both the ...