Compilation

De Wiki de Jordan LE NUFF
< Technique‎ | PHP
Sauter à la navigation Sauter à la recherche
 
(Une révision intermédiaire par le même utilisateur non affichée)
Ligne 638 : Ligne 638 :
 
=== make ===
 
=== make ===
 
La configuration de la compilation étant précédemment réalisée, lancer simplement la compilation avec la commande suivante :
 
La configuration de la compilation étant précédemment réalisée, lancer simplement la compilation avec la commande suivante :
  make
+
  make -j
 +
 
 +
L'option <code>-j</code> permet de lancer le maximum de jobs possible simultanément.
  
 
<u>Exemple de retour de compilation réussie :</u>
 
<u>Exemple de retour de compilation réussie :</u>
Ligne 809 : Ligne 811 :
 
Scan for additional .ini files in: (none)
 
Scan for additional .ini files in: (none)
 
Additional .ini files parsed:      (none)
 
Additional .ini files parsed:      (none)
 +
</pre>
 +
 +
==Partie en cours de rédaction==
 +
<pre>
 +
export PHP_VERSION=7.4.16
 +
mkdir -p /opt/php/php-${PHP_VERSION}
 +
wget https://www.php.net/distributions/php-${PHP_VERSION}.tar.gz
 +
tar -zxf php-${PHP_VERSION}.tar.gz -C /data/builds
 +
cd /data/builds/php-${PHP_VERSION}
 +
./configure --prefix=/opt/php/php-${PHP_VERSION} --with-config-file-path=/opt/php/php-${PHP_VERSION} --disable-all --enable-static --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-fpm-systemd --disable-ipv6 --enable-cli --with-zlib --with-bz2 --enable-calendar --enable-ctype --with-curl --enable-gd --enable-intl --with-ldap --enable-mbstring --enable-pdo --with-pdo-mysql --enable-shared=pdo-mysql --enable-phar --with-libxml --with-iconv --enable-dom --enable-filter --enable-tokenizer --enable-json --enable-session --enable-xml --enable-xmlreader --enable-xmlwriter --enable-opcache --enable-fileinfo --enable-simplexml --enable-soap --enable-ftp --with-zip --with-pear --with-openssl --with-libdir=lib64 --with-mysqli && make -j && make install
 +
cp /opt/php/php-${PHP_VERSION}/etc/php-fpm.conf.default /opt/php/php-${PHP_VERSION}/etc/php-fpm.conf
 +
sed -e 's@;pid = run/php-fpm.pid@;pid = run/php-fpm.pid\npid = run/php-fpm.pid@g' /opt/php/php-${PHP_VERSION}/etc/php-fpm.conf
 +
cp /data/builds/php-${PHP_VERSION}/php.ini-production /opt/php/php-${PHP_VERSION}/php.ini
 +
sed -i -e 's@;error_log = syslog@;error_log = syslog\nerror_log = /data/logs/localhost/localhost_php-${PHP_VERSION}.log@g' /opt/php/php-${PHP_VERSION}/php.ini
 +
sed -i -e 's@;sendmail_path =@sendmail_path = "/usr/bin/msmtp -t"@g' /opt/php/php-${PHP_VERSION}/php.ini
 +
sed -i -e 's@;date.timezone =@date.timezone = "Europe/Paris"@g' /opt/php/php-${PHP_VERSION}/php.ini
 +
sed -i -e 's@\[opcache\]@\[opcache\]\nzend_extension=$(grep no-debug-non-zts /opt/php/php-${PHP_VERSION}/bin/php-config|awk -F\' '{print $2}')/opcache.so@g' /opt/php/php-${PHP_VERSION}/php.ini
 +
