Our company has a significant investment in PHP development as well as AWS. We desire the ability to run PHP in AWS Lambda despite the fact that it isn't formally supported. We created this system for building PHP Executables that will run properly in AWS Lambda Functions.
We have tested building PHP Versions:
- 7.2
We keep pre-baked archives on the release page:
https://github.com/stechstudio/php-lambda/releases
Un-archive that and skip the entire build process if you like.
The process depends on Docker.
Clone this repository and run:
$ make
When complete you should have something like export/php-7.2.14.zip
in your working directory and the php.ini
generated in the package will be in your current directory.
$> make publish
Done! Published arn:aws:lambda:us-east-1:965741605173:layer:sts-php-7_2:1
You can create configuration files to customize PHP configuration:
- create a
php/config.d/
subdirectory in your project. - create a
php.ini
file inside that directory (the name of the file does not matter, but it must have an.ini
extensions)
PHP will automagically scan that directory and load the *.ini
files you place there, overridding any default settings.
If you would like to have PHP scan a different directory in your project, simply set the environment varialbe to and absolute path to the directory you want scanned.
The
PHP_INI_SCAN_DIR
must contain an absolute path. Since your code is placed in/var/task
on AWS Lambda the environment variable should contain something like/var/task/my/different/dir
.
Here is an example of how to define the Environment variable in your SAM template:
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
# ...
Environment:
Variables:
PHP_INI_SCAN_DIR: '/var/task/phpScanDir'
We include a common set of extensions in the PHP layer.
The layer bundles two categories of extensions.
- Those that are enabled and can not be disabled.
- Those that are disabled and can be enabled.
- OPCache - OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.
- intl - Internationalization extension (referred as Intl) is a wrapper for » ICU library, enabling PHP programmers to perform various locale-aware operations.
- APCu - APCu is APC stripped of opcode caching.
- ElastiCache php-memcached extension -
- phpredis - The phpredis extension provides an API for communicating with the Redis key-value store.
- PostgreSQL PDO Driver - PDO_PGSQL is a driver that implements the PHP Data Objects (PDO) interface to enable access from PHP to PostgreSQL databases.
- MySQL PDO Driver - PDO_MYSQL is a driver that implements the PHP Data Objects (PDO) interface to enable access from PHP to MySQL databases.
- Mongodb - Unlike the mongo extension, this extension is developed atop the » libmongoc and » libbson libraries. It provides a minimal API for core driver functionality: commands, queries, writes, connection management, and BSON serialization.
- pthreads - pthreads is an object-orientated API that provides all of the tools needed for multi-threading in PHP. PHP applications can create, read, write, execute and synchronize with Threads, Workers and Threaded objects.
You can enable these extensions by loading them in your project php/config.d/php.ini
, for example:
extension=opcache
extension=intl
extension=apcu
extension=amazon-elasticache-cluster-client.so
extension=redis
extension=pdo_pgsql
extension=pdo_mysql
extension=mongodb
extension=pthreads
If you need an extension that is not available in the layer, you will need to build the extension (and any required libraries) against the PHP binary and libraries in this Layer. Then you can either include it in your own layer, loaded after this layer, or you could simply package the extensions.so
in your project and add to PHP by setting extension=/var/task/extension.so
(must be a an absolute path) in your php/config.d/php.ini