Serverless App Engine (SAE) supports multiple PHP runtime environments, and you can select an environment based on your business requirements.
Overview
Web applications: NGINX and PHP-FPM
SAE provides an LNMP runtime environment in which web applications can run. LNMP is an abbreviation of Linux, NGINX, MySQL, and PHP. The environment consists of the following components:
The default base image of PHP FastCGI Process Manager (FPM).
NGINX.
Common PHP extensions.
If your application requires more resources, you can use the following Alibaba Cloud services:
Application monitoring: ARMS
You can use Application Real-Time Monitoring Service (ARMS) to monitor SAE applications that run PHP of a version between 5.4 and 7.3. ARMS allows you to obtain application information, such as application traces, and the latency when you access ApsaraDB RDS for MySQL resources and Tair (Redis OSS-Compatible) resources. For more information, see ARMS overview.
PHP runtime environments
Software versions
The following table describes the available PHP runtime environments. If you require an environment of later versions, submit a ticket or join the DingTalk group 32874633 for technical support.
Runtime environment | Updated at | PHP version | NGINX version | Operating system version | Description |
PHP-FPM 7.3 | 2021-12-27 | 7.3.32 | 1.18.0 | Debian 11.0 |
|
PHP-FPM 7.2 | 2021-09-17 | 7.2.34 | 1.18.0 | Debian 10.7 | None |
PHP-FPM 7.1 | 2021-09-17 | 7.1.33 | 1.18.0 | Debian 10.2 | None |
PHP-FPM 7.0 | 2021-09-17 | 7.0.33 | 1.18.0 | Debian 9.6 | None |
PHP-FPM 5.6 | 2021-09-17 | 5.6.40 | 1.18.0 | Debian 9.6 | None |
PHP-FPM 5.5 | 2021-09-17 | 5.5.38 | 1.18.0 | Debian 8.5 | None |
PHP-FPM 5.4 | 2021-09-17 | 5.4.45 | 1.18.0 | Debian 8.2 | None |
Default Directories
SAE allows you to deploy application packages in PHP runtime environments by using the following directories.
Type | Directory | Description |
Directory | /home/admin/app/ | The default directory in which application packages are deployed. |
Directory | /home/admin/app/nginx/ |
|
Directory | /home/admin/app/php/ | The default directory in which PHP is deployed. The directory is ./php/ in the demo package. |
File | /usr/local/etc/php.ini | The recommended directory in which the php.ini configuration file is stored. For more information, see Configure a configuration file for a PHP application. |
The following table describes the default configurations of PHP-FPM.
Type | Directory | Description |
Directory | /usr/local/etc/php |
|
Directory | /usr/local/etc/php/conf.d/ | The directory in which the PHP extensions are stored. By default, the extensions are included in .ini files. |
File | /usr/local/etc/php-fpm.conf | The default configuration file for PHP-FPM. For more information, see the Configuration section for FastCGI Process Manager (FPM). |
The following table describes the default configurations of NGINX.
Type | Directory | Description |
File | /usr/sbin/nginx | The directory of NGINX. |
Directory | /etc/nginx/ | The directory of default NGINX configurations. |
File | /etc/nginx/nginx.conf | The default configuration file nginx.conf. The file contains |
Built-in extensions
PHP runtime environments provided by SAE support the following extensions:
Official PHP extensions: released together with PHP updates.
PHP PECL extensions: independently released and maintained by the PECL community.
Related permissions
By default, PHP-FPM and NGINX use the www-data user to run applications. The following table describes the permissions.
Service | User group that is granted specific permissions | Description |
PHP-FPM | Process
| Default user or user group of PHP. The user or user group is specified in the configuration file in the /usr/local/etc/php-fpm.conf directory. |
Nginx | Process
| Default user or user group of NGINX. The user or user group is specified in the configuration file in the /etc/nginx/nginx.conf directory. |
File directory | Directory
| The directory in which application packages are deployed. Specify /home/admin/app/ for the directory. Examples:
|
We recommend that you retain the user to prevent application failures.
Support for PHP extensions
Default and PECL extensions
You can configure the php.ini file to specify built-in extensions when you deploy an application, including the default PHP extensions and PECL extensions. For more information, see Configure a configuration file for a PHP application.
Run the following command in the webshell to view the built-in extensions. For information about how to use the webshell, see Display the webshell window in full screen.
php -m
Examples of built-in extensions supported by different runtime environments:
PECL 7.3
[PHP Modules]
amqp
ARMS
bcmath
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gettext
gmp
hash
iconv
igbinary
imagick
json
libxml
mbstring
mcrypt
memcached
mongodb
msgpack
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
redis
Reflection
session
shmop
SimpleXML
soap
sockets
sodium
SPL
sqlite3
standard
swoole
sysvmsg
sysvsem
sysvshm
timezonedb
tokenizer
xdebug
xml
xmlreader
xmlrpc
xmlwriter
yaml
Zend OPcache
zip
zlib
[Zend Modules]
Xdebug
Zend OPcache
PECL 7.2
[PHP Modules]
amqp
ARMS
bcmath
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gettext
gmp
hash
iconv
igbinary
imagick
json
libxml
mbstring
mcrypt
memcached
mongodb
msgpack
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
redis
Reflection
session
shmop
SimpleXML
soap
sockets
sodium
SPL
sqlite3
standard
swoole
sysvmsg
sysvsem
sysvshm
timezonedb
tokenizer
xdebug
xml
xmlreader
xmlrpc
xmlwriter
yaml
Zend OPcache
zip
zlib
[Zend Modules]
Xdebug
Zend OPcache
PECL 7.1
[PHP Modules]
amqp
ARMS
bcmath
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gettext
gmp
hash
iconv
igbinary
imagick
json
libxml
mbstring
mcrypt
memcached
mongodb
msgpack
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
redis
Reflection
session
shmop
SimpleXML
soap
sockets
sodium
SPL
sqlite3
standard
swoole
sysvmsg
sysvsem
sysvshm
timezonedb
tokenizer
xdebug
xml
xmlreader
xmlrpc
xmlwriter
yaml
Zend OPcache
zip
zlib
[Zend Modules]
Xdebug
Zend OPcache
PECL 7.0
[PHP Modules]
amqp
ARMS
bcmath
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gettext
gmp
hash
iconv
igbinary
imagick
json
libxml
mbstring
memcached
mongodb
msgpack
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
redis
Reflection
session
shmop
SimpleXML
soap
sockets
sodium
SPL
sqlite3
standard
swoole
sysvmsg
sysvsem
sysvshm
timezonedb
tokenizer
xdebug
xml
xmlreader
xmlrpc
xmlwriter
yaml
Zend OPcache
zip
zlib
[Zend Modules]
Xdebug
Zend OPcache
PECL 5.6
[PHP Modules]
amqp
ARMS
bcmath
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gettext
gmp
hash
iconv
igbinary
imagick
json
libxml
mbstring
memcached
mongodb
msgpack
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
redis
Reflection
session
shmop
SimpleXML
soap
sockets
sodium
SPL
sqlite3
standard
swoole
sysvmsg
sysvsem
sysvshm
timezonedb
tokenizer
xdebug
xml
xmlreader
xmlrpc
xmlwriter
yaml
Zend OPcache
zip
zlib
[Zend Modules]
Xdebug
Zend OPcache
PECL 5.5
[PHP Modules]
amqp
ARMS
bcmath
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gettext
gmp
hash
iconv
igbinary
imagick
json
libxml
mbstring
memcached
mongodb
msgpack
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
redis
Reflection
session
shmop
SimpleXML
soap
sockets
sodium
SPL
sqlite3
standard
swoole
sysvmsg
sysvsem
sysvshm
timezonedb
tokenizer
xdebug
xml
xmlreader
xmlrpc
xmlwriter
yaml
Zend OPcache
zip
zlib
[Zend Modules]
Xdebug
Zend OPcache
PECL 5.4
[PHP Modules]
amqp
ARMS
bcmath
bz2
calendar
Core
ctype
curl
date
dom
ereg
exif
fileinfo
filter
ftp
gettext
gmp
hash
iconv
igbinary
imagick
json
libxml
mbstring
memcached
mongodb
msgpack
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
recode
redis
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
swoole
sysvmsg
sysvsem
sysvshm
timezonedb
tokenizer
xdebug
xml
xmlreader
xmlrpc
xmlwriter
yaml
zip
zlib
[Zend Modules]
Xdebug
Install extensions by using the console or API
You can enable the default PHP extensions or install specific PECL extensions in the SAE console or by calling the API based on your business requirements. For more information, see Deploy a PHP application by using a ZIP package in the SAE console.
Install third-party extensions
This section describes how to install a third-party extension by compiling the YAML file that contains the source code in PHP-FPM 7.3.
Create an SAE application that runs in PHP 7.3.
Allow the application to access the Internet.
Log on to the instance by using the webshell, and run the following command to compile and install the extension:
# Obtain the source code. wget https://pecl.php.net/get/yaml-2.2.2.tgz tar -zxvf yaml-2.2.2.tgz # Compile and install the extension. cd yaml-2.2.2 && \ phpize && \ ./configure && \ make && make install
Download the extension to your on-premises machine. For more information, see Upload and download logs to check the health status of applications.
The directory of the extension:
/home/admin/yaml-2.2.2/modules/yaml.so
Include the extension in the ZIP package of the application. ZIP package structure:
. ├── nginx │ ├── default.conf │ ├── fastcgi_params │ ├── global │ └── root.dir ├── php │ ├── index.php └── extensions └── yaml.so
Deploy the application, upload the ZIP package that contains the extension, and then add the extension configuration to the php.ini file to enable the extension.
extension=/home/admin/app/extensions/yaml.so
Log on to the instance by using the webshell and check whether the extension is loaded.
# php -m | grep yaml yaml
Extension enhancement
PHP can optimize the following extensions: WebP, JPEG, PNG, zlib, XPM, and FreeType. For more information, see Image Processing and GD.
Common configurations
Default service ports
The following table describes the default service ports that are configured in SAE for NGINX and PHP-FPM.
Item | Port |
Nginx |
|
PHP-FPM |
|
PHP-FPM logs: standard output
PHP-FPM logs are contained in real-time logs. The following sample code provides an example of the log configurations. The configuration file is stored in the /usr/local/etc/php-fpm.d/docker.conf directory.
[global] error_log = /proc/self/fd/2 log_limit = 8192 [www] access.log = /proc/self/fd/2
The PHP error logs that are not displayed. The following sample code provides an example of the log configuration in the php.ini file.
error_log => no value => no value log_errors => Off => Off log_errors_max_len => 1024 => 1024
If you want to include the error logs in real-time logs, you must configure the php.ini file when you create or deploy an application. You must specify the following directory to store the error logs.
log_errors = on error_log = /proc/self/fd/2
NGINX logs: standard output
NGINX logs are contained in real-time logs. The following sample code provides an example of the log configurations. The configuration file is stored in the ./nginx/default.conf directory.
#access_log /dev/stdout; error_log stderr;
You can configure
access_log
to determine whether to include access logs in the real-time logs.
File caching: static files, images, and media
You can configure the php.ini file to determine whether to enable OPcache for PHP. By default, OPcache is enabled in SAE. For more information, see OPcache configuration options in Runtime Configuration.
Enable OPcache: This option is suitable for online websites to accelerate code execution and reduce disk input/output (I/O).
Disable OPcache: This option is suitable for debugging scenarios. Code modifications can immediately take effect.
Configurations in the php.ini
file:
; Enable OPcache.
opcache.enable=1
; Disable OPcache.
;opcache.enable=0
File caching: PHP code
You can enable or disable caching in NGINX for a large number of static files, such as images, videos, Cascading Style Sheets (CSS) files, and JavaScript files. For more information, see the sample code and Module ngx_http_headers_module.
Enable static resource caching: This option is suitable for online services and websites that load a large number of media files. This improves traffic throughput and reduces read input/output operations per second (IOPS).
Disable static resource caching: This option is suitable for remote debugging, upload and download, and other usage scenarios. You can access the files in real time.
Configurations in the ./nginx/default.conf directory:
# Static resource caching
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
# Disable caching.
#expires off;
# Enable caching.
expires 1s;
}
Enhancement
Hot restart
ConfigMap allows you to modify the configurations without interrupting services.
Webshell allows you to restart services without interrupting services.
Hot restart of NGINX
# Check the validity of the configuration file. nginx -t # Restart NGINX without service interruptions. nginx -s reload
Hot restart of PHP-FPM (The services may be interrupted. You must make sure that the php.ini file is valid.)
ps uax | grep 'php-fpm: master' | grep -v grep | awk '{print $2}' | xargs kill -USR2
Upload and download
SAE provides multiple methods to allow you to upload and download files. You can select one of the following methods based on your business requirements.
Use the SAE console: You can upload and download files whose size does not exceed 10 MB for debugging.
For more information, see Use the webshell feature to upload and download files.
Use the SAE console: You can perform remote debugging.
Use the persistence storage: File Storage NAS or Object Storage Service (OSS).
When you deploy an application, you can mount NAS or OSS and share file directories.
Upload and download files in NAS: You can use Serverless Devs.
Upload and download files in OSS: You can use the OSS console.
Upload and download files in OSS: You can use ossutil.