2012 coscup - build your php application on heroku

Post on 07-May-2015

7.601 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Build your PHP applicationon Heroku

Ronny Wang @ PIXNET

2

PaaS

Platform-as-a-Service

3

Deploy… And Run!

4

Install…Config…

Linux? FreeBSD? Debian? Ubuntu?RPM? Ports? Package? Apt? yum?

Apache? Nginx?PHP-cgi? FastCGI? PHP-Fpm?

5

Heroku

her-OH-koo

6

2007 ~

7

Amazon Web Service US-east-1

~200ms latency

8

Lots of Addons

9

Easy scale

10

PostgreSQL

11

Free!!!

On a small scale…

12

What’s Stack?

Aspen, Bamboo, Cedar

13

Stack Cedar

Clojure Facebook Java Spring or Play Node.js

Python or Django Ruby or Rails Scala

14

No PHP?

15

Facebook and Heroku

http://blog.heroku.com/archives/2011/9/15/facebook/

16

What’s Dyno?

17

Web, worker, cron, run process

All are dynos

18

0.05US$/hour/dyno

= 1000NT$/month/dyno

19

750hours free!

20

Heroku toolbelt

https://toolbelt.heroku.comHeroku Client, Foreman, Git

21

First: heroku login

22

heroku create

# heroku createCreating evening-earth-7959... done, stack is cedarhttp://evening-earth-7959.herokuapp.com/ | git@heroku.com:evening-earth-7959.gitGit remote heroku added

23

Add index.php

<?phpecho 'Hello World';

24

git commit index.php –m ‘add Hello World’

25

git pushCounting objects: 3, done.Writing objects: 100% (3/3), 250bytes, done.Total 3 (delta 0), reused 0(delta 0)-----> Heroku receiving push-----> PHP app detected-----> Bundling Apache 2.2.12-----> Bundling PHP 5.3.10-----> Discovering process typesProcfile declares types -> (none)Default types for PHP -> web-----> Compiled slug size in 21.5MB-----> Launching... done, v3http://evening-earth-7959.herokuapp.com deployed to Heroku

26

27

heroku logs

2012-08-16T10:59:15+00:00 app[web.1]: [Thu Aug 16 10:59:15 2012] [notice] Apache/2.2.22 (Unix) PHP/5.3.10 configured -- resuming normal operations2012-08-16T10:59:16+00:00 app[web.1]: [Thu Aug 16 10:59:16 2012] [error] server reached MaxClients setting, consider raising the MaxClients setting2012-08-16T11:03:16+00:00 app[web.1]: 10.189.119.194 - - [16/Aug/2012:11:03:15 +0000] "GET / HTTP/1.1" 200 142012-08-16T11:03:16+00:00 app[web.1]: 10.217.59.175 - - [16/Aug/2012:11:03:15 +0000] "GET /favicon.ico HTTP/1.1" 200 1025

28

Database: PostgreSQL

https://postgres.heroku.com/

29

Starter databases

DevPlan Free 10K rowsBasicPlan $9/month 10M rows

30

Production databases

Size: up to 1TBCrane 400MB Cache $50/month

Kappa 800MB Cache $100/month : :

Baku 34GB Cache $3200/monthMecha 68GB Cache $6400/month

31

heroku config

> heroku configDATABASE_URL: postgres://foofoofoo:barbarbar@ec2-123-123-123-123.compute-1.amazonaws.com/foofoofooSHARED_DATABASE_URL: postgres://foofoofoo:barbarbar@ec2-123-123-123-123.compute-1.amazonaws.com/foofoofoo

32

if (!getenv('DATABASE_URL')) { die('Need DATABASE_URL');}if (!preg_match(‘#postgres://([^:]*):([^@]*)@([^/:]*)(:\d+)?/(.*)#’, strval(getenv('DATABASE_URL')), $matches)) { die('Unknown DATABASE_URL');}$user = $matches[1];$pass = $matches[2];$host = $matches[3];$port = ltrim($matches[4], ':') ?: 1486;$dbname = $matches[5];$dbconn = pg_connect("host={$host} port={$port} dbname={$dbname} user={$user} password=${pass} sslmode=require options='--client_encoding=UTF8'") or die('Could not connect: ' . pg_last_error());

pg_execute($dbconn, "SELECT * FROM table");

#https://github.com/pixnet/pixframework-heroku/blob/master/init.inc.php

33

heroku run

Start a dyno and run command

34

heroku config:set LD_LIBRARY_PATH=/app/php/ext:/app/apache/lib

35

heroku run "~/bin/php ~/www/script.php"

36

Heroku Scheduler

Schedule your task

37

Daily, hourly, 10 minutes

38

39

$ ~/bin/php ~/www/cron.php

40

Heroku worker

41

File: Procfile

worker: ~/bin/php ~/www/worker.php

42

heroku ps:scale worker=N

43

heroku ps

# heroku ps=== web: `sh boot.sh`web.1: starting for 4s

=== worker: `~/bin/php ~/www/test.php`worker.1: up for 25s#

44

heroku logs –p worker -t

# heroku logs –p worker –t2012-08-14T08:21:29+00:00 heroku[worker.1]: State changed from up to down2012-08-14T08:21:31+00:00 heroku[worker.1]: Stopping all processes with SIGTERM2012-08-14T08:21:33+00:00 heroku[worker.1]: Process exited with status 1432012-08-16T10:58:48+00:00 heroku[worker.1]: Starting process with command `while true; do ~/bin/php ~/www/test.php; sleep 1; done`2012-08-16T10:58:49+00:00 heroku[worker.1]: State changed from starting to up

45

• Procfileworker: while true; do ~/bin/php ~/www/worker.php sleep 1; done

https://github.com/pixnet/pixframework-heroku/blob/master/Procfile

46

Addons – Custom domain

xxxxx.herokuapp.com

47

$ heroku domains:add www.example.com

48

Addons - Memcache

49

Memcache with SASL

50

51

heroku config

> heroku configMEMCACHE_PASSWORD => *********MEMCACHE_SERVERS => mc6.ec2.northscale.netMEMCACHE_USERNAME => app******%40heroku.com

52

https://github.com/ronnywang/PHPMemcacheSASL

include('MemcacheSASL.php');

$m = new MemcacheSASL;$m->addServer(getenv('MEMCACHE_SERVERS'), '11211');$m->setSaslAuthData(getenv('MEMCACHE_USERNAME'), getenv('MEMCACHE_PASSWORD'));var_dump($m->add('test', '123'));$m->delete('test');

53

Pix Framework on Heroku

http://framework.pixnet.net/http://github.com/pixnet/pixframewrok

54

http://github.com/pixnet/pixframework-heroku

• Core– /init.inc.php– /.gitignore– /libs/pixframework/– /models/

• Web– /.htaccess– /index.php– /controllers/– /views/

• Worker/Cron/Script– /prompt.php– /cron.php– /worker.php– /Procfile

55

Create table

# heroku run “~/bin/php ~/www/prompt”Running `~/bin/php ~/www/prompt.php` attached to terminal... up, run.1>> User::createTable()

>> exit#

56

Q&A

We are hiring!techjob@pixnet.tw

top related