sed -i -e 's@\[opcache\]@\[opcache\]\nzend_extension='$(grep no-debug-non-zts /opt/php/php-${PHP_VERSION}/bin/php-config|awk -F\' '{print $2}')'/opcache.so@g' /opt/php/php-${PHP_VERSION}/php.ini
 +
/opt/php/php-${PHP_VERSION}/bin/pear config-set php_ini /opt/php/php-${PHP_VERSION}/php.ini system
 +
/opt/php/php-${PHP_VERSION}/bin/pecl config-set php_ini /opt/php/php-${PHP_VERSION}/php.ini system
 +
cat <<EOF >/opt/php/php-${PHP_VERSION}/etc/php-fpm.d/localhost.conf
 +
[localhost]
 +
listen = /opt/php/sockets/php-${PHP_VERSION}_\$pool.sock
 +
listen.owner = php-fpm
 +
listen.group = www
 +
listen.mode = 0660
 +
pm = dynamic
 +
pm.max_children = 5
 +
pm.start_servers = 2
 +
pm.min_spare_servers = 1
 +
pm.max_spare_servers = 3
 +
pm.status_path = /php-fpm-status
 +
ping.path = /php-fpm-ping
 +
access.log = /data/logs/\$pool/\$pool_php-${PHP_VERSION}.access.log
 +
slowlog = /data/logs/\$pool/\$pool_php-${PHP_VERSION}.log.slow
 +
php_admin_value[error_log] = /data/logs/\$pool/\$pool_php-${PHP_VERSION}.log
 +
php_admin_flag[log_errors] = on
 +
php_admin_value[error_reporting] = E_ALL
 +
php_admin_value[session.save_path] = "/opt/php/sessions/\$pool/"
 +
php_value[session.save_path] = "/opt/php/\$pool/"
 +
EOF
 +
mkdir -p /opt/php/sockets
 +
mkdir -p /opt/php/sessions
 +
chown -R php-fpm:www /opt/php
 +
cp /data/builds/php-${PHP_VERSION}/sapi/fpm/php-fpm.service /usr/lib/systemd/system/php-fpm-${PHP_VERSION}.service
 +
sed -i -e 's/^ProtectKernelModules/#ProtectKernelModules/g' /usr/lib/systemd/system/php-fpm-${PHP_VERSION}.service
 +
sed -i -e 's/^ProtectKernelTunables/#ProtectKernelTunables/g' /usr/lib/systemd/system/php-fpm-${PHP_VERSION}.service
 +
sed -i -e 's/^ProtectControlGroups/#ProtectControlGroups/g' /usr/lib/systemd/system/php-fpm-${PHP_VERSION}.service
 +
sed -i -e 's/^RestrictRealtime/#RestrictRealtime/g' /usr/lib/systemd/system/php-fpm-${PHP_VERSION}.service
 +
sed -i -e 's/^RestrictNamespaces/#RestrictNamespaces/g' /usr/lib/systemd/system/php-fpm-${PHP_VERSION}.service
 +
chmod o+r /usr/lib/systemd/system/php-fpm-${PHP_VERSION}.service
 +
systemctl daemon-reload
 +
systemctl start php-fpm-${PHP_VERSION}.service
 +
sed -i -e 's/\[Service\]/\[Service\]\nUMask=0007\nUser=php-fpm\nGroup=www\nEnvironment="PHP_VERSION='${PHP_VERSION}'"/g' /usr/lib/systemd/system/php-fpm-${PHP_VERSION}.service
 +
systemctl daemon-reload
 +
systemctl start php-fpm-${PHP_VERSION}.service
 
</pre>
 
</pre>

Version actuelle datée du 27 avril 2021 à 16:29

Présentation

Cette page a pour objet de décrire comment compiler PHP depuis les sources.

Passage de PHP 5 à PHP 7

Extensions disparues

mssql

Selon la documentation officielle de l'extension mssql pour PHP, cette extension n'est plus disponible depuis la version 7.0.0.

L'alternative de cette extension sur un serveur Linux est détaillée dans la documentation des fonctions ODBC et DB2 (PDO_ODBC).

regex

Selon la documentation officielle de l'extension regex pour PHP, cette extension n'est plus disponible depuis la version 7.0.0.

L'alternative de cette extension est l'installation de l'extension PCRE. Elle est activée par défaut.

mcrypt

Selon la documentation officielle de l'extension mcrypt pour PHP, cette extension n'est plus disponible depuis la version 7.2.0.

L'alternative de cette extension est l'installation de l'extension OpenSSL.

Recompilation

Se rendre dans le dossier de la dernière compilation, par exemple /local/builds/php-7.3.4, et faire un nettoyage préalable à la compilation :

make mostlyclean
make clean
make distclean
make maintainer-clean

Lancer ensuite la compilation normalement.

Compilation dernière version de PHP 5

Téléchargement de sources

La dernière version officielle de PHP 5 est la 5.6.40 du 10 janvier 2019. Il n'y a donc plus de support sur la version 5 depuis cette date.

Créer le dossier d'installation cible :

mkdir /local/php/5.6.40

Créer un lien symbolique pour pointer vers la version 5.6.40 :

ln -s /local/php/5.6.40 /local/php/php5

Créer le dossier de compilation, si inexistant :

mkdir /local/builds

Créer le dossier de téléchargement, si inexistant :

mkdir ~/downloads

Télécharger la version 5.6.40 avec la commande suivante :

wget -O ~/downloads/php-5.6.40.tar.gz https://www.php.net/distributions/php-5.6.40.tar.gz

La dézipper :

tar -xzf ~/downloads/php-5.6.40.tar.gz -C /local/builds/

Se rendre dans le dossier des sources et commencer la configuration de la compilation :

cd /local/builds/php-5.6.40

configure

Lancer la configuration de la compilation avec les options désirées :

./configure \
--prefix=/local/php/php5 \
--with-ldap \
--enable-inline-optimization \
--enable-debug=no \
--enable-calendar \
--enable-ftp \
--enable-sysvsem \
--enable-sysvshm \
--disable-static \
--with-mysql \
--with-mssql=/usr/local/freetds \
--with-gettext \
--with-regex=system \
--with-oci8=instantclient,/usr/lib/oracle/10.2.0.4/client64/lib \
--enable-soap \
--with-mcrypt=/usr/lib \
--with-gd \
--enable-zip \
--with-bz2 \
--with-zlib-dir \
--with-mysqli \
--with-libxml-dir=/usr/lib \
--with-jpeg-dir=/usr/lib \
--with-apxs2=/local/www/2.4.12/bin/apxs \
--with-pdo-oci \
--with-pdo-mysql \
--enable-mbstring \
--with-curl \
--with-libdir=lib64 \
--with-openssl

Exemple de retour sans erreur :

Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

Traitement des erreurs

Lors du configure, des erreurs peuvent être détectées. Le script s'arrête alors avec un message.

Exemple de message d'erreur :

checking for the location of libXpm... no
checking for FreeType 2... yes
checking whether to enable JIS-mapped Japanese font support in GD... no
If configure fails try --with-webp-dir=<DIR>
checking for jpeg_read_header in -ljpeg... yes
checking for png_write_image in -lpng... yes
If configure fails try --with-xpm-dir=<DIR>
configure: error: freetype-config not found.

L'erreur ci-dessus porte sur le sous-module freetype du module gd de PHP. En effet, lançant la commande suivante :

./configure --help

l'aide à la configuration de la compilation indique que l'option --with-freetype-dir attend un répertoire comme valeur :

...
  --with-gd=DIR           Include GD support.  DIR is the GD library base
                          install directory BUNDLED
  --with-webp-dir=DIR     GD: Set the path to libwebp install prefix
  --with-jpeg-dir=DIR     GD: Set the path to libjpeg install prefix
  --with-png-dir=DIR      GD: Set the path to libpng install prefix
  --with-zlib-dir=DIR     GD: Set the path to libz install prefix
  --with-xpm-dir=DIR      GD: Set the path to libXpm install prefix
  --with-freetype-dir=DIR GD: Set the path to FreeType 2 install prefix
  --enable-gd-jis-conv    GD: Enable JIS-mapped Japanese font support
...

Ce genre d'erreur est fréquente lors de la configuration de la compilation. Cela signifie qu'il manque des paquets nécessaire à la compilation. En l'occurrence, pour une une utilisation normale de freetype sur le serveur, seul le paquet freetype est nécessaire. Toutefois, en cas de compilation, il faut disposer des en-tête de développement en installant le paquet freetype-devel. :

yum install freetype-devel

La plupart de ces erreurs se traitent comme ceci : installer le paquet <module>-dev ou <module>-devel, où <module> correspond au module ayant généré une erreur.

Une fois l'installation réalisée, relancer la configuration de la compilation.

make

La configuration de la compilation étant précédemment réalisée, lancer simplement la compilation avec la commande suivante :

make

Exemple de retour de compilation réussie :

Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
clicommand.inc
pharcommand.inc
directorytreeiterator.inc
directorygraphiterator.inc
invertedregexiterator.inc
phar.inc

Build complete.
Don't forget to run 'make test'.

En cas d'erreur

Autant les erreurs de configuration de compilation sont facilement résolvables, autant les erreurs liées à l'étape de compilation sont particulières. Il faudra traiter au cas par cas et faire preuve d'un bon esprit d'analyse.

Par ailleurs, en cas d'erreur rencontrée lors de la compilation, il est recommandé de nettoyer le dossier des sources avant de relancer une configuration suivie d'une compilation avec la commande suivante :

make mostlyclean
make clean
make distclean
make maintainer-clean

make install

Une fois la compilation terminée, les fichiers résultants se trouvent dans le dossier de compilation. Pour installer les fichiers dans les différents dossiers nécessaires à l'utilisation de PHP, lancer la commande suivante :

make install

Exemple de retour d'installation sans erreur :

Installing PHP SAPI module:       apache2handler
/local/www/2.4.12/build/instdso.sh SH_LIBTOOL='/usr/lib64/apr-1/build/libtool' libphp7.la /local/www/2.4.12/modules
/usr/lib64/apr-1/build/libtool --mode=install install libphp7.la /local/www/2.4.12/modules/
libtool: install: install .libs/libphp7.so /local/www/2.4.12/modules/libphp7.so
libtool: install: install .libs/libphp7.lai /local/www/2.4.12/modules/libphp7.la
libtool: install: warning: remember to run `libtool --finish /local/builds/php-7.2.19/libs'
chmod 755 /local/www/2.4.12/modules/libphp7.so
[activating module `php7' in /local/www/2.4.12/conf/httpd.conf]
Installing shared extensions:     /local/php/php7/lib64/extensions/no-debug-zts-20170718/
Installing PHP CLI binary:        /local/php/php7/bin/
Installing PHP CLI man page:      /local/php/php7/php/man/man1/
Installing phpdbg binary:         /local/php/php7/bin/
Installing phpdbg man page:       /local/php/php7/php/man/man1/
Installing PHP CGI binary:        /local/php/php7/bin/
Installing PHP CGI man page:      /local/php/php7/php/man/man1/
Installing build environment:     /local/php/php7/lib64/build/
Installing header files:          /local/php/php7/include/php/
Installing helper programs:       /local/php/php7/bin/
  program: phpize
  program: php-config
Installing man pages:             /local/php/php7/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /local/php/php7/lib64/php/
[PEAR] Archive_Tar    - installed: 1.4.7
[PEAR] Console_Getopt - installed: 1.4.2
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.4.3
[PEAR] PEAR           - installed: 1.10.9
Wrote PEAR system config file at: /local/php/php7/etc/pear.conf
You may want to add: /local/php/php7/lib64/php to your php.ini include_path
/local/builds/php-7.2.19/build/shtool install -c ext/phar/phar.phar /local/php/php7/bin
ln -s -f phar.phar /local/php/php7/bin/phar
Installing PDO headers:           /local/php/php7/include/php/ext/pdo/

Il n'y a généralement pas d'erreur lors de cette étape.

Compilation PHP 7

Il faut adapter cette procédure en fonction de la version de PHP 7 désirée.

Dans cette procédure, c'est la version 7.2.19 qui est utilisée pour la démonstration.

Téléchargement de sources

Créer le dossier d'installation cible :

mkdir /local/php/7.2.19

Créer un lien symbolique pour pointer vers la version 5.6.40 :

ln -s /local/php/7.2.19 /local/php/php7

Créer le dossier de compilation, si inexistant :

mkdir /local/builds

Créer le dossier de téléchargement, si inexistant :

mkdir ~/downloads

Télécharger la version 5.6.40 avec la commande suivante :

wget -O ~/downloads/php-7.2.19.tar.gz https://www.php.net/distributions/php-7.2.19.tar.gz

La dézipper :

tar -xzf ~/downloads/php-7.2.19.tar.gz -C /local/builds/

Se rendre dans le dossier des sources et commencer la configuration de la compilation :

cd /local/builds/php-7.2.19

configure

Lancer la configuration de la compilation avec les options désirées :

./configure \
--prefix=/local/php/php7 \
--with-ldap \
--enable-inline-optimization \
--enable-debug=no \
--enable-calendar \
--enable-ftp \
--enable-sysvsem \
--enable-sysvshm \
--disable-static \
--enable-mysqlnd \
--with-gettext \
--with-pcre-regex \
--with-oci8=instantclient,/usr/lib/oracle/10.2.0.4/client64/lib \
--enable-soap \
--with-gd \
--enable-zip \
--with-bz2 \
--with-zlib-dir \
--with-mysqli \
--with-libxml-dir=/usr/lib \
--with-jpeg-dir=/usr/lib \
--with-apxs2=/local/www/2.4.12/bin/apxs \
--with-pdo-oci \
--with-pdo-mysql \
--enable-mbstring \
--with-curl \
--with-libdir=lib64 \
--with-openssl

Exemple de retour sans erreur :

Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

config.status: creating php7.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/phpdbg/phpdbg.1
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

Traitement des erreurs

Lors du configure, des erreurs peuvent être détectées. Le script s'arrête alors avec un message.

Exemple de message d'erreur :

checking for the location of libXpm... no
checking for FreeType 2... yes
checking whether to enable JIS-mapped Japanese font support in GD... no
If configure fails try --with-webp-dir=<DIR>
checking for jpeg_read_header in -ljpeg... yes
checking for png_write_image in -lpng... yes
If configure fails try --with-xpm-dir=<DIR>
configure: error: freetype-config not found.

L'erreur ci-dessus porte sur le sous-module freetype du module gd de PHP. En effet, lançant la commande suivante :

./configure --help

l'aide à la configuration de la compilation indique que l'option --with-freetype-dir attend un répertoire comme valeur :

...
  --with-gd=DIR           Include GD support.  DIR is the GD library base
                          install directory BUNDLED
  --with-webp-dir=DIR     GD: Set the path to libwebp install prefix
  --with-jpeg-dir=DIR     GD: Set the path to libjpeg install prefix
  --with-png-dir=DIR      GD: Set the path to libpng install prefix
  --with-zlib-dir=DIR     GD: Set the path to libz install prefix
  --with-xpm-dir=DIR      GD: Set the path to libXpm install prefix
  --with-freetype-dir=DIR GD: Set the path to FreeType 2 install prefix
  --enable-gd-jis-conv    GD: Enable JIS-mapped Japanese font support
...

Ce genre d'erreur est fréquente lors de la configuration de la compilation. Cela signifie qu'il manque des paquets nécessaire à la compilation. En l'occurrence, pour une une utilisation normale de freetype sur le serveur, seul le paquet freetype est nécessaire. Toutefois, en cas de compilation, il faut disposer des en-tête de développement en installant le paquet freetype-devel. :

yum install freetype-devel

La plupart de ces erreurs se traitent comme ceci : installer le paquet <module>-dev ou <module>-devel, où <module> correspond au module ayant généré une erreur.

Une fois l'installation réalisée, relancer la configuration de la compilation.

make

La configuration de la compilation étant précédemment réalisée, lancer simplement la compilation avec la commande suivante :

make

Exemple de retour de compilation réussie :

Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
clicommand.inc
pharcommand.inc
directorytreeiterator.inc
directorygraphiterator.inc
invertedregexiterator.inc
phar.inc

Build complete.
Don't forget to run 'make test'.

En cas d'erreur

Autant les erreurs de configuration de compilation sont facilement résolvables, autant les erreurs liées à l'étape de compilation sont particulières. Il faudra traiter au cas par cas et faire preuve d'un bon esprit d'analyse.

Par ailleurs, en cas d'erreur rencontrée lors de la compilation, il est recommandé de nettoyer le dossier des sources avant de relancer une configuration suivie d'une compilation avec la commande suivante :

make mostlyclean
make clean
make distclean
make maintainer-clean

make install

Une fois la compilation terminée, les fichiers résultants se trouvent dans le dossier de compilation. Pour installer les fichiers dans les différents dossiers nécessaires à l'utilisation de PHP, lancer la commande suivante :

make install

Exemple de retour d'installation sans erreur :

Installing PHP SAPI module:       apache2handler
/local/www/2.4.12/build/instdso.sh SH_LIBTOOL='/usr/lib64/apr-1/build/libtool' libphp5.la /local/www/2.4.12/modules
/usr/lib64/apr-1/build/libtool --mode=install install libphp5.la /local/www/2.4.12/modules/
libtool: install: install .libs/libphp5.so /local/www/2.4.12/modules/libphp5.so
libtool: install: install .libs/libphp5.lai /local/www/2.4.12/modules/libphp5.la
libtool: install: warning: remember to run `libtool --finish /local/builds/php-5.6.40/libs'
chmod 755 /local/www/2.4.12/modules/libphp5.so
[activating module `php5' in /local/www/2.4.12/conf/httpd.conf]
Installing shared extensions:     /local/php/php5/lib64/extensions/no-debug-zts-20131226/
Installing PHP CLI binary:        /local/php/php5/bin/
Installing PHP CLI man page:      /local/php/php5/php/man/man1/
Installing PHP CGI binary:        /local/php/php5/bin/
Installing PHP CGI man page:      /local/php/php5/php/man/man1/
Installing build environment:     /local/php/php5/lib64/build/
Installing header files:           /local/php/php5/include/php/
Installing helper programs:       /local/php/php5/bin/
  program: phpize
  program: php-config
Installing man pages:             /local/php/php5/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /local/php/php5/lib64/php/
[PEAR] Archive_Tar    - installed: 1.4.4
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.4.3
[PEAR] PEAR           - installed: 1.10.7
Wrote PEAR system config file at: /local/php/php5/etc/pear.conf
You may want to add: /local/php/php5/lib64/php to your php.ini include_path
/local/builds/php-5.6.40/build/shtool install -c ext/phar/phar.phar /local/php/php5/bin
ln -s -f phar.phar /local/php/php5/bin/phar
Installing PDO headers:           /local/php/php5/include/php/ext/pdo/

Il n'y a généralement pas d'erreur lors de cette étape.

Compilation PHP 7.4.7

Téléchargement de sources

Créer le dossier d'installation cible :

mkdir -p /local/php/php-7.4.7

Créer un lien symbolique pour pointer vers la version 7.4.7 :

ln -s php-7.4.7 /local/php/current

Créer le dossier de compilation, si inexistant :

mkdir -p /local/builds

Créer le dossier de téléchargement, si inexistant :

mkdir -p ~/downloads

Télécharger la version 5.6.40 avec la commande suivante :

wget -O ~/downloads/php-7.4.7.tar.gz https://www.php.net/distributions/php-7.4.7.tar.gz

La dézipper :

tar -xzf ~/downloads/php-7.4.7.tar.gz -C /local/builds/

Se rendre dans le dossier des sources et commencer la configuration de la compilation :

cd /local/builds/php-7.4.7

Prérequis généraux

Au regard des options utilisées dans la section configure ci-dessous, les paquets suivants sont nécessaires :

yum install zlib-devel
yum install bzip2-devel
yum install libcurl-devel
yum install libpng-devel
yum install libicu-devel libicu
yum install gcc-c++ libstdc++-devel
yum install openldap-devel cyrus-sasl cyrus-sasl-devel
yum install oniguruma-devel oniguruma
yum install libxml2-devel xz-devel

Prérequis spécifique

Un des grands changement du passage de PHP 7.3 vers 7.4 est la migration d'un grand nombre d'extensions vers pkg-config.

De ce fait, comme indiqué dans la documentation PHP, pour chaque extension, il faudra spécifier les chemins de bibliothèques personnalisés soit en ajoutant des dossiers supplémentaires à code>PKG_CONFIG_PATH soit en spécifiant les options de compilations explicitement à travers FOO_CFLAGS et FOO_LIBS.

Sans configuration supplémentaire de pkg-config, pour connaître les chemins par défaut dans lesquels pkg-config cherche les bibliothèques, lancer la commande suivante :

pkg-config --variable pc_path pkg-config

Exemple de retour :

Putty icon.png Console SSH

Ainsi, pour que la compilation de PHP puisse s'appuyer sur les bibliothèques nécessaires, il faudra, au choix :

  • installer les bibliothèques dans les chemins de recherche par défaut de pkg-config
  • ajouter les chemins de recherche supplémentaires en définissant la variable PKG_CONFIG_PATH
  • spécifier des options de compilation particulières de la forme FOO_CFLAGS et FOO_LIBS

Extension zip

Depuis la version 7.4 de PHP, la bibliothèque libzip intégré a été supprimée. Une libzip >= 0.11 système est désormais requise pour compiler l'extension zip.

Certains OS, comme CentOS 7, ne dispose pas d'une version de libzip supérieure ou égale à 0.11 (version 10.1 disponible). Il faudra donc installer manuellement libzip sur le serveur.

L'installation de libzip nécessite l'utilisation de cmake. Se référer à l'installation de CMake pour plus d'informations.

Une fois le prérequis de la présence de CMake respecté, télécharger la bibliothèque libzip :

cd
wget https://libzip.org/download/libzip-1.7.1.tar.gz

Décompresser les sources de libzip :

tar -zxf libzip-1.7.1.tar.gz -C /local/builds

Créer un sous-dossier de build :

mkdir /local/builds/libzip-1.7.1/build

Se rendre dans le sous-dossier de build et lancer la configuration de la compilation avec CMake en spécifiant le path d'installation CMAKE_INSTALL_PREFIX :

cd /local/builds/libzip-1.7.1/build
cmake -DCMAKE_INSTALL_PREFIX=/usr ..

Définir l'option CMAKE_INSTALL_PREFIX à /usr permet d'installer libzip dans les dossiers /usr/lib64 et /usr/share et, par conséquent, permet de créer un fichier libzip.pc dans le dossier /usr/lib64/pkgconfig (qui est un des chemins de recherche par défaut de pkg-config).

Lancer la compilation avec :

make

Lancer l'installation avec :

make install

La bibliothèque libzip est désormais installée sur le serveur et sera automatiquement chargée (grâce à pkg-config) lors de la compilation de PHP.

configure

Lancer la configuration de la compilation avec les options désirées :

./configure \
--prefix=/local/php/php-7.4.7 \
--with-config-file-path=/local/php/php-7.4.7 \
--disable-all \
--enable-static \
--enable-fpm \
--with-fpm-user=php-fpm \
--with-fpm-group=php-fpm \
--with-fpm-systemd \
--disable-ipv6 \
--enable-cli \
--with-zlib \
--with-bz2 \
--enable-calendar \
--enable-ctype \
--with-curl \
--enable-gd \
--enable-intl \
--disable-json \
--with-ldap \
--enable-mbstring \
--enable-pdo \
--with-pdo-mysql \
--enable-shared=pdo-mysql \
--enable-phar \
--with-libxml \
--enable-xml \
--enable-xmlreader \
--enable-xmlwriter \
--enable-opcache \
--with-zip \
--with-pear \
--with-libdir=lib64

Exemple de retour sans erreur :

Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

config.status: creating php7.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/phpdbg/phpdbg.1
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

Traitement des erreurs

Lors du configure, des erreurs peuvent être détectées. Le script s'arrête alors avec un message.

Exemple de message d'erreur :

checking for the location of libXpm... no
checking for FreeType 2... yes
checking whether to enable JIS-mapped Japanese font support in GD... no
If configure fails try --with-webp-dir=<DIR>
checking for jpeg_read_header in -ljpeg... yes
checking for png_write_image in -lpng... yes
If configure fails try --with-xpm-dir=<DIR>
configure: error: freetype-config not found.

L'erreur ci-dessus porte sur le sous-module freetype du module gd de PHP. En effet, lançant la commande suivante :

./configure --help

l'aide à la configuration de la compilation indique que l'option --with-freetype-dir attend un répertoire comme valeur :

...
  --with-gd=DIR           Include GD support.  DIR is the GD library base
                          install directory BUNDLED
  --with-webp-dir=DIR     GD: Set the path to libwebp install prefix
  --with-jpeg-dir=DIR     GD: Set the path to libjpeg install prefix
  --with-png-dir=DIR      GD: Set the path to libpng install prefix
  --with-zlib-dir=DIR     GD: Set the path to libz install prefix
  --with-xpm-dir=DIR      GD: Set the path to libXpm install prefix
  --with-freetype-dir=DIR GD: Set the path to FreeType 2 install prefix
  --enable-gd-jis-conv    GD: Enable JIS-mapped Japanese font support
...

Ce genre d'erreur est fréquente lors de la configuration de la compilation. Cela signifie qu'il manque des paquets nécessaire à la compilation. En l'occurrence, pour une une utilisation normale de freetype sur le serveur, seul le paquet freetype est nécessaire. Toutefois, en cas de compilation, il faut disposer des en-tête de développement en installant le paquet freetype-devel. :

yum install freetype-devel

La plupart de ces erreurs se traitent comme ceci : installer le paquet <module>-dev ou <module>-devel, où <module> correspond au module ayant généré une erreur.

Une fois l'installation réalisée, relancer la configuration de la compilation.

make

La configuration de la compilation étant précédemment réalisée, lancer simplement la compilation avec la commande suivante :

make -j

L'option -j permet de lancer le maximum de jobs possible simultanément.

Exemple de retour de compilation réussie :

Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
clicommand.inc
pharcommand.inc
directorytreeiterator.inc
directorygraphiterator.inc
invertedregexiterator.inc
phar.inc

Build complete.
Don't forget to run 'make test'.

En cas d'erreur

Autant les erreurs de configuration de compilation sont facilement résolvables, autant les erreurs liées à l'étape de compilation sont particulières. Il faudra traiter au cas par cas et faire preuve d'un bon esprit d'analyse.

Par ailleurs, en cas d'erreur rencontrée lors de la compilation, il est recommandé de nettoyer le dossier des sources avant de relancer une configuration suivie d'une compilation avec la commande suivante :

make mostlyclean
make clean
make distclean
make maintainer-clean

make install

Une fois la compilation terminée, les fichiers résultants se trouvent dans le dossier de compilation. Pour installer les fichiers dans les différents dossiers nécessaires à l'utilisation de PHP, lancer la commande suivante :

make install

Exemple de retour d'installation sans erreur :

/bin/sh /local/builds/php-7.4.7/libtool --silent --preserve-dup-deps --mode=install cp ext/opcache/opcache.la /local/builds/php-7.4.7/modules
Installing shared extensions:     /local/php/php-7.4.7/lib/php/extensions/no-debug-non-zts-20190902/
Installing PHP CLI binary:        /local/php/php-7.4.7/bin/
Installing PHP CLI man page:      /local/php/php-7.4.7/php/man/man1/
Installing PHP FPM binary:        /local/php/php-7.4.7/sbin/
Installing PHP FPM defconfig:     /local/php/php-7.4.7/etc/
Installing PHP FPM man page:      /local/php/php-7.4.7/php/man/man8/
Installing PHP FPM status page:   /local/php/php-7.4.7/php/php/fpm/
Installing phpdbg binary:         /local/php/php-7.4.7/bin/
Installing phpdbg man page:       /local/php/php-7.4.7/php/man/man1/
Installing PHP CGI binary:        /local/php/php-7.4.7/bin/
Installing PHP CGI man page:      /local/php/php-7.4.7/php/man/man1/
Installing build environment:     /local/php/php-7.4.7/lib/php/build/
Installing header files:          /local/php/php-7.4.7/include/php/
Installing helper programs:       /local/php/php-7.4.7/bin/
  program: phpize
  program: php-config
Installing man pages:             /local/php/php-7.4.7/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /local/php/php-7.4.7/lib/php/
[PEAR] Archive_Tar    - already installed: 1.4.9
[PEAR] Console_Getopt - already installed: 1.4.3
[PEAR] Structures_Graph- already installed: 1.1.1
[PEAR] XML_Util       - already installed: 1.4.5
[PEAR] PEAR           - already installed: 1.10.12
Wrote PEAR system config file at: /local/php/php-7.4.7/etc/pear.conf
You may want to add: /local/php/php-7.4.7/lib/php to your php.ini include_path
/local/builds/php-7.4.7/build/shtool install -c ext/phar/phar.phar /local/php/php-7.4.7/bin
ln -s -f phar.phar /local/php/php-7.4.7/bin/phar
Installing PDO headers:           /local/php/php-7.4.7/include/php/ext/pdo/

Il n'y a généralement pas d'erreur lors de cette étape.

Intégration des binaires PHP dans le PATH

Version unique de PHP

Dans le cas d'une version unique de PHP installée sur le serveur, il est possible d'intégrer l'ensemble des binaires PHP disponibles dans le PATH en ajoutant la ligne suivantes dans un des fichiers profile du serveur :

export PATH=$PATH:/local/php/5.6.40/bin

Le chemin /local/php/5.6.6/bin est un exemple, l'adapter au besoin.

La modification du PATH se fera automatiquement à chaque ouverture de session. Pour une prise en compte immédiate, il faut sourcer le fichier. Par exemple, si la modification du PATH a été indiquée dans le fichier /etc/profile.d/php.sh, le sourcer en faisant :

. /etc/profile.d/php.sh

De cette façon, les commandes suivantes seront toutes disponibles depuis n'importe quel endroit du serveur, avec n'importe quel utilisateur :

  • pear
  • peardev
  • pecl
  • phar
  • phar.phar
  • php
  • php-cgi
  • php-config
  • phpize

Pour n'inclure que certains des binaires ci-dessus, se reporter à la section Versions multiples de PHP.

Versions multiples de PHP

Dans le cas de plusieurs versions de PHP installées sur le serveur, les binaires portant exactement le même nom, il faudra les différencier selon la version désirée.

Ainsi, pour chaque binaire désiré dans le PATH, il faudra créer un lien symbolique depuis un PATH existant du serveur vers le binaire en question.

Versions majeures

S'il n'y a que deux versions majeures différentes sur le serveur, la création des liens symboliques suivants dans le PATH /usr/bin avec le numéro de version majeure est suffisant :

ln -s /local/php/php5/bin/php /usr/bin/php5
ln -s /local/php/php7/bin/php /usr/bin/php7

De cette façon, le binaire php sera accessible depuis n'importe tout sur le serveur, avec n'importe quel utilisateur, depuis la commande php5 pour du PHP 5 et depuis la commande php7 pour du PHP 7.

Versions mineures

En cas de présence différentes version mineures de PHP, en s'inspirant de la section Versions majeures, il faudra simplement rajouter un chiffre distinguant sur le numéro de version.

Par exemple, en cas de présence des versions 5.6.40, 5.6.6, 7.2.19 et 7.3.6, il faudra créer les liens symboliques suivants :

ln -s /local/php/5.6.40/bin/php /usr/bin/php5640
ln -s /local/php/5.6.6/bin/php /usr/bin/php566
ln -s /local/php/7.2.19/bin/php /usr/bin/php72
ln -s /local/php/7.3.6/bin/php /usr/bin/php73

Fichier php.ini

Nouvelle installation

En cas de nouvelle installation, pour partir d'une configuration par défaut, il est possible de s'appuyer sur un des deux fichiers modèles fournis par PHP.

Ces fichiers se trouvent dans le dossier de build de PHP. Exemple pour la version 5.6.40 en lançant la commande suivante :

ls -1 /local/builds/php-5.6.40/php.ini*

Exemple de retour :

/local/builds/php-5.6.40/php.ini-development
/local/builds/php-5.6.40/php.ini-production

Pour retrouver ces informations, il est possible de consulter le fichier INSTALL dans le dossier de build.

Exemple d'informations relatives au fichier php.ini pour PHP 5.6.40 :

210 13. Setup your php.ini file:
211 
212       cp php.ini-development /usr/local/lib/php.ini
213 
214     You may edit your .ini file to set PHP options.  If you prefer your
215     php.ini in another location, use --with-config-file-path=/some/path in
216     step 10.
217 
218     If you instead choose php.ini-production, be certain to read the list
219     of changes within, as they affect how PHP behaves.

Exemple d'informations relatives au fichier php.ini pour PHP 7.2.19 :

237     6. Setup your php.ini
238 cp php.ini-development /usr/local/lib/php.ini
239 
240        You  may  edit  your  .ini  file  to set PHP options. If you prefer
241        having php.ini in another location, use
242        --with-config-file-path=/some/path in step 5.
243        If  you  instead  choose php.ini-production, be certain to read the
244        list of changes within, as they affect how PHP behaves.

NB : Les chemins indiqués sont, bien évidemment, à adapter selon l'arborescence dans laquelle PHP a été installé.

Par exemple, en PHP 5.6.40, pour s'appuyer sur le php.ini de développement, lancer la commande suivante :

cp /local/builds/php-5.6.40/php.ini-development /local/php/php5/lib64/php.ini

Autre exmple, en PHP 7.2.19, pour s'appuyer sur le php.ini de production, lancer la commande suivante :

cp /local/builds/php-7.2.19/php.ini-production /local/php/php7/lib64/php.ini

Il faut ensuite éditer le fichier php.ini pour l'adapter aux besoins.

Installation existante

Pour identifier le fichier php.ini chargé par PHP, lancer la commande suivante :

php --ini

Exemple de retour :

Configuration File (php.ini) Path: /local/php/php5/lib64
Loaded Configuration File:         /local/php/php5/lib64/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

Partie en cours de rédaction

export PHP_VERSION=7.4.16
mkdir -p /opt/php/php-${PHP_VERSION}
wget https://www.php.net/distributions/php-${PHP_VERSION}.tar.gz
tar -zxf php-${PHP_VERSION}.tar.gz -C /data/builds
cd /data/builds/php-${PHP_VERSION}
./configure --prefix=/opt/php/php-${PHP_VERSION} --with-config-file-path=/opt/php/php-${PHP_VERSION} --disable-all --enable-static --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-fpm-systemd --disable-ipv6 --enable-cli --with-zlib --with-bz2 --enable-calendar --enable-ctype --with-curl --enable-gd --enable-intl --with-ldap --enable-mbstring --enable-pdo --with-pdo-mysql --enable-shared=pdo-mysql --enable-phar --with-libxml --with-iconv --enable-dom --enable-filter --enable-tokenizer --enable-json --enable-session --enable-xml --enable-xmlreader --enable-xmlwriter --enable-opcache --enable-fileinfo --enable-simplexml --enable-soap --enable-ftp --with-zip --with-pear --with-openssl --with-libdir=lib64 --with-mysqli && make -j && make install
cp /opt/php/php-${PHP_VERSION}/etc/php-fpm.conf.default /opt/php/php-${PHP_VERSION}/etc/php-fpm.conf
sed -e 's@;pid = run/php-fpm.pid@;pid = run/php-fpm.pid\npid = run/php-fpm.pid@g' /opt/php/php-${PHP_VERSION}/etc/php-fpm.conf
cp /data/builds/php-${PHP_VERSION}/php.ini-production /opt/php/php-${PHP_VERSION}/php.ini
sed -i -e 's@;error_log = syslog@;error_log = syslog\nerror_log = /data/logs/localhost/localhost_php-${PHP_VERSION}.log@g' /opt/php/php-${PHP_VERSION}/php.ini
sed -i -e 's@;sendmail_path =@sendmail_path = "/usr/bin/msmtp -t"@g' /opt/php/php-${PHP_VERSION}/php.ini
sed -i -e 's@;date.timezone =@date.timezone = "Europe/Paris"@g' /opt/php/php-${PHP_VERSION}/php.ini
sed -i -e 's@\[opcache\]@\[opcache\]\nzend_extension=$(grep no-debug-non-zts /opt/php/php-${PHP_VERSION}/bin/php-config|awk -F\' '{print $2}')/opcache.so@g' /opt/php/php-${PHP_VERSION}/php.ini
sed -i -e 's@\[opcache\]@\[opcache\]\nzend_extension='$(grep no-debug-non-zts /opt/php/php-${PHP_VERSION}/bin/php-config|awk -F\' '{print $2}')'/opcache.so@g' /opt/php/php-${PHP_VERSION}/php.ini
/opt/php/php-${PHP_VERSION}/bin/pear config-set php_ini /opt/php/php-${PHP_VERSION}/php.ini system
/opt/php/php-${PHP_VERSION}/bin/pecl config-set php_ini /opt/php/php-${PHP_VERSION}/php.ini system
cat <<EOF >/opt/php/php-${PHP_VERSION}/etc/php-fpm.d/localhost.conf
[localhost]
listen = /opt/php/sockets/php-${PHP_VERSION}_\$pool.sock
listen.owner = php-fpm
listen.group = www
listen.mode = 0660
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.status_path = /php-fpm-status
ping.path = /php-fpm-ping
access.log = /data/logs/\$pool/\$pool_php-${PHP_VERSION}.access.log
slowlog = /data/logs/\$pool/\$pool_php-${PHP_VERSION}.log.slow
php_admin_value[error_log] = /data/logs/\$pool/\$pool_php-${PHP_VERSION}.log
php_admin_flag[log_errors] = on
php_admin_value[error_reporting] = E_ALL
php_admin_value[session.save_path] = "/opt/php/sessions/\$pool/"
php_value[session.save_path] = "/opt/php/\$pool/"
EOF
mkdir -p /opt/php/sockets
mkdir -p /opt/php/sessions
chown -R php-fpm:www /opt/php
cp /data/builds/php-${PHP_VERSION}/sapi/fpm/php-fpm.service /usr/lib/systemd/system/php-fpm-${PHP_VERSION}.service
sed -i -e 's/^ProtectKernelModules/#ProtectKernelModules/g' /usr/lib/systemd/system/php-fpm-${PHP_VERSION}.service
sed -i -e 's/^ProtectKernelTunables/#ProtectKernelTunables/g' /usr/lib/systemd/system/php-fpm-${PHP_VERSION}.service
sed -i -e 's/^ProtectControlGroups/#ProtectControlGroups/g' /usr/lib/systemd/system/php-fpm-${PHP_VERSION}.service
sed -i -e 's/^RestrictRealtime/#RestrictRealtime/g' /usr/lib/systemd/system/php-fpm-${PHP_VERSION}.service
sed -i -e 's/^RestrictNamespaces/#RestrictNamespaces/g' /usr/lib/systemd/system/php-fpm-${PHP_VERSION}.service
chmod o+r /usr/lib/systemd/system/php-fpm-${PHP_VERSION}.service
systemctl daemon-reload
systemctl start php-fpm-${PHP_VERSION}.service
sed -i -e 's/\[Service\]/\[Service\]\nUMask=0007\nUser=php-fpm\nGroup=www\nEnvironment="PHP_VERSION='${PHP_VERSION}'"/g' /usr/lib/systemd/system/php-fpm-${PHP_VERSION}.service
systemctl daemon-reload
systemctl start php-fpm-${PHP_VERSION}.service