Skip to content

Commit a9d324a

Browse files
author
crashdev226
committed
Compile PHP 8.1 from source
1 parent 39f3726 commit a9d324a

File tree

1 file changed

+101
-57
lines changed

1 file changed

+101
-57
lines changed

php-81/cpu-x86.Dockerfile

Lines changed: 101 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,110 @@
11
FROM bref/base-devel-x86 as build-environment
22

3-
# Specifying the exact PHP version lets us avoid the Docker cache when a new version comes out
4-
ENV VERSION_PHP=8.1.12-1
5-
# Check out the latest version available on this page:
6-
# https://rpms.remirepo.net/enterprise/7/php81/x86_64/repoview/php-cli.html
3+
ENV VERSION_PHP=8.1.14
4+
5+
RUN mkdir -p /tmp/php
6+
WORKDIR /tmp/php
7+
8+
# PHP Build
9+
# https://github.com/php/php-src/releases
10+
# Needs:
11+
# - zlib
12+
# - libxml2
13+
# - openssl
14+
# - readline
15+
# - sodium
16+
17+
# Download and unpack the source code
18+
# --location will follow redirects
19+
# --silent will hide the progress, but also the errors: we restore error messages with --show-error
20+
# --fail makes sure that curl returns an error instead of fetching the 404 page
21+
RUN curl --location --silent --show-error --fail https://www.php.net/get/php-${VERSION_PHP}.tar.gz/from/this/mirror \
22+
| tar xzC . --strip-components=1
23+
24+
# Configure the build
25+
# -fstack-protector-strong : Be paranoid about stack overflows
26+
# -fpic : Make PHP's main executable position-independent (improves ASLR security mechanism, and has no performance impact on x86_64)
27+
# -fpie : Support Address Space Layout Randomization (see -fpic)
28+
# -O3 : Optimize for fastest binaries possible.
29+
# -I : Add the path to the list of directories to be searched for header files during preprocessing.
30+
# --enable-option-checking=fatal: make sure invalid --configure-flags are fatal errors instead of just warnings
31+
# --enable-ftp: because ftp_ssl_connect() needs ftp to be compiled statically (see https://github.com/docker-library/php/issues/236)
32+
# --enable-mbstring: because otherwise there's no way to get pecl to use it properly (see https://github.com/docker-library/php/issues/195)
33+
# --with-zlib and --with-zlib-dir: See https://stackoverflow.com/a/42978649/245552
34+
RUN ./buildconf --force
35+
RUN CFLAGS="-fstack-protector-strong -fpic -fpie -O3 -I${INSTALL_DIR}/include -I/usr/include -ffunction-sections -fdata-sections" \
36+
CPPFLAGS="-fstack-protector-strong -fpic -fpie -O3 -I${INSTALL_DIR}/include -I/usr/include -ffunction-sections -fdata-sections" \
37+
LDFLAGS="-L${INSTALL_DIR}/lib64 -L${INSTALL_DIR}/lib -Wl,-O1 -Wl,--strip-all -Wl,--hash-style=both -pie" \
38+
./configure \
39+
--build=x86_64-pc-linux-gnu \
40+
--prefix=${INSTALL_DIR} \
41+
--enable-option-checking=fatal \
42+
--enable-sockets \
43+
--with-config-file-path=/opt/bref/etc/php \
44+
--with-config-file-scan-dir=/opt/bref/etc/php/conf.d:/var/task/php/conf.d \
45+
--enable-fpm \
46+
--disable-cgi \
47+
--enable-cli \
48+
--disable-phpdbg \
49+
--with-sodium \
50+
--with-readline \
51+
--with-openssl \
52+
--with-zlib=${INSTALL_DIR} \
53+
--with-zlib-dir=${INSTALL_DIR} \
54+
--with-curl \
55+
--enable-exif \
56+
--enable-ftp \
57+
--with-gettext \
58+
--enable-mbstring \
59+
--with-pdo-mysql=shared,mysqlnd \
60+
--with-mysqli \
61+
--enable-pcntl \
62+
--with-zip \
63+
--enable-bcmath \
64+
--with-pdo-pgsql=shared,${INSTALL_DIR} \
65+
--enable-intl=shared \
66+
--enable-soap \
67+
--with-xsl=${INSTALL_DIR} \
68+
# necessary for `pecl` to work (to install PHP extensions)
69+
--with-pear
70+
RUN make -j $(nproc)
71+
# Run `make install` and override PEAR's PHAR URL because pear.php.net is down
72+
RUN set -xe; \
73+
make install PEAR_INSTALLER_URL='https://github.com/pear/pearweb_phars/raw/master/install-pear-nozlib.phar'; \
74+
{ find ${INSTALL_DIR}/bin ${INSTALL_DIR}/sbin -type f -perm +0111 -exec strip --strip-all '{}' + || true; }; \
75+
make clean; \
76+
cp php.ini-production ${INSTALL_DIR}/etc/php/php.ini
77+
78+
79+
# Install extensions
80+
# We can install extensions manually or using `pecl`
81+
RUN pecl install APCu
82+
83+
84+
# ---------------------------------------------------------------
85+
# Start from a clean image to copy only the files we need
86+
FROM public.ecr.aws/lambda/provided:al2-x86_64 as isolation
787

88+
RUN mkdir /opt/bin \
89+
&& mkdir /opt/lib \
90+
&& mkdir -p /opt/bref/extensions
891

9-
# Work in a temporary /bref dir to avoid any conflict/mixup with other /opt files
10-
# /bref will eventually be moved to /opt
11-
RUN mkdir /bref \
12-
&& mkdir /bref/bin \
13-
&& mkdir /bref/lib \
14-
&& mkdir -p /bref/bref/extensions
1592

