PHP with Apache application runtime
Overview
PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.
The HTTP server is Apache 2, and the PHP code is executed by PHP-FPM.
Create your PHP application
To create a new PHP application, use the Clever Cloud Console or Clever Tools:
clever create --type phpConfigure your PHP application
Mandatory needs
PHP runtime requires a working web application. The HTTP server is Apache 2 with PHP-FPM. If you need to serve files from a specific directory, set the CC_WEBROOT environment variable (e.g. /public).
clever env set CC_WEBROOT /publicBuild phase
Composer build is supported out of the box. If a composer.json file is present at the root of your repository, dependencies are installed automatically during the build phase.
PHP version
Set the CC_PHP_VERSION environment variable to one of the following versions.
| Default Version | Accepted Versions |
|---|---|
8 (8.4) | 5.6 (EOL) |
7 (EOL) | |
7.1 (EOL) | |
7.2 (EOL) | |
7.3 (EOL) | |
7.4 (EOL) | |
8 | |
8.0 (EOL) | |
8.1 (EOL) | |
8.2 | |
8.3 | |
8.4 | |
8.5 |
The end-of-life (EOL) versions are marked as such.
All new PHP applications are created with a default CC_PHP_VERSION. You can change it whenever you want then redeploy your application to use the version you want. Only values based on the first two digits (X or X.Y, not X.Y.Z) are supported.
Custom PHP configuration
Most PHP settings can be changed using a .user.ini file.
If you want the settings to be applied to the whole application, you should put this file in your webroot. If you did not change it, then your webroot is the root of the repository.
If you put the .user.ini file in a subdirectory, settings will be applied recursively starting from this subdirectory.
You can review the available directives; all the PHP_INI_USER, PHP_INI_PERDIR, and PHP_INI_ALL directives can be set from within .user.ini.
Same configuration between PHP-CLI and PHP-FPM
.user.ini files are not loaded by the PHP CLI by default.
However, some PHP applications may want to check for the PHP-FPM configuration pre-requisites, post_max_size or upload_max_filesize values for example.
To load the PHP-FPM .user.ini file during a PHP-CLI process, in a hook, use the PHP_INI_SCAN_DIR environment variable to load the additional file.
Assuming the script runs at the root-folder of the application:
#!/usr/bin/env bash
export PHP_INI_SCAN_DIR=":."
php myscript.phpThis appends the current directory while still loading the default configuration.
Note
The : at the beginning of the string is mandatory. It indicates defaults files must still load.
A specific .ini file can be loaded with:
#!/usr/bin/env bash
export PHP_INI_SCAN_DIR=":.php-configuration/"
php myscript.phpThis loads every .ini files in the php-configuration/ directory.
Timezone configuration
All instances on Clever Cloud run on the UTC timezone. We recommend to handle all your dates in UTC internally, and only handle timezones when reading or displaying dates.
Additionally, you can set PHP’s time zone setting with .user.ini. For instance, to use the french time zone, edit .user.ini to add this line:
date.timezone=Europe/ParisMemory Limit
When php-fpm spawns a worker it allocates a smaller part of the application’s memory to the worker, here is the allocated memory for each flavor:
| Flavor | Memory Limit |
|---|---|
| Pico | 64M |
| Nano | 64M |
| XS | 128M |
| S | 256M |
| M | 384M |
| L | 512M |
| XL | 768M |
| 2XL | 1024M |
| 3XL | 1536M |
| 4XL+ | 2048M |
To change this limit you can define MEMORY_LIMIT environment variable.
If you define a limit exceeding the application memory it will use the default one.
Frameworks and CMS
The following is the list of tested CMS by our team.
It’s quite not exhaustive, so it does not mean that other CMS can’t work on the Clever Cloud platform.
Others PHP frameworks tested on Clever Cloud:
- Prestashop
- Dokuwiki
- Joomla
- SugarCRM
- Drupal
- Magento
- Status.net
- Symfony
- Thelia
- Laravel
- Sylius
Configure Monolog
A lot of frameworks (including Symfony) use Monolog to handle logging. The default configuration of Monolog doesn’t allow to log errors into the console.
Here is a basic configuration of Monolog to send your application’s logs into our logging system and access them into the Console:
monolog:
handlers:
clever_logs:
type: error_log
level: warningYou can change the level to whatever level you desire. For Symfony, the configuration file is app/config/config_prod.yml.
Laravel doesn’t need Monolog to retrieve logs via Clever console or Clever CLI. Here, ensure that you have the following line in config/app.php:
return [
// …
'log' => env('APP_LOG'),
// …
];Then, set APP_LOG=syslog as Clever application environment variable.
Monitor your application with New Relic
You can use New Relic to monitor your application on Clever Cloud.
Please refer to our New Relic documentation to configure it for your application.
Monitor your application with Blackfire
You can use Blackfire to monitor your application on Clever Cloud.
Please refer to our Blackfire documentation to configure it for your application.
ProxySQL
ProxySQL is a tool that acts like a proxy between your application and your MySQL add-on. Instead of connecting to your MySQL add-on, you can connect to the local ProxySQL and it will forward all your requests to your MySQL add-on.
This allows you to let ProxySQL take care of some interesting features like connection pooling or leader / follower setup.
You can learn more about ProxySQL on the dedicated documentation page
Enable health check during deployment
The healthcheck allows you to limit downtimes. Indeed, you can provide Clever Cloud with paths to check. If these paths return something other than 200, the deployment will fail.
Add one (or several) environment variable as such:
CC_HEALTH_CHECK_PATH=/my/awesome/pathOr
CC_HEALTH_CHECK_PATH_0=/my/awesome/path
CC_HEALTH_CHECK_PATH_1=/my/other/pathThe deployment process checks all paths. All of them must reply with a 200 OK response code.
By default, when no environment variable (for ex: APP_HOME) is defined, the monitoring checks your repository root path /.
Example
Using the path listed above, below are the expected logs:
Response from GET /my/awesome/path is 200
Response from GET /my/other/path is 500
Health check failed:
- GET /my/other/path returned 500.
If the deployment fails after this message, please update your configuration and redeploy.In this example, the first path is OK, but the second one failed. This gives you a hint on what failed in your application.
Best practice for healthcheck endpoints
To make the most of a healthcheck endpoint, have it check your critical dependencies. For example:
- execute
SELECT 1 + 1;on your database - retrieve a specific Cellar file
- ping a specific IP through a VPN
Request Flow: Varnish, Redirection.io, custom proxy
Request Flow automatically chains reverse proxies between port 8080 (public) and your application, managing port allocation with no manual configuration. Supported services are activated by their presence in your project:
- Otoroshi Challenge: set
OTOROSHI_CHALLENGE_SECRET - Varnish: add a
clevercloud/varnish.vclfile or setCC_VARNISH_FILE - Redirection.io: set
CC_REDIRECTIONIO_PROJECT_KEY
All three can be active simultaneously. To control the order, set CC_REQUEST_FLOW (e.g. redirectionio,varnish). To add a custom middleware, include custom in the chain and define CC_REQUEST_FLOW_CUSTOM with @@LISTEN_PORT@@ and @@FORWARD_PORT@@ placeholders. To block public access, set CC_REQUEST_FLOW=block.
When at least one middleware is active, your application must listen on port 9000 instead of 8080.