Running websites on different CMS or different versions of the same CMS, it is usual that some sites need PHP 7.4, others PHP 7.2 and others can work on PHP 8.0 and so on. Centos 7 now has several php-fpm packages, and this article describes how to run two or more php-fpm services and configure nginx to use certain php version for certain site.
1. Update to PHP 8 on Centos if you still did not
Check current php version
php -v
PHP 7.4.30 (cli) (built: Jun 7 2022 08:38:19) ( NTS )
Install yum-utils if you still have not
yum install epel-release yum-utils
Check which php versions are mentioned in yum-config-manager
yum-config-manager | grep php
[remi-php74]
...
Check available versions of php:
yum search php-fpm
php-fpm.x86_64 : PHP FastCGI Process Manager
php54-php-fpm.x86_64 : PHP FastCGI Process Manager
php55-php-fpm.x86_64 : PHP FastCGI Process Manager
php56-php-fpm.x86_64 : PHP FastCGI Process Manager
php70-php-fpm.x86_64 : PHP FastCGI Process Manager
php71-php-fpm.x86_64 : PHP FastCGI Process Manager
php72-php-fpm.x86_64 : PHP FastCGI Process Manager
php73-php-fpm.x86_64 : PHP FastCGI Process Manager
php74-php-fpm.x86_64 : PHP FastCGI Process Manager
php80-php-fpm.x86_64 : PHP FastCGI Process Manager
php81-php-fpm.x86_64 : PHP FastCGI Process Manager
php82-php-fpm.x86_64 : PHP FastCGI Process Manager
There are actually many versions, but you should check which can be used from remi repository. I found it is remi-php80, because `yum-config-manager --enable remi-php82` added nothing and `yum-config-manager | grep php` result was not changed.
Add php80, update and check it is default:
yum-config-manager --enable remi-php80
yum update -y
php -v
PHP 8.0.21 (cli) (built: Jul 6 2022 10:13:53) ( NTS gcc x86_64 )
Now the latest php80 is running as default.
2. Edit php-fpm.d/www.conf for old PHP 7.4 to listen another socket as PHP 8.0
You can find and configure socket that the default PHP 8.0 is listening:
grep -P '^listen = ' /etc/php-fpm.d/www.conf
listen = 127.0.0.1:9000
Also "user" and "group" should be checked there, for me it is nginx.
Next, configure another socket, for example listen = 127.0.0.1:9001 in remi php74-php-fpm here (thanks to [3]):
vim /etc/opt/remi/php74/php-fpm.d/www.conf
user = nginx
group = nginx
listen = 127.0.0.1:9001
So, these lines should be checked and configured.
3. Add php74-php-fpm service
Centos 7 works with systemctl, let's check our services
systemctl status php74-php-fpm
... disabled
Enable and start service of php 7.4, then check listening ports:
systemctl enable php74-php-fpm
systemctl start php74-php-fpm
netstat -tunlp
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 11419/php-fpm: mast
tcp 0 0 127.0.0.1:9001 0.0.0.0:* LISTEN 11638/php-fpm: mast
Here, port 9000 is listening by default php80-php-fpm and port 9001 is listening by remi's php74-php-fpm. Now let's go to nginx configs.
4. Configure nginx to work with different php versions
Use hosts config files for that. On my server I defined php socket in /etc/nginx/fastcgi_params like this:
head -1 /etc/nginx/fastcgi_params
fastcgi_pass 127.0.0.1:9000;
Sometimes variable fastcgi_pass is defined in host configuration files like this
grep fastcgi_pass /etc/nginx/sites-available/example.com
fastcgi_pass 127.0.0.1:9000;
But sometimes this variable can be set in /etc/nginx/fastcgi_params.
Please, read carefully /etc/nginx/nginx.conf and /etc/nginx/fastcgi_params and also configs of your sites (hosts) for fit certain php version. On my server that was like this. I copied fastcgi_params, changed port for another version, and included different fastcgi_params to hosts configs:
cp /etc/nginx/fastcgi_params /etc/nginx/fastcgi_params_php7
vim /etc/nginx/fastcgi_params_php7
fastcgi_pass 127.0.0.1:9001;vim /etc/nginx/sites-available/example.com
include /etc/nginx/fastcgi_params_php7;vim /etc/nginx/sites-available/mysite.com
include /etc/nginx/fastcgi_params;
To sites are here: mysite.com is using /etc/nginx/fastcgi_params
and exmaple.com is using /etc/nginx/fastcgi_params_php7
Next check configs and restart nginx:
nginx -t
service nginx restart
5. Check phpinfo(); on sites
Create php file with content
<?php phpinfo(); ?>
Place it to sites directories and visit it:
https://example.com/phpinfo.php
PHP Version 7.4.30https://mysite.com/phpinfo.php
PHP Version 8.0.21
That's it.
mysite.com is configured to forward request to php files to socket of php74: fastcgi_pass 127.0.0.1:9000
example.com is configured to forward request to php files to socket of php80: fastcgi_pass 127.0.0.1:9001
Links
1. Update to php80-php-fpm on Centos 7 https://patrickdomingues.com/2021/05/01/how-to-upgrade-to-php-8-on-cent…
2. Run multiple websites with different php versions in nginx https://www.tecmint.com/run-multiple-websites-with-different-php-versio…
3. Where are config files for remi php repositories? https://stackoverflow.com/questions/59457017/php-7-4-configuration-file…