Skip to content

Commit 527fe34

Browse files
committed
feat: add php 8.1
1 parent f9bd3a2 commit 527fe34

File tree

7 files changed

+282
-5
lines changed

7 files changed

+282
-5
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
strategy:
3232
fail-fast: false
3333
matrix:
34-
php-versions: ['7.2', '7.3', '7.4', '8.0']
34+
php-versions: ['7.2', '7.3', '7.4', '8.0', '8.1']
3535
steps:
3636
- name: Checkout code
3737
uses: actions/checkout@v2

runtime/Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
SHELL := /bin/bash
22
.PHONY: build build-images publish
33

4-
build: build-php-72.zip build-php-73.zip build-php-74.zip build-php-80.zip
4+
build: build-php-72.zip build-php-73.zip build-php-74.zip build-php-80.zip build-php-81.zip
55

66
build-php%.zip: build-images
77
PHP_VERSION=$$(echo $@ | cut -d'.' -f 1 | cut -d'-' -f 2,3); \
@@ -16,10 +16,11 @@ build-images:
1616
cd php-73 ; docker build -t ymir/runtime/php-73 .
1717
cd php-74 ; docker build -t ymir/runtime/php-74 .
1818
cd php-80 ; docker build -t ymir/runtime/php-80 .
19+
cd php-81 ; docker build -t ymir/runtime/php-81 .
1920

20-
publish-images: publish-php-72 publish-php-73 publish-php-74 publish-php-80
21+
publish-images: publish-php-72 publish-php-73 publish-php-74 publish-php-80 publish-php-81
2122

22-
publish-dev-images: publish-dev-php-72 publish-dev-php-73 publish-dev-php-74 publish-dev-php-80
23+
publish-dev-images: publish-dev-php-72 publish-dev-php-73 publish-dev-php-74 publish-dev-php-80 publish-dev-php-81
2324

2425
publish-php%: build-images
2526
PHP_VERSION=$$(echo $@ | cut -d'.' -f 1 | cut -d'-' -f 2,3); \

runtime/layers.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"php-72": "Ymir PHP 7.2 runtime",
33
"php-73": "Ymir PHP 7.3 runtime",
44
"php-74": "Ymir PHP 7.4 runtime",
5-
"php-80": "Ymir PHP 8.0 runtime"
5+
"php-80": "Ymir PHP 8.0 runtime",
6+
"php-81": "Ymir PHP 8.1 runtime"
67
}

