Function Compute supports PHP as a managed runtime. This page covers the supported PHP version, built-in packages and extensions, and how to install custom extensions.
PHP runtime
| Version | Operating system | Architecture |
|---|---|---|
| PHP 7.2 | Linux | x86_64 |
Built-in packages
The following packages are pre-installed and available without any additional setup.
| Package | Version | Description |
|---|---|---|
| oss | v2.4.3 | Object Storage Service (OSS) SDK for PHP |
| tablestore | v4.1.1 | Tablestore SDK for PHP |
| mns | v1.3.5.5 | Simple Message Queue (formerly MNS) SDK for PHP |
| fc | v1.2.1 | Function Compute SDK for PHP |
Built-in extensions
The following extensions are enabled by default in the PHP 7.2 runtime.
To list all installed extensions at runtime, include print_r(get_loaded_extensions()); in your function code.bcmath, bz2, calendar, Core, ctype, curl, date, dom, exif, fileinfo, filter, ftp, gd, gettext, gmp, hash, iconv, imagick, json, libxml, mbstring, memcached, mysqli, mysqlnd, openSSL, pcntl, pcre, PDO, pdo_mysql, Phar, posix, protobuf, readline, redis, Reflection, session, shmop, SimpleXML, soap, sockets, SPL, standard, sysvmsg, sysvsem, sysvshm, tokenizer, Xdebug, xml, xmlreader, xmlrpc, xmlwriter, Zend OPcache, zip, zlib
Example: parse a JSON string
The following example uses the built-in json extension to parse a JSON string and print the result to stdout.
<?php
function handler($event, $context) {
var_dump(json_decode('{"a":123, "b":true, "c":"abcd", "d":{"a":123}}', true));
return "bye";
}Install a non-built-in extension
If the extension you need is not in the built-in list, install it using the PHP runtime Docker image and deploy it alongside your function code. The following steps use MongoDB as an example.
Prerequisites
Before you begin, ensure that you have:
Docker installed on your machine. For more information, see the "Install Docker" section of Install Serverless Devs and Docker.
A function with the PHP runtime already created. For more information, see the "Create a function" section of Manage functions.
Install and deploy the extension
In your project directory, start the PHP runtime container and mount the current directory to
/code:On Windows, replace
$(pwd)with an absolute path.sudo docker run -v $(pwd):/code -it --entrypoint="" registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-php7.2:latest bashInside the container, install the MongoDB extension:
pecl install mongodbFind the compiled
.sofile:find /usr -name "mongodb.so"Copy the extension file to the
/codedirectory (which maps to your project directory):cp /usr/local/lib/php/extensions/no-debug-non-zts-20170718/mongodb.so /codeExit the container:
exitVerify that
mongodb.soexists in your project directory:lsExpected output:
mongodb.soLog on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
In the top navigation bar, select a region. On the Services page, click the target service.
Click the target function. On the Code tab, create a folder named
extensionin the same directory as your handler file.Upload
mongodb.soto theextensionfolder, then create a file namedmongodb.iniin the same folder.Add the following content to
mongodb.iniand save the file:extension=/code/extension/mongodb.so
Other custom extensions
Download the following pre-built extension packages based on your requirements: