Skip to main content

Install PHP

Core

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

Configure PHP

/usr/local/php/php.ini
include_path = ".:/usr/local/php/lib/php"
expose_php = Off
error_log = "/var/log/php-error.log"
max_execution_time = 30
memory_limit = 128M
post_max_size = 8M
display_errors = Off
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,show_source
mysqli.default_socket = /run/mysqld.sock
pdo_mysql.default_socket = /run/mysqld.sock

Configure PHP-FPM

/usr/local/php/etc/php-fpm.conf
pid = /run/php-fpm.pid
error_log = /var/log/php-fpm.log
log_level = notice

Directory and Permissions

# Directory and permissions for log files
mkdir -p /var/log
touch /var/log/php-fpm.log
touch /var/log/php-error.log
chown www:www /var/log/php-fpm.log
chown www:www /var/log/php-error.log
chmod 640 /var/log/php-fpm.log
chmod 640 /var/log/php-error.log

# 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

# Write permissions are needed for PHP-FPM error and access logs.
mkdir -p /var/log/php-fpm
chown -R www:www /var/log/php-fpm
chmod -R 750 /var/log/php-fpm

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

Start PHP-FPM Service

systemctl daemon-reload
systemctl enable --now php-fpm
systemctl status php-fpm