PHP

Detection

Your application will be detected as a PHP application if:

  • There is a composer.lock file at the root of your project
  • index.php is present in the root directory (legacy app)

Stack

The PHP buildpack provided by Scalingo is based on Nginx and PHP-FPM.

Composer

Composer is the official package manager for PHP. The official website is https://getcomposer.org/. You can find their its configuration. This buildpack is based on Composer. The dependencies of your app will be installed according to your Composer configuration

composer.json

This file defines the different dependencies used by your application. It is also used to configure custom settings of your project (see below)

composer.lock

When you have defined the different third-party libraries you want to use, you need to fix the version you want to install. These versions will be written in a compose.lock file. This file will be read to deploy your application on Scalingo, so it is required

To generate compose.lock you need to run

$ composer install

When you need to upgrade a library, for instance slim:

$ composer update slim/slim

The composer.lock file will be updated automatically, do not forget to commit the modifications of the file.

Composer during the deployment

We consider that when you deploy your application on Scalingo that your want it to run in “production” mode. As a result, composer install is run with the flag --no-dev.

If, in order to debug your application, you wish to install development dependencies, you can set the following environment variable:

  • COMPOSER_DEVtrue

Officially supported frameworks

For these framework we have done some extra work to save your configuration worktime. Actually you have nothing special to configure in your composer.json, just git push your code and everything will work out of the box.

List of the frameworks:

  • Symfony 2
  • Zend Framework 2
  • Magento
  • Slim
  • Laravel
  • Lumen
  • Silex
  • Cake PHP 2
  • Change 2

Configuration tweaks

Setup basic auth

You may want to hide your application behind a simple authentication gateway, you can configure HTTP basic auth for you application.

PHP FPM concurrency

The level of concurrency configured is defined automatically according to the size of the containers of your app. If you want to override this value, you can define the environment variable: WEB_CONCURRENCY

The value defined here, directly modify the pm.max_children parameter, defining how much process will be run by php-fpm.

Buildpack tweaks - custom configuration

What is a buildpack

The buildpack allows you to configure precisely how your application are deployed. This configuration is done in your composer.json at the root of your project.

All these fields are optional, define them if you need to modify their default value.

{
   // Your content
  "extra": {
    // default values of PaaS specific instructions
    "paas": {
      "document-root": "",
      "index-document": "index.php",
      "engines": {
        "nginx": "default",
        "php": "default"
      },
      "php-config": [],
      "php-includes": [],
      "php-fpm-config": [],
      "php-fpm-includes": [],
      "nginx-includes": [],
      "log-files": [],
      "compile": [],
      "new-relic": false,
      "access-log-format": ""
    }
  }
}

.extra.paas.document-root

The directory where nginx will define the root of your app.

It is empty by default, it means that nginx will look at the index.php at the root of your project.

This parameter can also be overrided with the environment variable:

  • DOCUMENT_ROOT

.extra.paas.index-document

Name of the index document of each directory. When accessing a directory (https://example.scalingo.io/web/), nginx looks by default to an index.php file to execute, if your project is using another name, specify it here.

.extra.paas.engines.nginx

Define a precise version of nginx to use. By default we prive to your app the last stable version of the server, so in most of the time, you don’t need to change its value.

.extra.paas.engines.php

Define a precise version of PHP to use. The last stable version will be installed if nothing is specified.

Example

{
  "engines": {
    "php": "~7.0"
  },
}

.extra.paas.php-config

List of directive which will be added to your php.ini

Examples

{
  "php-config": [
    "display_errors=off",
    "short_open_tag=on"
  ]
}

.extra.paas.php-fpm-config

List of directive which will be added to your php-fpm.ini

Examples

{
  "php-fpm-config": [
    "log_level=debug"
  ]
}

.extra.paas.php-includes

Extra files which will be included in the php.ini

.extra.paas.php-fpm-includes

Extra files which will be included in the php-fpm.ini

.extra.paas.nginx-includes

Extra files which will be included in the Nginx configuration of your application

.extra.paas.compile

Commands to run after the dependency installation.

{
  "compile": [
    "php app/console assetic:dump --env=prod --no-debug"
  ]
}

.extra.paas.log-files

If your project is writing in custom logfiles, you can specify them here and they will be streamed to the logs of your application.

Your can read the logs on your dashboard or with the Scalingo CLI utility.

.extra.paas.new-relic

If true, enable new-relic instrumentation tools.

The environment variable named NEW_RELIC_LICENSE_KEY is required.

.extra.paas.access-log-format

The format of the logs produced by the proxy nginx for each request to your application.

It is empty by default, it means that nginx will use the following format:

'$http_x_forwarded_for - "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"'

ie.

109.26.203.98 - "GET / HTTP/1.1" 200 1761 "http://google.com/search" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:51.0) Gecko/20100101 Firefox/51.0"

Note that the default output doesn’t display any timestamp, as Scalingo log aggregation system will prepend it in the global logs of the application: For instance 2017-01-26 12:02:06.504267001 +0100 CET [web-1] <nginx log output>

So when defining a custom value for the log format, please refer to the nginx documentation page

This parameter can also be overrided with the environment variable:

  • ACCESS_LOG_FORMAT

Warning: .htaccess files

Legacy projects often use .htaccess file in their project to modify the configuration of the frontal Apache server. As the buildpack is based on Nginx + PHP-FPM, these files are ineffective.

Instead of using these files, you have write directives for nginx and configure the nginx-includes part of your composer.json. It can contains one or more configuration files:

{
  
  "extra": {
    "paas": {
      "nginx-includes" = ["path/to/nginx_conf_file", ]
    }
  }
}

Example: URL rewriting (i.e. Wordpress)

Here is an example of legacy .htaccess. This file won’t work on Scalingo, you need to replace it with the nginx configuration following the example.

.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

nginx configuration

location / {
  try_files $uri $uri/ /index.php?$args;
}
schedule 26 Jan 2017