runtime/php-81/Dockerfile

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
FROM ymir/runtime/base as php-build
2+
3+
###############################################################################
4+
# Oniguruma
5+
# This library is not packaged in PHP since PHP 7.4.
6+
# See https://github.com/php/php-src/blob/43dc7da8e3719d3e89bd8ec15ebb13f997bbbaa9/UPGRADING#L578-L581
7+
# We do not install the system version because I didn't manage to make it work...
8+
# Ideally we shouldn't compile it ourselves.
9+
# https://github.com/kkos/oniguruma/releases
10+
# Needed by:
11+
# - php mbstring
12+
ENV VERSION_ONIG=6.9.8
13+
ENV ONIG_BUILD_DIR=${BUILD_DIR}/oniguruma
14+
RUN set -xe; \
15+
mkdir -p ${ONIG_BUILD_DIR}; \
16+
curl -Ls https://github.com/kkos/oniguruma/releases/download/v${VERSION_ONIG}/onig-${VERSION_ONIG}.tar.gz \
17+
| tar xzC ${ONIG_BUILD_DIR} --strip-components=1
18+
WORKDIR ${ONIG_BUILD_DIR}/
19+
RUN set -xe; \
20+
./configure --prefix=${INSTALL_DIR}; \
21+
make -j $(nproc); \
22+
make install
23+
24+
25+
ENV VERSION_PHP=8.1.12
26+
27+
28+
ENV PHP_BUILD_DIR=${BUILD_DIR}/php
29+
RUN set -xe; \
30+
mkdir -p ${PHP_BUILD_DIR}; \
31+
# Download and upack the source code
32+
# --location will follow redirects
33+
# --silent will hide the progress, but also the errors: we restore error messages with --show-error
34+
# --fail makes sure that curl returns an error instead of fetching the 404 page
35+
curl --location --silent --show-error --fail https://www.php.net/get/php-${VERSION_PHP}.tar.gz/from/this/mirror \
36+
| tar xzC ${PHP_BUILD_DIR} --strip-components=1
37+
# Move into the unpackaged code directory
38+
WORKDIR ${PHP_BUILD_DIR}/
39+
40+
# Configure the build
41+
# -fstack-protector-strong : Be paranoid about stack overflows
42+
# -fpic : Make PHP's main executable position-independent (improves ASLR security mechanism, and has no performance impact on x86_64)
43+
# -fpie : Support Address Space Layout Randomization (see -fpic)
44+
# -O3 : Optimize for fastest binaries possible.
45+
# -I : Add the path to the list of directories to be searched for header files during preprocessing.
46+
# --enable-option-checking=fatal: make sure invalid --configure-flags are fatal errors instead of just warnings
47+
# --enable-ftp: because ftp_ssl_connect() needs ftp to be compiled statically (see https://github.com/docker-library/php/issues/236)
48+
# --enable-mbstring: because otherwise there's no way to get pecl to use it properly (see https://github.com/docker-library/php/issues/195)
49+
# --with-zlib and --with-zlib-dir: See https://stackoverflow.com/a/42978649/245552
50+
# --with-pear: necessary for `pecl` to work (to install PHP extensions)
51+
#
52+
RUN set -xe \
53+
&& ./buildconf --force \
54+
&& CFLAGS="-fstack-protector-strong -fpic -fpie -O3 -I${INSTALL_DIR}/include -I/usr/include -ffunction-sections -fdata-sections" \
55+
CPPFLAGS="-fstack-protector-strong -fpic -fpie -O3 -I${INSTALL_DIR}/include -I/usr/include -ffunction-sections -fdata-sections" \
56+
LDFLAGS="-L${INSTALL_DIR}/lib64 -L${INSTALL_DIR}/lib -Wl,-O1 -Wl,--strip-all -Wl,--hash-style=both -pie" \
57+
./configure \
58+
--build=x86_64-pc-linux-gnu \
59+
--prefix=${INSTALL_DIR} \
60+
--enable-option-checking=fatal \
61+
--enable-sockets \
62+
--with-config-file-path=${INSTALL_DIR}/etc/php \
63+
--with-config-file-scan-dir=${INSTALL_DIR}/etc/php/conf.d:/var/task/php/conf.d \
64+
--enable-fpm \
65+
--disable-cgi \
66+
--enable-cli \
67+
--disable-phpdbg \
68+
--with-sodium \
69+
--with-readline \
70+
--with-openssl \
71+
--with-zlib=${INSTALL_DIR} \
72+
--with-zlib-dir=${INSTALL_DIR} \
73+
--with-curl \
74+
--enable-exif \
75+
--enable-ftp \
76+
--with-gettext \
77+
--enable-mbstring \
78+
--with-pdo-mysql=shared,mysqlnd \
79+
--with-mysqli \
80+
--enable-pcntl \
81+
--with-zip \
82+
--enable-bcmath \
83+
--enable-intl=shared \
84+
--enable-soap \
85+
--with-xsl=${INSTALL_DIR} \
86+
--with-pear
87+
RUN make -j $(nproc)
88+
# Run `make install` and override PEAR's PHAR URL because pear.php.net is down
89+
RUN set -xe; \
90+
make install PEAR_INSTALLER_URL='https://github.com/pear/pearweb_phars/raw/master/install-pear-nozlib.phar'; \
91+
{ find ${INSTALL_DIR}/bin ${INSTALL_DIR}/sbin -type f -perm +0111 -exec strip --strip-all '{}' + || true; }; \
92+
make clean; \
93+
cp php.ini-production ${INSTALL_DIR}/etc/php/php.ini
94+
95+
# Install extensions using pecl
96+
RUN pecl install APCu
97+
RUN pecl install igbinary
98+
RUN pecl install msgpack
99+
RUN pecl install zstd
100+
101+
# Build extensions
102+
WORKDIR ${IMAGICK_BUILD_DIR}
103+
RUN set -xe; \
104+
pecl download imagick-${VERSION_IMAGICK_EXTENSION}; \
105+
tar xzf imagick-${VERSION_IMAGICK_EXTENSION}.tgz
106+
WORKDIR ${IMAGICK_BUILD_DIR}/imagick-${VERSION_IMAGICK_EXTENSION}
107+
RUN set -xe; \
108+
phpize; \
109+
./configure --with-imagick=${INSTALL_DIR}; \
110+
make -j $(nproc); \
111+
make install;
112+
113+
RUN set -xe; \
114+
mkdir -p ${RELAY_BUILD_DIR}; \
115+
curl -L "https://cachewerk.s3.amazonaws.com/relay/v${VERSION_RELAY_EXTENSION}/relay-v${VERSION_RELAY_EXTENSION}-php8.1-centos7-x86-64.tar.gz" \
116+
| tar xzC ${RELAY_BUILD_DIR} --strip-components=1
117+
WORKDIR ${RELAY_BUILD_DIR}/
118+
RUN cp relay.ini ${INSTALL_DIR}/etc/php/conf.d/50-relay.ini; \
119+
cp relay-pkg.so ${INSTALL_DIR}/lib/php/extensions/no-debug-non-zts-20210902/relay.so; \
120+
sed -i "s/BIN:31415926-5358-9793-2384-626433832795/BIN:$(cat /proc/sys/kernel/random/uuid)/" ${INSTALL_DIR}/lib/php/extensions/no-debug-non-zts-20210902/relay.so;
121+
122+
# Install Composer
123+
RUN curl -sS https://getcomposer.org/installer | ${INSTALL_DIR}/bin/php -- --install-dir=${INSTALL_DIR}/bin/ --filename=composer
124+
125+
# Symlink all our binaries into /opt/bin so that Lambda sees them in the path.
126+
RUN mkdir -p /opt/bin \
127+
&& cd /opt/bin \
128+
&& ln -s ../ymir/bin/* . \
129+
&& ln -s ../ymir/sbin/* .
130+
131+
# Remove extra files to make the layers as slim as possible
132+
COPY clean.sh /tmp
133+
RUN /tmp/clean.sh && rm /tmp/clean.sh
134+
135+
# Copy config files
136+
COPY php.ini ${INSTALL_DIR}/etc/php/conf.d
137+
COPY php-fpm.conf ${INSTALL_DIR}/etc/php-fpm.d
138+
139+
# Build PHP runtime
140+
RUN git clone https://github.com/ymirapp/php-runtime.git /tmp/runtime-build \
141+
&& cd /tmp/runtime-build \
142+
&& git checkout tags/v1.7.0 \
143+
&& cd /opt \
144+
&& cp -R /tmp/runtime-build/composer.json /tmp/runtime-build/composer.lock /tmp/runtime-build/runtime/bootstrap /tmp/runtime-build/runtime/runtime.php /tmp/runtime-build/src /tmp/runtime-build/templates ./ \
145+
&& chmod 0555 /opt/bootstrap /opt/runtime.php \
146+
&& composer install --no-dev
147+
148+
# Now we start back from a clean image.
149+
# We get rid of everything that is unnecessary (build tools, source code, and anything else
150+
# that might have created intermediate layers for docker) by copying online the /opt directory.
151+
FROM public.ecr.aws/lambda/provided:al2
152+
ENV PATH="/opt/bin:${PATH}" \
153+
LD_LIBRARY_PATH="/opt/ymir/lib64:/opt/ymir/lib"
154+
155+
# Copy everything we built above into the same dir on the base AmazonLinux container.
156+
COPY --from=php-build /opt /opt
157+
158+
# Needed for building the layer
159+
COPY --from=php-build /usr/lib64 /usr/lib64

runtime/php-81/clean.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# ------------------------------------------
2+
# This script cleans extra files from /opt
3+
# to keep the layers as small as possible.
4+
# ------------------------------------------
5+
6+
# Stop on error
7+
set -e
8+
# Treat unset variables and parameters as an error.
9+
set -u
10+
11+
# Strip all the unneeded symbols from shared libraries to reduce size.
12+
find /opt/ymir -type f -name "*.so*" -exec strip --strip-unneeded {} \;
13+
find /opt/ymir -type f -name "*.a"|xargs rm
14+
find /opt/ymir -type f -name "*.la"|xargs rm
15+
find /opt/ymir -type f -name "*.dist"|xargs rm
16+
find /opt/ymir -type f -executable -exec sh -c "file -i '{}' | grep -q 'x-executable; charset=binary'" \; -print|xargs strip --strip-all
17+
18+
# Cleanup all the binaries we don't want.
19+
find /opt/ymir/sbin -mindepth 1 -maxdepth 1 ! -name "composer" ! -name "php" ! -name "php-fpm" -exec rm {} \+
20+
find /opt/ymir/bin -mindepth 1 -maxdepth 1 ! -name "composer" ! -name "php" ! -name "php-fpm" -exec rm {} \+
21+
find /opt/bin -mindepth 1 -maxdepth 1 ! -name "composer" ! -name "php" ! -name "php-fpm" -exec rm {} \+
22+
23+
# Cleanup all the files we don't want either
24+
# We do not support running pear functions in Lambda
25+
rm -rf /opt/ymir/lib/php/PEAR
26+
rm -rf /opt/ymir/share
27+
rm -rf /opt/ymir/include
28+
rm -rf /opt/ymir/{lib,lib64}/pkgconfig
29+
rm -rf /opt/ymir/{lib,lib64}/cmake
30+
rm -rf /opt/ymir/lib/xml2Conf.sh
31+
find /opt/ymir/lib/php -mindepth 1 -maxdepth 1 -type d -a ! -name "extensions" -exec rm -rf {} \;
32+
find /opt/ymir/lib/php -mindepth 1 -maxdepth 1 -type f -exec rm -rf {} \;
33+
rm -rf /opt/ymir/lib/php/test
34+
rm -rf /opt/ymir/lib/php/doc
35+
rm -rf /opt/ymir/lib/php/docs
36+
rm -rf /opt/ymir/tests
37+
rm -rf /opt/ymir/doc
38+
rm -rf /opt/ymir/docs
39+
rm -rf /opt/ymir/man
40+
rm -rf /opt/ymir/php
41+
rm -rf /opt/ymir/www
42+
rm -rf /opt/ymir/cfg
43+
rm -rf /opt/ymir/libexec
44+
rm -rf /opt/ymir/var
45+
rm -rf /opt/ymir/data

runtime/php-81/php-fpm.conf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
; Logging anywhere on disk doesn't make sense on lambda since instances are ephemeral
2+
error_log = /dev/null
3+
pid = /tmp/.ymir/php-fpm.pid
4+
; Log above warning because PHP-FPM logs useless notices
5+
; We must comment this flag else uncaught exceptions/fatal errors are not reported in the logs!
6+
; TODO: report that to the PHP bug tracker
7+
;log_level = 'warning'
8+
9+
[default]
10+
pm = static
11+
; We only need one child because a lambda can process only one request at a time
12+
pm.max_children = 1
13+
listen = /tmp/.ymir/php-fpm.sock
14+
; Allows PHP processes to access the lambda's environment variables
15+
clear_env = no
16+
; Forward stderr of PHP processes to stderr of PHP-FPM (so that it can be sent to cloudwatch)
17+
catch_workers_output = yes
18+
; New PHP 7.3 option that disables a verbose log prefix
19+
decorate_workers_output = no
20+
; Limit the number of core dump logs to 1 to avoid filling up the /tmp disk
21+
rlimit_core = 1

runtime/php-81/php.ini

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
; Do not display errors in production because with PHP-FPM that means
2+
; errors will be output in the HTTP response
3+
display_errors=0
4+
5+
; Since PHP 7.4 the default value is E_ALL
6+
; We override it to set the recommended configuration value for production.
7+
; See https://github.com/php/php-src/blob/d91abf76e01a3c39424e8192ad049f473f900936/php.ini-production#L463
8+
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
9+
10+
memory_limit=3008M
11+
12+
opcache.enable=1
13+
opcache.enable_cli=1
14+
15+
; Skip this check to save a bit
16+
opcache.validate_permission=0
17+
18+
; The code is readonly on lambdas so it never changes
19+
opcache.validate_timestamps=0
20+
21+
; Set sane values, modern PHP applications have higher needs than opcache's defaults
22+
; See https://tideways.com/profiler/blog/fine-tune-your-opcache-configuration-to-avoid-caching-suprises
23+
opcache.memory_consumption=128
24+
opcache.max_accelerated_files=10000
25+
opcache.max_wasted_percentage=10
26+
27+
extension=apcu.so
28+
extension=igbinary.so
29+
extension=imagick.so
30+
extension=intl.so
31+
extension=msgpack.so
32+
extension=pdo_mysql.so
33+
extension=zstd.so
34+
zend_extension=opcache.so
35+
36+
; Disable the header "X-Powered-By" exposing the installed PHP version
37+
expose_php=0
38+
39+
; This directive determines which super global arrays are registered when PHP
40+
; starts up. G,P,C,E & S are abbreviations for the following respective super
41+
; globals: GET, POST, COOKIE, ENV and SERVER.
42+
; We explicitly populate all variables else ENV is not populated by default.
43+
variables_order="EGPCS"
44+
45+
; The lambda environment is not compatible with fastcgi_finish_request
46+
disable_functions=fastcgi_finish_request
47+
48+
; API Gateway has a timeout of 29 seconds. Setting this to 28 will give PHP some
49+
; time to properly finish up its resources and flush logs to CloudWatch.
50+
max_execution_time=28

0 commit comments

Comments
 (0)