Blog / Linux/ PHP 7 Compilation Configuration Options Explained with Best Practices

PHP 7 Compilation Configuration Options Explained with Best Practices

Overview of Core Configuration Options

PHP's configure script provides numerous compilation options for customizing the build on Unix-like systems. These options only take effect during compilation; runtime configuration is handled via the php.ini file.

To view all available options, run the following command in the PHP source directory:

./configure --help

Below is a categorized explanation of commonly used configuration options.

Installation Directory Configuration

These options control the installation paths for PHP and its related files.

  • --prefix=PREFIX: The root directory for architecture-independent files. Default is /usr/local.
  • --exec-prefix=EPREFIX: Directory for architecture-dependent files. Default is PREFIX.
  • --bindir=DIR: Directory for user executables. Default is EPREFIX/bin.
  • --libdir=DIR: Directory for library files. Default is EPREFIX/lib. On 64-bit systems, you may need to specify --with-libdir=lib64.
  • --sysconfdir=DIR: Directory for read-only single-machine configuration files. Default is PREFIX/etc.

SAPI (Server API) Modules

SAPI defines how PHP interacts with the external environment.

Common SAPI Options

  • --enable-fpm: Build PHP-FPM (FastCGI Process Manager), suitable for web servers like Nginx.
  • --with-fpm-user=USER: Set the user for php-fpm to run as. Default is nobody.
  • --with-fpm-group=GRP: Set the group for php-fpm to run as. Default is nobody.
  • --with-fpm-systemd: Enable systemd integration.
  • --with-apxs2=FILE: Build Apache 2.0 shared module. Requires the path to the apxs tool.
  • --disable-cli: Disable building the CLI (Command Line Interface) version.
  • --disable-cgi: Disable building the CGI version. Since PHP 5.3.0, this enables FastCGI.

General Settings

  • --enable-debug: Enable debugging symbols for troubleshooting.
  • --with-config-file-path=PATH: Set the search path for php.ini. Default is PREFIX/lib.
  • --with-config-file-scan-dir=PATH: Set the directory for scanning additional configuration files.
  • --disable-short-tags: Disable short tags (<?) by default. Recommended for better code portability.
  • --enable-opcache: Enable Zend OPcache bytecode cache. Highly recommended for production environments.

Extension Module Configuration

PHP extensions can be compiled statically or built as shared modules (.so files). Use the =shared suffix to build as a shared extension.

Common Extension Options Example

--with-mysqli=mysqlnd
--with-pdo-mysql=mysqlnd
--with-openssl
--with-zlib
--enable-mbstring
--enable-bcmath
--enable-calendar
--enable-ftp
--with-curl
--with-gd
--enable-exif
--enable-intl
--with-xsl
--enable-zip

Key Notes

  • MySQL Driver: It is recommended to use mysqlnd (MySQL Native Driver), which is usually built into the PHP source and requires no external library.
    --with-mysqli=mysqlnd
    --with-pdo-mysql=mysqlnd
  • GD Library: For image processing, enable GD and specify related library paths.
    --with-gd --with-freetype --with-jpeg --with-png
  • Shared Extensions: To enable/disable extensions individually, build them as shared modules.
    --with-zlib=shared

Compilation Optimization and Thread Safety

  • --enable-maintainer-zts: Enable thread safety (ZTS). Only required if you need to embed PHP into a multi-threaded application (like certain Apache MPM modes) or build thread-safe extensions. Most CLI or FPM environments do not need this.
  • --disable-inline-optimization: Try this option if compilation of zend_execute.lo fails.

Environment Variables

Pass parameters to the toolchain via environment variables during compilation:

  • CFLAGS: C compiler flags, e.g., optimization level -O2.
  • CPPFLAGS: Preprocessor flags, e.g., include path -I/usr/local/include.
  • LDFLAGS: Linker flags, e.g., library path -L/usr/local/lib.

Typical Compilation Configuration Example

Below is a general configuration example suitable for a PHP 7.4+ production environment, including common extensions and OPcache enabled:

./configure 
    --prefix=/usr/local/php 
    --with-config-file-path=/usr/local/php/etc 
    --enable-fpm 
    --with-fpm-user=www-data 
    --with-fpm-group=www-data 
    --enable-opcache 
    --with-mysqli=mysqlnd 
    --with-pdo-mysql=mysqlnd 
    --with-openssl 
    --with-zlib 
    --enable-mbstring 
    --enable-bcmath 
    --with-curl 
    --with-gd 
    --with-jpeg 
    --with-freetype 
    --enable-exif 
    --with-zip 
    CFLAGS="-O2"

Note: Actual configuration should be adjusted based on libraries installed on the server and project requirements. Ensure all necessary development packages (e.g., libxml2-dev, libssl-dev) are installed before compiling.

Post a Comment

Your email will not be published. Required fields are marked with *.