Skip to main content

Compile from Source

Compiling PHP from source offers greater flexibility in tailoring the installation to your specific needs. However, this process can be quite memory-intensive. Based on our tests, if your system has less than 2GB of available RAM, we do not recommend compiling from source, as you may encounter performance issues or compilation failures.

Install PHP

install-php.sh
#!/bin/sh

# Interactive input for PHP version
read -p "Please enter the PHP version you want to install (e.g., 8.3.15): " php_version

# Install required dependencies
dnf install -y gcc gcc-c++ make libxml2-devel bzip2-devel curl-devel libpng-devel libjpeg-devel libXpm-devel freetype-devel gmp-devel libmcrypt-devel oniguruma-devel libxslt-devel libzip-devel libwebp-devel zip cronie sqlite sqlite-devel systemd-devel

# Download and compile PHP
cd /usr/local/src
wget https://www.php.net/distributions/php-${php_version}.tar.gz
tar -xzf php-${php_version}.tar.gz
cd php-${php_version}

make clean
./configure \
--prefix=/usr/local/php \
--enable-gd \
--enable-soap \
--enable-intl \
--enable-bcmath \
--enable-sockets \
--enable-opcache \
--enable-mbstring \
--enable-pcntl \
--enable-fpm \
--with-config-file-path=/usr/local/php \
--with-fpm-user=www \
--with-fpm-group=www \
--with-fpm-systemd \
--with-mysqli \
--with-pdo-mysql \
--with-openssl \
--with-curl \
--with-zip \
--with-zlib \
--with-bz2 \
--with-external-pcre \
--with-jpeg \
--with-freetype \
--with-xsl \
--with-pear

make -j$(nproc)
make install
cp php.ini-production /usr/local/php/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

Create PHP-FPM Service

vi /etc/systemd/system/php-fpm.service
[Unit]
Description=PHP FastCGI Process Manager
After=network.target

[Service]
Type=simple
RuntimeDirectory=php-fpm
RuntimeDirectoryMode=0750
RuntimeDirectoryOwner=www
RuntimeDirectoryGroup=www
PIDFile=/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGQUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Directory and Permissions

# The 'www' user needs read access to the PHP installation directory to load core files, but should not have root-level write access.
chown -R root:www /usr/local/php
chmod -R 750 /usr/local/php

# PHP-FPM requires access to extension modules (e.g., redis.so).
chown -R root:www /usr/local/php/lib/php/extensions
chmod -R 750 /usr/local/php/lib/php/extensions

# The PHP-FPM master process needs to read global and pool configuration files.
chown -R www:www /usr/local/php/etc
chmod -R 750 /usr/local/php/etc

Location of PHP Configuration Files

  • PHP.ini location: /usr/local/php/php.ini
  • PHP-FPM configuration file: /usr/local/php/etc/php-fpm.conf
  • PHP FPM configuration directory: /usr/local/php/etc/php-fpm.d/