16-
RUN yum-config-manager --enable remi-php81
93+
RUN cp ${INSTALL_DIR}/bin/php /bref/bin/php && chmod +x /bref/bin/php
1794

18-
RUN yum update -y && yum upgrade -y
1995

20-
# --setopt=skip_missing_names_on_install=False makes sure we get an error if a package is missing
21-
RUN yum install --setopt=skip_missing_names_on_install=False -y \
22-
php-cli-${VERSION_PHP}.el7.remi.x86_64
96+
# --------------------------------------------------------
97+
# Now we copy what we need from:
98+
# - /lib | /lib64 (system libraries installed with `yum`)
99+
# - /usr/local/bin | /usr/local/lib | /usr/local/lib64 (libraries compiled from source)
100+
# into `/opt` (the directory of Lambda layers)
101+
#
102+
# HOW?
103+
# `ldd /usr/local/bin/php` will list the libraries a binary or library depends on.
104+
# We use `ldd` and copy all the dependencies.
105+
# BUT some system libraries are native to Amazon Linux 2 (they already exist in Lambda),
106+
# so we don't copy these (the lines will be commented below to show that we know about them).
107+
23108

24109
# These files are included on Amazon Linux 2
25110

@@ -37,7 +122,7 @@ RUN yum install --setopt=skip_missing_names_on_install=False -y \
37122
# RUN cp /lib64/libsmime3.so /bref/lib/libsmime3.so
38123

39124
# PHP Binary
40-
RUN cp /usr/bin/php /bref/bin/php && chmod +x /bref/bin/php
125+
RUN cp ${INSTALL_DIR}/bin/php /bref/bin/php && chmod +x /bref/bin/php
41126
RUN cp /lib64/libtinfo.so.5 /bref/lib/libtinfo.so.5
42127
RUN cp /lib64/libedit.so.0 /bref/lib/libedit.so.0
43128
RUN cp /lib64/libncurses.so.5 /bref/lib/libncurses.so.5
@@ -60,16 +145,6 @@ RUN cp /lib64/libncurses.so.5 /bref/lib/libncurses.so.5
60145
#RUN cp /lib64/libtinfo.so.6 /bref/lib/libtinfo.so.6
61146
#RUN cp /lib64/libpcre.so.1 /bref/lib/libpcre.so.1
62147

63-
# Default Extensions
64-
RUN cp /lib64/php/modules/ctype.so /bref/bref/extensions/ctype.so
65-
RUN cp /lib64/php/modules/exif.so /bref/bref/extensions/exif.so
66-
RUN cp /lib64/php/modules/fileinfo.so /bref/bref/extensions/fileinfo.so
67-
RUN cp /lib64/php/modules/ftp.so /bref/bref/extensions/ftp.so
68-
RUN cp /lib64/php/modules/gettext.so /bref/bref/extensions/gettext.so
69-
RUN cp /lib64/php/modules/iconv.so /bref/bref/extensions/iconv.so
70-
RUN cp /lib64/php/modules/sockets.so /bref/bref/extensions/sockets.so
71-
RUN cp /lib64/php/modules/tokenizer.so /bref/bref/extensions/tokenizer.so
72-
73148
# cURL
74149
RUN cp /lib64/php/modules/curl.so /bref/bref/extensions/curl.so
75150
#RUN cp /lib64/libcurl.so.4 /bref/lib/libcurl.so.4
@@ -83,34 +158,6 @@ RUN cp /lib64/php/modules/curl.so /bref/bref/extensions/curl.so
83158
#RUN cp /lib64/libplc4.so /bref/lib/libplc4.so
84159
#RUN cp /lib64/libnspr4.so /bref/lib/libnspr4.so
85160

86-
RUN yum install -y --setopt=skip_missing_names_on_install=False \
87-
php-mbstring \
88-
php-bcmath \
89-
php-dom \
90-
php-mysqli \
91-
php-mysqlnd \
92-
php-opcache \
93-
php-pdo \
94-
php-pdo_mysql \
95-
php-phar \
96-
php-posix \
97-
php-simplexml \
98-
php-soap \
99-
php-sodium \
100-
php-xml \
101-
php-xmlreader \
102-
php-xmlwriter \
103-
php-xsl \
104-
php-intl \
105-
php-apcu \
106-
php-pdo_pgsql \
107-
php-zip
108-
109-
# Install development tools to compile extra PHP extensions
110-
RUN yum install -y --setopt=skip_missing_names_on_install=False \
111-
php-devel \
112-
php-pear
113-
114161
RUN cp /lib64/php/modules/mbstring.so /bref/bref/extensions/mbstring.so
115162
RUN cp /usr/lib64/libonig.so.105 /bref/lib/libonig.so.105
116163

@@ -162,9 +209,6 @@ RUN cp /lib64/php/modules/xml.so /bref/bref/extensions/xml.so
162209
RUN cp /lib64/php/modules/xmlreader.so /bref/bref/extensions/xmlreader.so
163210
RUN cp /lib64/php/modules/xmlwriter.so /bref/bref/extensions/xmlwriter.so
164211

165-
# Start from a clean image to copy only the files we need
166-
FROM public.ecr.aws/lambda/provided:al2-x86_64 as isolation
167-
168212
COPY --from=build-environment /bref /opt
169213

170214
# This doesn't do anything on Lambda, but is useful when running via Docker (e.g. local dev)

0 commit comments

Comments
 (0)