You can now use the Upsun composable image to install runtimes and tools in your application container.
To find out more about this feature, see the dedicated documentation page.
Also, see how you can modify your PHP runtime when using the composable image.
Also, see how you can modify your PHP runtime when using the composable image.
Supported versions
You can select the major and minor version. Patch versions are applied periodically for bug fixes and the like. When you deploy your app, you always get the latest available patches.- 8.5
- 8.4
Specify the language
To use PHP, specifyphp as your app’s type:
For example:
Deprecated versions
The following versions are still available in your projects, but they’re at their end of life and are no longer receiving security updates from upstream.- 8.3
- 8.2
Retired versions
The following versions have been retired and are no longer available. If your project uses a retired version, you must update to a supported version.- 8.1
- 8.0
- 7.4
- 7.3
- 7.2
- 7.1
- 7.0
- 5.6
- 5.5
- 5.4
Usage example
Configure your app to use PHP on Upsun.1. Specify the version
Choose a supported version and add it to your app configuration:2. Serve your app
To serve your app, define what (and how) content should be served by setting thelocations parameter.
Usually, it contains the two following (optional) keys:
-
rootfor the document root, the directory to which all requests for existing.phpand static files (such as.css,.jpg) are sent. -
passthruto define a front controller to handle nonexistent files. The value is a file path relative to the app root.For enhanced security, when settingpassthrutotrue, you might also want to add the following configuration:-
Set
scriptstofalse. This prevents PHP scripts from being executed from the specified location. -
Set
allowtofalse. By default, when PHP scripts aren’t executed, their source code is delivered. Settingallowtofalseallows you to keep the source code of your PHP scripts confidential.
-
Set
locations block to fit your needs.
In the following example, all requests made to your site’s root (/) are sent to the public directory
and nonexistent files are handled by app.php:
See how to create a basic PHP app with a front controller.
To have more control, you can define rules to specify which files you want to allow from which location.
Complete example
A complete basic app configuration looks like the following:Dependencies
Up to PHP version 8.1, it’s assumed that you’re using Composer 1.x to manage dependencies. If you have acomposer.json file in your code, the default build flavor is run:
Change the build flavor
If you need more control over the dependency management, you can either use your custom build flavor or interact with Composer itself through its environment variables. You can remove the default build flavor and run your own commands for complete control over your build. Set the build flavor tonone and add the commands you need to your build hook, as in the following example:
That installs production dependencies with Composer but not development dependencies.
The same can be achieved by using the default build flavor and adding the COMPOSER_NO_DEV variable.
See more on build flavors.
Alternative repositories
In addition to the standarddependencies format,
you can specify alternative repositories for Composer to use as global dependencies.
So you can install a forked version of a global dependency from a custom repository.
To install from an alternative repository:
- Set an explicit
requireblock:
composer require platform/client 2.x-dev.
- Add the repository to use:
platformsh/client from the specified repository URL as a global dependency.
Configure security blocking
When building a PHP app, Upsun runscomposer install, which runs the latest available Composer version.
By default, PHP builds fail if a dependency in a project has a known vulnerability. A PHP build might also fail if a dependency is abandoned.
The best practice is to upgrade the dependencies to reduce security risks and to catch issues sooner. However, you can configure the level of security blocking by defining the following keys in the .dependencies.php.config section of your .upsun/config.yaml file.
Examples:
Related information:
Additional Composer schema properties
In addition to alternate repositories, other Composer schema properties can be added to the global dependencies. For example, one of your dependencies may be a plugin where you need to explicitly whitelist it as an allowed-plugin. To add additional composer schema properties:- Set an explicit
requireblock:
- Add each additional property as a block at the same indentation as the
requireblock:
Connect to services
You can access service credentials to connect to managed services from environment variables present in the application container. Consult each of the individual service documentation to see how to retrieve and surface credentials into your application.- ClickHouse
- Elasticsearch
- Gotenberg
- Headless Chrome
- InfluxDB
- Kafka
- MariaDB/MySQL
- Memcached
- mercure
- mongodb
- Network Storage
- OpenSearch
- PostgreSQL
- RabbitMQ
- Redis
- Solr
- Valkey
- Varnish
- Vault KMS
- Chroma
- Qdrant
PHP settings
You can configure your PHP-FPM runtime configuration by specifying the runtime in your app configuration. In addition to changes in runtime, you can also change the PHP settings. Some commonly used settings are:Retrieve the default values
To retrieve the default PHP values, run the following CLI command:opcache.memory_consumption, run the following command:
Retrieve the settings
To see the settings used on your environment:-
Find the PHP configuration files with the following CLI command:
The output is something like the following:
-
Display the configuration file by adapting the following command with the output from step 1:
Customize PHP settings
You can customize PHP values for your app in two ways. The recommended method is to use variables.- Using variables
- Using `php.ini`
Set variables to override PHP settings for a given environment using the CLI.For example, to set the PHP memory limit to 256 MB on a specific environment, run the following CLI command:For more information, see how to use PHP-specific variables.
Disable functions for security
A common recommendation for securing PHP installations is disabling built-in functions frequently used in remote attacks. By default, Upsun doesn’t disable any functions. If you’re sure a function isn’t needed in your app, you can disable it. For example, to disablepcntl_exec and pcntl_fork, add the following to your app configuration:
Common functions to disable include:
Execution mode
PHP has two execution modes you can choose from:- The command line interface mode (PHP-CLI) is the mode used for command line scripts and standalone apps.
This is the mode used when you’re logged into your container via SSH, for crons,
and usually also for alternate start commands.
To use PHP-CLI, run your script with
php <PATH_TO_SCRIPT>, where<PATH_TO_SCRIPT>is a file path relative to the app root. - The Common Gateway Interface mode (PHP-CGI) is the mode used for web apps and web requests.
This is the default mode when the
startcommand isn’t explicitly set. To use PHP-CGI, run your script with a symlink:/usr/bin/start-php-app <PATH_TO_SCRIPT>, where<PATH_TO_SCRIPT>is a file path relative to the app root. With PHP-CGI, PHP is run using the FastCGI Process Manager (PHP-FPM).
Alternate start commands
To specify an alternative process to run your code, set astart command.
For more information about the start command, see the web commands reference.
By default, start commands use PHP-CLI.
Find out how and when to use each execution mode.
Note that the start command must run in the foreground and is executed before the deploy hook.
That means that PHP-FPM can’t run simultaneously with another persistent process
such as ReactPHP
or Amp.
If you need multiple processes, they have to run in separate containers.
See some generic examples on how to use alternate start commands:
- Run a custom script
- Run a custom web server
- Run specific tasks
- Add your script in a PHP file.
-
Specify an alternative
startcommand by adapting the following:
.upsun/config.yaml
Foreign function interfaces
PHP 7.4 introduced support for foreign function interfaces (FFIs). FFIs allow your PHP program to call routines or use services written in C or Rust. Note: FFIs are only intended for advanced use cases. Use with caution. If you are using C code, you need.so library files.
Either place these files directly in your repository or compile them in a makefile using gcc in your build hook.
Note: The .so library files shouldn’t be located in a publicly accessible directory.
If you are compiling Rust code, use the build hook to install Rust.
To leverage FFIs, follow these steps:
- Enable and configure OPcache preloading.
- Enable the FFI extension:
-
Make sure that your preload script calls the
FFI::load()function. Using this function in preload is considerably faster than loading the linked library on each request or script run. - If you are running FFIs from the command line, enable the preloader by adding the following configuration:
-
Run your script with the following command:
Frameworks
All major PHP web frameworks can be deployed on Upsun. See dedicated guides for deploying and working with them:Modify your PHP runtime when using the composable image
The following table presents the possible modifications you can make to your PHP primary runtime using thestack.runtimes key in a composable image.
For example, extensions are enabled under .applications.frontend.stack.runtimes[0]["php@8.5"].extensions for PHP 8.5).
See the example below for more details.
The PHP-FPM service starts automatically only when PHP is the primary runtime.
PHP-FPM service sizing hints
The following table shows the properties that can be set insizing_hints:
See more about PHP-FPM workers and sizing.
Example PHP configuration
Here is an example configuration:You can also set your app’s runtime timezone.