Creating Yii2 App in Cloud9 → Pushing to Github → Deploying to Heroku

After getting help from Herokai, David Zuelke, I decided to create a how-to on the subject of "Creating Yii2 App in Cloud9 → Pushing to Github → Deploying to Heroku". Please see below:

  1. Create Yii2 “basic” app:

290795 $ composer create-project --prefer-dist yiisoft/yii2-app-basic basic

  1. Move into “basic” directory

290795 $ cd basic/

  1. Initialize local repo for “basic” directory

basic $ git init

  1. Add and commit Yii project

basic $ git add .

basic $ git commit -m "basic Yii project"

  1. Add procfile that will point to index.php (entry script) in “web” directory

basic $ echo "web: vendor/bin/heroku-php-apache2 web/" > Procfile

basic $ git add Procfile

basic $ git commit -m "Procfile for Heroku"

  1. Comment out debugging stuff (I do this manually instead of from command line)

basic $ vim web/index.php # remove dev/debug env stuff (not my way)

basic $ git add web/index.php

basic $ git commit -m "remove dev/debug env"

  1. This updates the dependency from v1.0.3 to v1.1 (absolute necessity)

basic $ composer require fxp/composer-asset-plugin

basic $ git add composer.json composer.lock

basic $ git commit -m "use fxp/composer-asset-plugin in project"

8) Create Heroku app and push to it. (I instead create and push to Github repo)

basic $ heroku create

basic $ git push heroku master (again I push to Github Repo synced with Heroku)

  1. Add logging to Heroku for Yii app (no logging for Yii out of the box)

basic $ echo "web: vendor/bin/heroku-php-apache2 -l runtime/logs/app.log web/" > Procfile

basic $ git add Procfile

basic $ git commit -m "tail runtime/logs/app.log"

basic $ git push heroku master (again I push to Github Repo synced with Heroku)

  1. This allows the “Contact” page to work (not sure why)

basic $ composer require ext-gd:* --ignore-platform-reqs

basic $ git add composer.json composer.lock

basic $ git commit -m "require GD for contact CAPTCHA"

basic $ git push heroku master (again I push to Github Repo synced with Heroku)

Thank you for this topic, I’ll be sure to remember it and try it out when I have the time.

As for point 10), I think I can shed some light on the matter. When I last developed an app with Yii, there was an error on the Contact page, telling me that Yii needs “either imagemagick or [something else]” for the Captcha-generation on the contact page. I solved it by installing imagemagick and php5-imagick, but perhaps this “ext-gd” is this [something else] that I can’t remember now.

Cheers

Christoph

Hi everyone…

I followed this post to deploy my app on Heroku, but after push to heroku without error on CLI my app shows the message "An internal server error occurred."

I don’t know why its appear.

This could be a few reasons. Are you sure your Heroku app is the required min PHP version for Yii2 to run?

I think the most common issue is that it wasn’t able to find/set the document root to your ‘/web’ directory.

If your using enable pretty urls, have you created the htaccess file and is it correct?


I can confirm that you need to manually add the GD library to Heroku for Captcha to work. Imagick and GD are both image rendering libraries used to create the Captcha. It shouldn’t need both, but I know there are other guides out there about adding GD to Heroku so it seems to be the more popular choice. I personally don’t use the built-in captcha and I use the Google v2 recaptcha widget. Any modern bot can easily bypass the captcha generator that comes with Yii, so it is really worthless to use.

I use OpenShift, and I have it create symlinks in my deploy hook. When I push to OpenShift, it runs composer update, migrate, creates symlinks, all kinds of things with I push.