Getting going with Heroku and PHP (part 1)

As a preface, I’ve tinkered a little bit with Heroku and Ruby, so I already had the Heroku client installed, but it’s not something that I’d worked with properly.  However, I had a little project I wanted to work on, and it seemed like a good idea to have a play, so here’s how it went.

I started by reading the Getting Started on Heroku with PHP tutorial on their website, and you should definitely start here to.  I did skip over a couple of bits, and to be fair, some of that bit me later.  However, I also looked at the related Github repo (as suggested in the guide) and this gave me a useful example.

However, I was trying to apply this to an existing project, so I skipped bits and tried to just add in what I required.  I created my Heroku account, and then promptly skipped installing PHP or Composer.  I just FTP everything up as part of my Gulp task usually, so I don’t bother running PHP locally, and I write fairly simple PHP and haven’t needed to think about dependencies, so Composer has always seemed like a sledgehammer for my nut cracking.

Next step, install Git – tick, I’ve got Git already installed.  And install Heroku client app, also a tick.  Although it was out of date and not logged in, so I ran the following commands…

heroku update
heroku login

I have two-factor authentication switched on, and I highly recommend you do too, so I am asked for username, password and code to log in.

I did not clone the app source.  As I say, I’m trying to fit this into an existing project – skipped.  I did have a look at the repo though, as I mentioned above.

I actually created my Heroku app in the dashboard in the web browser, rather than doing it through the client.  From there I could connect to GitHub in order to do automatic deployments – very nice!

However, you may work like me, with a development process with Git and Gulp.  For me, this means a folder structure something like this…

  • build
    • css\
    • img\
    • js\
    • maps\
    • index.php
    • .htaccess
  • css\
  • img\
  • js\
  • index.php
  • .htaccess

So all my working source files are in the root and subfolders, but Gulp outputs all the compiled/minified/etc. production files into the “build” folder.  Well this is a problem for Heroku, as it assumes all your files are in root.

After some quick Googling around, I found “git subtree”.  This can be a little complicated, but to over-simplify them rather, it allows you to push a subfolder into a new repo.  This means I can then point Heroku at this repo instead of my “master”, problem solved.

You need to commit the files before you can push them into a subtree, so my set of commands following a change went something like this…

gulp
git add .
git commit -m "A useful commit message"
git push
git subtree push --prefix build heroku master

As you can see, this is a little wordy, but the result of that final command is that my app (just the final output in the “build” folder) has been deployed to Heroku.

Unfortunately, Heroku isn’t particularly happy with me.  It has worked, but it’s been forced to make some assumptions.  Because there’s an “index.php” file, it’s worked out it must be a PHP environment that’s required, and therefore it also selects Apache by default too.  What it really wants though, is some of those steps I missed earlier sorting out, specifically the adding of a “Procfile” and a “composer.json” file.

I’ve got another blog post coming up shortly to tell you what I did next.

EDIT: Getting going with Heroku and PHP (part 2) has now been published.