PHP applications are supported in the Legacy Buildpack using Apache and mod_php, and require very little
configuration to deploy.
Alternative buildpacks specifically for PHP are also available:
To use one of these, specify the buildpack URL in the buildpack:
value in your manifest.yml file, or using the --buildpack CLI
option.
The steps below describe deployment using the Legacy Buildpack, though the instructions on parsing environment variables for data service credentials are applicable to any PHP application.
You will need at least two files to deploy a PHP app on Helion Stackato: index.php, and manifest.yml.
The manifest must specify the php as the framework type:
framework:
type: php
For full details on manifest.yml and all possible options, see Manifest.yml Options.
Some applications require the user to specify the APP_URL. Below is an example on how to obtain the correct urls:
$appinfo = getenv("VCAP_APPLICATION");
$appinfo_json = json_decode($appinfo,true);
$admin = $appinfo_json['uris'][0];
Non-HTTP apps that run as a Helion Stackato application under the control of the Health Manager.
To deploy worker applications, you need to use the
command: key and set the
processes: web: key to Null (~).
name: php-app
framework: php
command: php worker.php
processes:
web: ~
Authentication details for your configured database services can be found in the $_SERVER variable,
under DATABASE_URL. Here is an example of getting the correct credentials.
<?php
$url_parts = parse_url($_SERVER['DATABASE_URL']);
$db_name = substr( $url_parts['path'], 1 );
$db_connection_string = $url_parts['host'] . ':' . $url_parts['port'];
// ** MySQL settings from resource descriptor ** //
echo $db_name;
echo $url_parts['user'];
echo $url_parts['pass'];
echo $url_parts['host'];
echo $url_parts['port'];
?>
<?php
$services = getenv("VCAP_SERVICES");
$services_json = json_decode($services,true);
$mysql_config = $services_json["mysql"][0]["credentials"];
// ** MySQL settings from resource descriptor ** //
echo $mysql_config["name"];
echo $mysql_config["user"];
echo $mysql_config["password"];
echo $mysql_config["hostname"];
echo $mysql_config["port"];
);
?>
Additional PHP ini files will be loaded from the $STACKATO_APP_ROOT/apache/php/
directory. Refer to the example below for more information.
If your document root (the location of the main index.php file) is the
main application directory, the information stored in manifest.yml is
exposed to the browser.
Note
The manifest.yml file is generated automatically.
To prevent exposing this information, you can use an .htaccess file in
the document root directory with the following rule:
<filesmatch "^(manifest|stackato)\.yml$">
order allow,deny
deny from all
</filesmatch>
Alternatively, move your application into a subdirectory (for example, move
index.php to www/index.php) and explicitly set your document-root in
manifest.yml:
framework:
document-root: www
Using the .htaccess file will generate an HTTP 403 Forbidden error
if a user tries to access the denied files. Changing the document-root
will generate an HTTP 404 Not Found error instead.
These techniques can be use to hide other files in your application source tree which you do not want exposed to end users.
Helion Stackato serves web applications port 80 and/or 443 at the router, but
within the application container Apache will be running on a different
port. PHP will report this internal IP address and port in the
SERVER_ADDR and SERVER_PORT Apache environment variables respectively.
If your application makes use of these variables, you may need to adjust them by using an .htaccess file to set one or more RewriteRule directive to correct the server name or port in URLs.
One of the issues with managing a PHP application running multiple instances is dealing with user sessions.
If your application uses a shared filesystem service, you can store user sessions there. The
following manifest.yml snippet creates a persistent filesystem
service, creates a directory for sessions, and writes a PHP config file
to set the path to the session directory:
services:
${name}-fs: filesystem
hooks:
post-staging:
- mkdir -p "$STACKATO_FILESYSTEM"/sessions
- echo "session.save_path = $STACKATO_FILESYSTEM/sessions" > "$STACKATO_APP_ROOT"/apache/php/sessions.ini
For better performance, use a Memcached service for session storage instead:
services:
${name}-cache: memcached
hooks:
post-staging:
- echo "session.save_handler = memcached" > "$STACKATO_APP_ROOT"/apache/php/sessions.ini
- echo "session.save_path = $MEMCACHED_URL" >> "$STACKATO_APP_ROOT"/apache/php/sessions.ini
Additional modules for PHP can be added in the requirements
section of manifest.yml (as Ubuntu
packages) only if administrators have enabled an Ubuntu PPA for PHP 5.5.
These modules are not available from the standard Ubuntu 12.04
repositories configured in Helion Stackato by default.