CloudFoundry PHP Example Application: stand alone & tasks
This is an example application which can be run on CloudFoundry using the PHP Build Pack.
This is an example stand alone application and example of using CF tasks. It is simply a PHP script that runs outside of PHP-FPM as a stand alone process. It also includes a simple task that can be run with
- Clone the app (i.e. this repo).
git clone https://github.com/cloudfoundry-samples/cf-ex-stand-alone.git cd cf-ex-stand-alone
Edit the manifest.yml file. Change the 'host' attribute to something unique. Also, notice how the manifest file includes the
health-check-type: processattributes. This is important because it instructs CloudFoundry that this application will not handle web requests, which means it will not expect the application to listen on $PORT.
Push it to CloudFoundry.
cf logs php-app. This should start to tail the logs and you should see the application generate a message every five seconds. The application message will look like this.
2017-03-14T16:24:09.57-0400 [APP/PROC/WEB/0]OUT 20:24:09 php-app | Current Time From App [08:24:09]
- Run the task (optional).
cf run-task php-app 'php task.php' --name 'php-task'
cf logs php-app. This should start to tail the logs and you should see both the application and task generate a message every five seconds. The task message will look like this.
2017-03-14T16:24:11.53-0400 [APP/TASK/3839de9e/0]OUT Current Time From Task  [08:24:11]
The task should exit after 20 iterations. You can run
cf tasks to confirm it exited successfully.
How It Works
When you push the application here's what happens.
- The local bits are pushed to your target. This is small, three files around 1k.
- The server downloads the PHP Build Pack and runs it. This installs just PHP. The reason that no web server is installed is because we set the
- Because there is no web server, the build pack knows that the application is a stand alone app and it looks for a main file to execute. In this case, it finds
app.phpand so it runs it with the command
$HOME/php/bin/php -c "$HOME/php/etc" app.php.
- At this point, the build pack is done and CF runs our droplet executing the command from the previous step. It is critical that this application runs forever. If the application exits, CloudFoundry will think it has crashed and restart it.
- If you run the task, the task will spin up in a separate container. CF will execute the command indicated (i.e.
php task.php) using the same droplet as the application. The task will run and eventually exit, it should exit 0 to indicate success. You can run
cf tasks php-appto confirm it was successful.