Skip to content

cloudfoundry/php-buildpack

Cloud Foundry PHP Buildpack

CF Slack Join us on Slack

A buildpack to deploy PHP applications to Cloud Foundry based systems, such as a cloud provider or your own instance.

Buildpack User Documentation

Official buildpack documentation can be found here: php buildpack docs.

Building the Buildpack

To build this buildpack, run the following commands from the buildpack's directory:

  1. Source the .envrc file in the buildpack directory.

    source .envrc

    To simplify the process in the future, install direnv which will automatically source .envrc when you change directories.

  2. Install buildpack-packager

    go install github.com/cloudfoundry/libbuildpack/packager/buildpack-packager@latest
  3. Build the buildpack

    buildpack-packager build [ --cached ]

    Alternatively, use the package script:

    ./scripts/package.sh [ --cached ]
  4. Use in Cloud Foundry

    Upload the buildpack to your Cloud Foundry and optionally specify it by name

    cf create-buildpack [BUILDPACK_NAME] [BUILDPACK_ZIP_FILE_PATH] 1
    cf push my_app [-b BUILDPACK_NAME]

Contributing

Find our guidelines here.

Testing

Buildpacks use the Switchblade framework for running integration tests against Cloud Foundry. Before running the integration tests, you need to login to your Cloud Foundry using the cf cli:

cf login -a https://api.your-cf.com -u name@example.com -p pa55woRD

Note that your user requires permissions to run cf create-buildpack and cf update-buildpack. To run the integration tests, run the following commands from the buildpack's directory:

  1. Source the .envrc file in the buildpack directory.

    source .envrc

    To simplify the process in the future, install direnv which will automatically source .envrc when you change directories.

  2. Run unit tests

    ./scripts/unit.sh
  3. Run integration tests

    ./scripts/integration.sh

More information can be found on Github switchblade.

Project Structure

The project is broken down into the following directories:

  • bin/ - Executable shell scripts for buildpack lifecycle: detect, supply, finalize, release, start, rewrite
  • src/php/ - Go source code for the buildpack
    • detect/ - Detection logic
    • supply/ - Dependency installation (PHP, HTTPD, Nginx)
    • finalize/ - Final configuration and setup
    • release/ - Release information
    • extensions/ - Extension system (composer, newrelic, dynatrace, appdynamics, sessions)
    • config/ - Configuration management
    • options/ - Options parsing
    • hooks/ - Lifecycle hooks
    • integration/ - Integration tests
    • unit/ - Unit tests
  • defaults/ - Default configuration files
  • fixtures/ - Test fixtures and sample applications
  • scripts/ - Build and test scripts

Understanding the Buildpack

This buildpack uses Cloud Foundry's libbuildpack framework and is written in Go. The buildpack lifecycle consists of:

Build-Time Phases

  1. Detect (bin/detectsrc/php/detect/) - Determines if the buildpack should be used by checking for PHP files or composer.json

  2. Supply (bin/supplysrc/php/supply/) - Installs dependencies:

    • Downloads and installs PHP
    • Downloads and installs web server (HTTPD or Nginx)
    • Runs extensions in "configure" and "compile" phases
    • Installs PHP extensions
    • Runs Composer to install application dependencies
  3. Finalize (bin/finalizesrc/php/finalize/) - Final configuration:

    • Configures web server (HTTPD or Nginx)
    • Sets up PHP and PHP-FPM configuration
    • Copies rewrite and start binaries to .bp/bin/
    • Generates preprocess scripts that will run at startup
    • Prepares runtime environment
  4. Release (bin/releasesrc/php/release/) - Provides process types and metadata

Runtime Phases

  1. Rewrite (bin/rewritesrc/php/rewrite/cli/) - Configuration templating at runtime:

    • Called during application startup (before services start)
    • Replaces template patterns in configuration files with runtime environment variables
    • Supports patterns: @{VAR}, #{VAR}, @VAR@, #VAR
    • Allows configuration to adapt to the actual runtime environment (ports, paths, etc.)
    • Rewrites PHP, PHP-FPM, and web server configs
  2. Start (bin/startsrc/php/start/cli/) - Process management:

    • Runs preprocess commands (including rewrite operations)
    • Launches all configured services (PHP-FPM, web server, etc.) from .procs file
    • Monitors all processes
    • If any process exits, terminates all others and restarts the application

Extensions

The buildpack includes several built-in extensions written in Go:

Extension Architecture

Extensions implement the Extension interface defined in src/php/extensions/extension.go:

type Extension interface {
    Name() string
    ShouldCompile(ctx *Context) (bool, error)
    Configure(ctx *Context) error
    Compile(installer Installer) error
    PreprocessCommands(ctx *Context) ([]string, error)
    ServiceCommands(ctx *Context) (map[string]string, error)
    ServiceEnvironment(ctx *Context) (map[string]string, error)
}

Extension Lifecycle:

  1. Configure - Called early to modify buildpack configuration (e.g., set PHP version, add extensions)
  2. Compile - Main extension logic, downloads and installs components
  3. ServiceEnvironment - Contributes environment variables
  4. ServiceCommands - Contributes long-running services
  5. PreprocessCommands - Contributes commands to run before services start

For examples, see the built-in extensions in src/php/extensions/.

Note: Custom user extensions from .extensions/ directory are not currently supported in the Go-based buildpack. This feature may be added in a future release.

Help and Support

Join the #buildpacks channel in our Slack community

Reporting Issues

This project is managed through GitHub. If you encounter any issues, bug or problems with the buildpack please open an issue.

Active Development

The project backlog is on Pivotal Tracker

About

A Cloud Foundry Buildpack for PHP.

Topics

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 80