herokuでPHPをMac OS Xから使う

Mac OS Xからherokuを使えるように設定します。

必要なものは、以下の3つです。

  • Herokuのアカウント
  • PHP
  • Composer

アカウントにはSSH公開鍵も登録しておきます。

Heroku Toolbeltのインストール

「Download Heroku Toolbelt for Mac OS X」を押してHeroku Toolbeltをダウンロードしてインストールします。

ログイン

herokuコマンドでログインします。

$ heroku login

サンプルコードの取得

サンプルのコードをgitで取得します。

$ git clone https://github.com/heroku/php-getting-started.git

appの作成

herokuにappを作成します。

$ cd php-getting-started
$ heroku create
Creating immense-escarpment-4961... done, stack is cedar-14
https://immense-escarpment-4961.herokuapp.com/ | https://git.heroku.com/immense-escarpment-4961.git
Git remote heroku added

appのデプロイ

herokuにソースをpushします。

$ git push heroku master
Counting objects: 103, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (51/51), done.
Writing objects: 100% (103/103), 17.37 KiB | 0 bytes/s, done.
Total 103 (delta 39), reused 103 (delta 39)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> PHP app detected
remote: -----> No runtime requirements in composer.json, defaulting to PHP 5.6.3.
remote: -----> Installing system packages...
remote:        - PHP 5.6.3
remote:        - Apache 2.4.10
remote:        - Nginx 1.6.0
remote: -----> Installing PHP extensions...
remote:        - zend-opcache (automatic; bundled, using 'ext-zend-opcache.ini')
remote: -----> Installing dependencies...
remote:        Composer version 1.0-dev (37ec0bde9dd6826591308e7a1ad55cb5e38ef117) 2014-12-03 15:33:35
remote:        Loading composer repositories with package information
remote:        Installing dependencies from lock file
remote:          - Installing psr/log (1.0.0)
remote:            Downloading: 100%
remote:        
remote:          - Installing monolog/monolog (1.11.0)
remote:            Downloading: 100%
remote:        
remote:          - Installing symfony/routing (v2.5.5)
remote:            Downloading: 100%
remote:        
remote:          - Installing symfony/http-foundation (v2.5.5)
remote:            Downloading: 100%
remote:        
remote:          - Installing symfony/event-dispatcher (v2.5.5)
remote:            Downloading: 100%
remote:        
remote:          - Installing symfony/debug (v2.5.5)
remote:            Downloading: 100%
remote:        
remote:          - Installing symfony/http-kernel (v2.5.5)
remote:            Downloading: 100%
remote:        
remote:          - Installing pimple/pimple (v1.1.1)
remote:            Downloading: connection...    Failed to download pimple/pimple from dist: The "https://api.github.com/repos/fabpot/Pimple/zipball/2019c145fe393923f3441b23f29bbdfaa5c58c4d" file could not be downloaded (HTTP/1.1 404 Not Found)
remote:            Now trying to download from source
remote:          - Installing pimple/pimple (v1.1.1)
remote:            Cloning 2019c145fe393923f3441b23f29bbdfaa5c58c4d
remote:        
remote:          - Installing silex/silex (v1.2.2)
remote:            Downloading: 100%
remote:        
remote:        Generating optimized autoload files
remote: -----> Preparing runtime environment...
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: 
remote: -----> Compressing... done, 72.2MB
remote: -----> Launching... done, v3
remote:        https://immense-escarpment-4961.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
To https://git.heroku.com/immense-escarpment-4961.git
 * [new branch]      master -> master

webプロセスが1つあることを確かめます。

$ heroku ps:scale web=1

appへのアクセス

ブラウザで作成したappのページを開きます。

$ heroku open

ログを見る

$ heroku logs --tail
2014-12-04T13:05:13.653066+00:00 heroku[router]: at=info method=GET path="/" host=immense-escarpment-4961.herokuapp.com request_id=0162ab92-cd24-4cb5-aae0-3000f9e4e668 fwd="211.1.214.70" dyno=web.1 connect=0ms service=9ms status=200 bytes=238
2014-12-04T13:05:13.648651+00:00 app[web.1]: [2014-12-04 13:05:13] myapp.INFO: Matched route "GET_" (parameters: "_controller": "{}", "_route": "GET_") [] []
2014-12-04T13:05:13.651095+00:00 app[web.1]: [2014-12-04 13:05:13] myapp.DEBUG: logging output. [] []
2014-12-04T13:05:13.653104+00:00 app[web.1]: 10.68.82.78 - - [04/Dec/2014:13:05:13 +0000] "GET / HTTP/1.1" 200 5 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:33.0) Gecko/20100101 Firefox/33.0"
2014-12-04T13:05:13.649443+00:00 app[web.1]: [2014-12-04 13:05:13] myapp.INFO: > GET / [] []
2014-12-04T13:05:13.652025+00:00 app[web.1]: [2014-12-04 13:05:13] myapp.INFO: < 200 [] []

[control]+[C]で表示を終了します。

Date: 2014/12/04

Tags: heroku, php, mac