Compilando PHP FPM en CentOS 7

Realmente ya no es necesario en la mayoría de los casos compilar tu propia versión de PHP pero en ciertos momentos puede ser necesario, por ejemplo, a la hora de compilar una librería que no esté plenamente difundida o por temas de rendimiento porque al crear una versión compilada puedes incluir en el núcleo de php las extensiones que utilizas.

Este procedimiento lo hice en septiembre del año pasado, cuando aún no estaba tan extendido en linux la instalación de la librería de Microsoft para SQL Server, por eso la incluyo en este manual.

¿Qué necesitamos instalar primero?

yum groupinstall "Development Tools"
(yum install kernel-devel)

yum install wget \
     autoconf \
     libxml2-devel \
     krb5-devel \
     libicu-devel \
     gcc-c++ \
     bzip2-devel \
     curl-devel \
     libpng-devel \
     libc-client-devel \
     openssl-devel \
     libmcrypt-devel.x86_64 \
     net-snmp \ 
     net-snmp-devel \
     git

En caso de errores podéis consultar esta guía con los 20 errores más comunes compilando php. En el listado de dependencias anteriores a instalar solucionamos también errores como estos:

libxml2-devel

checking libxml2 install dir... no
checking for xml2-config path...
configure: error: xml2-config not found. Please check your libxml2 installation.

krb5-devel

configure: error: Kerberos libraries not found

libicu-devel

checking for location of ICU headers and libraries... not found
configure: error: Unable to detect ICU prefix or no failed. Please verify ICU install prefix 
and make sure icu-config works.

gcc-c++

configure: error: C++ preprocessor "/lib/cpp" fails sanity check

Luego necesitamos descargar el código fuente de la versión deseada de PHP y el código fuente de la librería que queremos compilar, moviendo este último al directorio de extensiones:
wget http://am1.php.net/distributions/php-7.0.10.tar.gz
tar xvzf php-7.0.10.tar.gz
git clone https://github.com/Microsoft/msphpsql/ --branch PHP-7.0-Linux  msphpsql
cd /root/msphpsql/source
cp -r * /root/php-7.0.10/ext/
cd /root/php-7.0.10
./buildconf --force

Esto producirá la siguiente salida

Forcing buildconf
Removing configure caches
buildconf: checking installation...
buildconf: autoconf version 2.69 (ok)
rebuilding aclocal.m4
rebuilding configure
rebuilding main/php_config.h.in

Lo siguiente son necesidades propias del driver para SQK Server, no es necesario que las incluyais. Este driver necesita unixODBC y debemos buscar su ubicación

sudo find / -name sql.h

/usr/include/sql.h
/home/reynier/unixODBC-2.3.1/include/sql.h

Finalmente compilamos

./configure LIBS=-lodbc --with-unixODBC=/home/reynier/unixODBC-2.3.1 \
                        --enable-fpm \
                        --enable-intl \
                        --with-kerberos \
                        --with-mcrypt \
                        --with-snmp \
                        --with-mhash \
                        --with-zlib \
                        --with-gettext \
                        --enable-exif \
                        --enable-zip \
                        --with-bz2 \
                        --enable-soap \
                        --enable-sockets \
                        --with-pear \
                        --enable-mbstring \
                        --with-libdir=lib64 \
                        --with-curl \
                        --with-gd \
                        --enable-calendar \
                        --enable-ftp \
                        --enable-pdo \
                        --enable-inline-optimization \
                        --with-imap \
                        --with-imap-ssl \
                        --with-fpm-user=nginx \
                        --with-fpm-group=nginx \
                        --enable-fileinfo CXXFLAGS=-std=c++11 \
                        --enable-sqlsrv=shared \
                        --with-pdo_sqlsrv=shared \
                        --with-odbcver=0x0380

--------
--with-config-file-path=/etc/php/ 
--with-config-file-scan-dir=/etc/php.d/ 
--------

make

make test

=====================================================================
TEST RESULT SUMMARY
---------------------------------------------------------------------
Exts skipped    :   32
Exts tested     :   45
---------------------------------------------------------------------

Number of tests : 14557             11939
Tests skipped   : 2618 ( 18.0%) --------
Tests warned    :    6 (  0.0%) (  0.1%)
Tests failed    :   40 (  0.3%) (  0.3%)
Expected fail   :   41 (  0.3%) (  0.3%)
Tests passed    : 11852 ( 81.4%) ( 99.3%)
---------------------------------------------------------------------
Time taken      : 2187 seconds
=====================================================================

You may have found a problem in PHP.
This report can be automatically sent to the PHP QA team at
http://qa.php.net/reports and http://news.php.net/php.qa.reports
This gives us a better understanding of PHP's behavior.
If you don't want to send the report immediately you can choose
option "s" to save it.  You can then email it to qa-reports@lists.php.net later.
Do you want to send this report now? [Yns]: Y

make install

Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20151012/
Installing PHP CLI binary:        /usr/local/bin/
Installing PHP CLI man page:      /usr/local/php/man/man1/
Installing PHP FPM binary:        /usr/local/sbin/
Installing PHP FPM config:        /usr/local/etc/
Installing PHP FPM man page:      /usr/local/php/man/man8/
Installing PHP FPM status page:   /usr/local/php/php/fpm/
Installing phpdbg binary:         /usr/local/bin/
Installing phpdbg man page:       /usr/local/php/man/man1/
Installing PHP CGI binary:        /usr/local/bin/
Installing PHP CGI man page:      /usr/local/php/man/man1/
Installing build environment:     /usr/local/lib/php/build/
Installing header files:           /usr/local/include/php/
Installing helper programs:       /usr/local/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/lib/php/
[PEAR] Archive_Tar    - installed: 1.4.0
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.3.0
[PEAR] PEAR           - installed: 1.10.1
Wrote PEAR system config file at: /usr/local/etc/pear.conf
You may want to add: /usr/local/lib/php to your php.ini include_path
/root/php-7.0.10/build/shtool install -c ext/phar/phar.phar /usr/local/bin
ln -s -f phar.phar /usr/local/bin/phar
Installing PDO headers:           /usr/local/include/php/ext/pdo/

Comprobamos la versión de PHP instalada

/usr/local/bin/php -v
PHP 7.0.10 (cli) (built: Sep  1 2016 10:36:29) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

Configuramos PHP FPM

cp php.ini-production /usr/local/lib/php.ini
# editamos la línea cgi.fix_pathinfo=0

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
cp /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf

# quitamos el comentario a las siguientes línea de este fichero
pid = run/php-fpm.pid
error_log = log/php-fpm.log

Errores comunes y cómo solucionarlos

Job for php-fpm.service failed because the control process exited with error code. 
See "systemctl status php-fpm.service" and "journalctl -xe" for details.

NONE/etc/php-fpm.d/*.conf por include=/usr/local/etc/php-fpm.d/*.conf

Unable to globalize '/usr/local/NONE/etc/php-fpm.d/*.conf' (ret = 2) 
from /usr/local/etc/php-fpm.conf at line 125. 
failed to load configuration file '/usr/local/etc/php-fpm.conf' 
FPM initialization failed

cp /usr/local/etc/php-fpm.d/www.conf.default /usr/local/etc/php-fpm.d/www.conf

Nothing matches the include pattern '/usr/local/etc/php-fpm.d/*.conf' 
from /usr/local/etc/php-fpm.conf at line 125. 
No pool defined at least one pool section must be specified in config file 
failed to post process the configuration 
FPM initialization failed

/sbin/chkconfig php-fpm on 

Failed to start php-fpm.service: Unit php-fpm.service failed to load: No such file or directory.

systemctl start php-fpm

Comentarios