From 96ae6e6f64534a7a38817e188bc9b4f8627df7c2 Mon Sep 17 00:00:00 2001 From: sspencerwire Date: Wed, 22 Oct 2025 11:38:43 -0500 Subject: [PATCH] Edit `guides/web/php.md` for 10: * remove 8 and 9 specific content * add procedure for discovering the php version that will install * show content of `php -v` for 10 * replace passive voice with acitve voice * replace conjunctions with words * simplify some sentences * ensure processes and files are in back ticks * change the meta "tested with:" to 10 --- docs/guides/web/php.md | 260 ++++++++++++----------------------------- 1 file changed, 77 insertions(+), 183 deletions(-) diff --git a/docs/guides/web/php.md b/docs/guides/web/php.md index 10c11138f4..9754f2cf66 100644 --- a/docs/guides/web/php.md +++ b/docs/guides/web/php.md @@ -2,7 +2,7 @@ title: PHP and PHP-FPM author: Antoine Le Morvan contributors: Steven Spencer, Ganna Zhyrnova, Joseph Brinkman -tested_with: 9.3, 8.9 +tested_with: 10.0 tags: - web - php @@ -11,13 +11,13 @@ tags: # PHP and PHP-FPM -**PHP** (**P**HP **H**ypertext **P**reprocessor) is a source scripting language specially designed for web application development. In 2024, PHP represented a little less than 80% of the web pages generated in the world. PHP is open-source and is the core of the most famous CMS (WordPress, Drupal, Joomla!, Magento, ...). +**PHP** (**P**HP **H**ypertext **P**reprocessor) is a source scripting language specially designed for web application development. In 2024, PHP represented a little less than 80% of the web pages generated in the world. PHP is open source and is the core of the most famous CMS (WordPress, Drupal, Joomla!, Magento, and others). -**PHP-FPM** (**F**astCGI **P**rocess **M**anager) is integrated to PHP since its version 5.3.3. The FastCGI version of PHP brings additional functionalities. +PHP integrates **PHP-FPM** (**F**astCGI **P**rocess **M**anager) since version 5.3.3. The FastCGI version of PHP brings additional functionalities. ## Generalities -**CGI** (**C**ommon **G**ateway **I**nterface) and **FastCGI** allow communication between the web server (Apache, Nginx, ...) and a development language (PHP, Python, Java): +**CGI** (**C**ommon **G**ateway **I**nterface) and **FastCGI** allow communication between the web server (Apache, Nginx, and others) and a development language (PHP, Python, Java): * In the case of **CGI**, each request creates a **new process**, which is less efficient in performance. * **FastCGI** relies on a **certain number of processes** to treat its client requests. @@ -35,155 +35,49 @@ PHP-FPM, **in addition to better performances**, brings: ## Choose a PHP version -Rocky Linux, like its upstream, offers many versions of the language. Some of them have reached the end of their life but are kept to continue hosting historical applications that are not yet compatible with new versions of PHP. Please refer to the [supported versions](https://www.php.net/supported-versions.php) page of the php.net website to choose a supported version. +Rocky Linux 10, as with its upstream, no longer has modules. The version of PHP is what is available from the Appstream repository. To check the available version of PHP use: -To obtain a list of available versions, enter the following command: - -=== "9.3 PHP module list" - - ```bash - $ sudo dnf module list php - - Rocky Linux 9 - AppStream - Name Stream Profiles Summary - php 8.1 [d] common [d], devel, minimal - - Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled - ``` - - The Remi repository offers more recent releases of PHP than the Appstream repository, including versions 8.3 and 8.4. - - To install the Remi repository, run the following command (note: if you are running Rocky Linux 8.x or 10.x, substitute in 8 or 10 next to the "release-" below): - - ```bash - sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm - ``` - - Once the Remi repository is installed, enable it by running the following command. - - ```bash - sudo dnf config-manager --set-enabled remi - ``` - - You can now activate a newer module (PHP 8.4) by entering the following command: - - ```bash - sudo dnf module enable php:remi-8.4 - ``` - -=== "8.9 PHP module list" - - ```bash - $ sudo dnf module list php - - Rocky Linux 8 - AppStream - Name Stream Profiles Summary - php 7.2 [d] common [d], devel, minimal PHP scripting language - php 7.3 common [d], devel, minimal PHP scripting language - php 7.4 common [d], devel, minimal PHP scripting language - php 8.0 common [d], devel, minimal PHP scripting language - - Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled - ``` - - Rocky provides different PHP modules from its AppStream repository. - - You will note that the default version of a Rocky 8.9 is 7.2 that has already reached its end of life at the time of writing. - - You can activate a newer module by entering the following command: - - ```bash - sudo dnf module enable php:8.0 - ============================================================================================== - Package Architecture Version Repository Size - ============================================================================================== - Enabling module streams: - httpd 2.4 - nginx 1.14 - php 8.0 - - Transaction Summary - ============================================================================================== +```bash +dnf whatprovides php - Is this ok [y/N]: +Last metadata expiration check: 0:00:03 ago on Wed 22 Oct 2025 03:58:30 PM UTC. - Transaction Summary - ============================================================================================== +php-8.3.19-1.el10_0.x86_64 : PHP scripting language for creating dynamic web sites +Repo : @System +Matched from: +Provide : php = 8.3.19-1.el10_0 - Is this ok [y/N]: y - Complete! - ``` +php-8.3.19-1.el10_0.x86_64 : PHP scripting language for creating dynamic web sites +Repo : appstream +Matched from: +Provide : php = 8.3.19-1.el10_0 +``` You can now proceed to the installation of the PHP engine. ## PHP cgi mode -First, let's see how to install and use PHP in CGI mode. We can only make it work with the Apache web server and its `mod_php` module. We will see, later in this document, in the FastCGI part (php-fpm) how to integrate PHP in Nginx (but also Apache). +First, install PHP. You can only make it work with the Apache web server and its `mod_php` module. You will see, later in this document, in the FastCGI part (`php-fpm`) how to integrate PHP in Nginx and Apache. ### Installation The installation of PHP is relatively trivial since it consists of installing the main package and the few modules you will need. -The example below installs PHP with the modules usually installed with it. - -=== "9.3 install PHP" - - ```bash - sudo dnf install php php-cli php-gd php-curl php-zip php-mbstring - ``` - - You will be prompted to import GPG keys for the epel9 (Extra Packages for Enterprise Linux 9) and Remi repositories during installation. Enter y to import the keys: - - ```bash - Extra Packages for Enterprise Linux 9 - x86_64 - Importing GPG key 0x3228467C: - Userid : "Fedora (epel9) " - Fingerprint: FF8A D134 4597 106E CE81 3B91 8A38 72BF 3228 467C - From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-9 - Is this ok [y/N]: y - Key imported successfully - Remi's RPM repository for Enterprise Linux 9 - x86_64 - Importing GPG key 0x478F8947: - Userid : "Remi's RPM repository (https://rpms.remirepo.net/) " - Fingerprint: B1AB F71E 14C9 D748 97E1 98A8 B195 27F1 478F 8947 - From : /etc/pki/rpm-gpg/RPM-GPG-KEY-remi.el9 - Is this ok [y/N]: y - Key imported successfully - Running transaction check - Transaction check succeeded. - Running transaction test - Transaction test succeeded. - - Complete! - ``` - -=== "8.9 install PHP" - - ```bash - sudo dnf install php php-cli php-gd php-curl php-zip php-mbstring - ``` - -You can check that the installed version corresponds to the expected one: +This example installs PHP with the modules commonly installed with it: -=== "9.3 check PHP version" - - ```bash - $ php -v - PHP 8.3.2 (cli) (built: Jan 16 2024 13:46:41) (NTS gcc x86_64) - Copyright (c) The PHP Group - Zend Engine v4.3.2, Copyright (c) Zend Technologies - with Zend OPcache v8.3.2, Copyright (c), by Zend Technologies - ``` +```bash +sudo dnf install php php-cli php-gd php-curl php-zip php-mbstring +``` -=== "8.9 check PHP version" +You can check that the installed version corresponds to the expected one: - ```bash - $ php -v - PHP 7.4.19 (cli) (built: May 4 2021 11:06:37) ( NTS ) - Copyright (c) The PHP Group - Zend Engine v3.4.0, Copyright (c) Zend Technologies - with Zend OPcache v7.4.19, Copyright (c), by Zend Technologies - ``` +```bash +php -v +PHP 8.3.19 (cli) (built: Mar 12 2025 13:10:27) (NTS gcc x86_64) +Copyright (c) The PHP Group +Zend Engine v4.3.19, Copyright (c) Zend Technologies + with Zend OPcache v8.3.19, Copyright (c), by Zend Technologies +``` ### Configuration @@ -193,25 +87,25 @@ To serve PHP pages in CGI mode, you must install the Apache server, configure it * Installation: - ```bash - sudo dnf install httpd - ``` + ```bash + sudo dnf install httpd + ``` - * Activation: + * Activation: - ```bash - sudo systemctl enable --now httpd - sudo systemctl status httpd - ``` + ```bash + sudo systemctl enable --now httpd + sudo systemctl status httpd + ``` -* Don't forget to configure the firewall: +* Do not forget to configure the firewall: - ```bash - sudo firewall-cmd --add-service=http --permanent - sudo firewall-cmd --reload - ``` + ```bash + sudo firewall-cmd --add-service=http --permanent + sudo firewall-cmd --reload + ``` -The default vhost should work out of the box. PHP provides a `phpinfo()` function that generates a summary table of its configuration. It's very useful to test the good working of PHP. However, be careful not to leave such test files on your servers. They represent a huge security risk for your infrastructure. +The default vhost should work right away. PHP provides a `phpinfo()` function that generates a summary table of its configuration. It is very useful to test the good working of PHP. However, be careful not to leave such test files on your servers. They represent a huge security risk for your infrastructure. Create the file `/var/www/html/info.php` (`/var/www/html` being the default vhost directory of the default Apache configuration): @@ -225,21 +119,21 @@ Use a web browser to check that the server works properly by going to the page [ !!! Warning - Do not leave the info.php file on your server! + Do not leave the `info.php` file on your server! ## PHP-FPM (FastCGI) As we highlighted earlier in this document, there are many advantages to switching web hosting to PHP-FPM mode. -### Installation +### Install `php-fpm` -The installation is limited to the php-fpm package: +To install the `php-fpm` package use: ```bash sudo dnf install php-fpm ``` -As php-fpm is a service from a system point of view, it must be activated and started: +As `php-fpm` is a service from a system point of view, you must activate and start it: ```bash sudo systemctl enable --now php-fpm @@ -248,7 +142,7 @@ sudo systemctl status php-fpm ### Configuration -The main configuration file is stored under `/etc/php-fpm.conf`. +`php-fpm` stores the main configuration file under `/etc/php-fpm.conf`: ```bash include=/etc/php-fpm.d/*.conf @@ -260,11 +154,11 @@ daemonize = yes !!! Note - The php-fpm configuration files are widely commented on. Go and have a look! + The `php-fpm` configuration files are widely commented on. Go and have a look! As you can see, the files in the `/etc/php-fpm.d/` directory with the `.conf` extension are always included. -By default, a PHP process pool, named `www`, is declared in `/etc/php-fpm.d/www.conf`. +By default, `php-fpm` declares a PHP process pool named `www` in `/etc/php-fpm.d/www.conf`: ```bash [www] @@ -295,15 +189,15 @@ php_value[soap.wsdl_cache_dir] = /var/lib/php/wsdlcache | `[pool]` | Process pool name. The configuration file can comprise several process pools (the pool's name in brackets starts a new section). | | `listen` | Defines the listening interface or the Unix socket used. | -#### Configuring the way to access php-fpm processes +#### Configuring the way to access `php-fpm` processes There are 2 ways to connect. -Via an inet interface such as: +With an inet interface such as: `listen = 127.0.0.1:9000`. -Or via a Unix socket: +Or with a Unix socket: `listen = /run/php-fpm/www.sock`. @@ -311,17 +205,17 @@ Or via a Unix socket: Using a socket when the web server and PHP server are on the same machine removes the TCP/IP layer and optimizes the performance. -When working via an interface, you have to configure `listen.owner`, `listen.group`, `listen.mode` to specify the owner, the owner group and the rights of the Unix socket. **Warning:** Both servers (web and PHP) must have access rights on the socket. +When working with an interface, you have to configure `listen.owner`, `listen.group`, `listen.mode` to specify the owner, the owner group and the rights of the Unix socket. **Warning:** Both servers (web and PHP) must have access rights on the socket. -When working via a socket, you must configure `listen.allowed_clients` to restrict access to the PHP server to certain IP addresses. +When working with a socket, you must configure `listen.allowed_clients` to restrict access to the PHP server to certain IP addresses. Example: `listen.allowed_clients = 127.0.0.1` #### Static or dynamic configuration -The processes of PHP-FPM can be managed statically or dynamically. +You can manage the processes of PHP-FPM either statically or dynamically. -In static mode, the number of child processes is set by the value of `pm.max_children`; +In static mode, `pm.max_children` sets the number of child processes: ```bash pm = static @@ -344,9 +238,9 @@ pm.max_spare_servers = 3 PHP-FPM will create a new process to replace one that has processed several requests equivalent to `pm.max_requests`. -By default, `pm.max_requests` is set to 0, meaning processes are never recycled. Using the `pm.max_requests` option can be interesting for applications with memory leaks. +By default, `php-fpm` sets `pm.max_requests` to 0, meaning processes are never recycled. Using the `pm.max_requests` option can be interesting for applications with memory leaks. -There is a third mode of operation, the `ondemand` mode. This mode only starts a process when it receives a request. It is not an optimal mode for sites with strong influences and is to be reserved for specific needs (sites with very weak requests, management backend, etc.). +There is a third mode of operation, the `ondemand` mode. This mode only starts a process when it receives a request. It is not an optimal mode for sites with strong influences and you should reserve this for specific needs (sites with very weak requests, management backend, and so on). !!! Note @@ -354,9 +248,9 @@ There is a third mode of operation, the `ondemand` mode. This mode only starts a #### Process status -PHP-FPM offers, like Apache and its `mod_status` module, a page indicating the status of the process. +As with Apache and its `mod_status` module, PHP-FPM offers a page indicating the status of the process. -To activate the page, set its access path via the `pm.status_path` directive: +To activate the page, set its access path with the `pm.status_path` directive: ```bash pm.status_path = /status @@ -382,7 +276,7 @@ slow requests: 0 #### Logging long requests -The slowlog directive specifies the file that receives logging of requests that are too long (i.e., whose time exceeds the value of the `request_slowlog_timeout` directive). +The `slowlog` directive specifies the file that receives logging of requests that are too long (for example, a file whose time exceeds the value of the `request_slowlog_timeout` directive). The default location of the generated file is `/var/log/php-fpm/www-slow.log`. @@ -395,9 +289,9 @@ A value of 0 for `request_slowlog_timeout` disables logging. ### NGinx integration -The default setting of nginx already includes the necessary configuration to make PHP work with PHP-FPM. +The default setting of `nginx` already includes the necessary configuration to make PHP work with PHP-FPM. -The configuration file `fastcgi.conf` (or `fastcgi_params`) is located under `/etc/nginx/`: +The configuration file `fastcgi.conf` (or `fastcgi_params`) is under `/etc/nginx/`: ```bash fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; @@ -427,7 +321,7 @@ fastcgi_param SERVER_NAME $server_name; fastcgi_param REDIRECT_STATUS 200; ``` -For nginx to process `.php` files, the following directives must be added to the site configuration file: +For `nginx` to process `.php` files, you must add the following directives to the site configuration file: If PHP-FPM is listening on port 9000: @@ -438,7 +332,7 @@ location ~ \.php$ { } ``` -If php-fpm is listening on a unix socket: +If `php-fpm` is listening on a Unix socket: ```bash location ~ \.php$ { @@ -469,9 +363,9 @@ The configuration of Apache to use a PHP pool is quite simple. You have to use t ### Solid configuration of PHP pools -It is essential to optimize the number of requests that will be able to be served and to analyze the memory used by the PHP scripts, to optimize the maximum amount of launched threads. +It is essential to optimize the number of served requests, and to analyze the memory used by the PHP scripts, to optimize the maximum amount of launched threads. -First of all, we need to know the average amount of memory used by a PHP process with the command: +First of all, you need to know the average amount of memory used by a PHP process with the command: ```bash while true; do ps --no-headers -o "rss,cmd" -C php-fpm | grep "pool www" | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"Mb") }' >> avg_php_proc; sleep 60; done @@ -479,11 +373,11 @@ while true; do ps --no-headers -o "rss,cmd" -C php-fpm | grep "pool www" | awk ' After a while, this should give us a pretty accurate idea of the average memory footprint of a PHP process on this server. -The result of the rest of this document is a memory footprint of 120MB per process at full load. +The result of the rest of this document is a memory footprint of 120 megabytes per process at full load. -On a server with 8Gb of RAM, keeping 1Gb for the system and 1Gb for the OPCache (see the rest of this document), is 6Gb left to process PHP requests from clients. +On a server with 8 gigabytes of RAM, keeping 1 gigabyte for the system and 1 gigabyte for the OPCache (see the rest of this document), 6 gigabytes is left to process PHP requests from clients. -We can easily conclude that this server can accept at most **50 threads** `((6*1024) / 120)`. +You can conclude that this server can accept at most **50 threads** `((6*1024) / 120)`. A good configuration of `php-fpm` specific to this use case would be: @@ -510,7 +404,7 @@ It keeps the compiled PHP scripts in memory, which strongly impacts the executio To configure it, we must work on: -* The size of the memory dedicated to the opcache according to the hit ratio, configuring it correctly +* The size of the memory dedicated to the `opcache` according to the hit ratio, configuring it correctly * The number of PHP scripts to cache (number of keys + maximum number of scripts) * The number of strings to cache @@ -530,16 +424,16 @@ opcache.max_accelerated_files=4000 where: -* `opcache.memory_consumption` corresponds to the amount of memory needed for the opcache (to be increased until a correct hit ratio is obtained). +* `opcache.memory_consumption` corresponds to the amount of memory needed for the `opcache` (increase this until you obtain a correct hit ratio). * `opcache.interned_strings_buffer` the amount of strings to cache. * `opcache.max_accelerated_files` is near to the result of the `find ./ -iname "*.php"|wc -l` command. -You can refer to an `info.php` page (including the `phpinfo();`) to configure the opcache (see for example the values of `Cached scripts` and `Cached strings`). +You can refer to an `info.php` page (including the `phpinfo();`) to configure the `opcache` (see for example the values of `Cached scripts` and `Cached strings`). !!! Note - At each new deployment of new code, it will be necessary to empty the opcache (for example by restarting the php-fpm process). + At each new deployment of new code, it will be necessary to empty the `opcache` (for example by restarting the `php-fpm` process). !!! Note - Don't underestimate the speed gain that can be achieved by setting up and configuring the opcache correctly. + Do not underestimate the speed gain that can be achieved by setting up and configuring the `opcache` correctly.