Centos7 multi version PHP deployment, PHP FPM and nginx configuration



The captcha components of some yii2 projects of the company do not display pictures. After investigation, it is found that the original is due to the PHP version upgrade on the test environment a while ago. originalYii 2.0.13It will be used before version\yii\base\ObjectClass, while object is inPHP 7.2After that, it will be changed into a reserved keyword, and an error will be reported as a class name. For specific information, please refer to the document: API document of Yii 2.0. Well, when you find the problem, it’s time to solve it.

Installing multiple versions of PHP

Currently, centos7 is used on the company’s server, and PHP 7.2.30 has been installed using yarn, so I only need to install a lower version of PHP. To install PHP with yarn, please refer to my previous article: laravel project deployment, PHP FPM and nginx configuration, which will not be discussed here. Note that the installation does not affect the previous PHP. If necessary, you can back up the relevant configuration of PHP and nginx.

Let php-fpm use UNIX socket

By default,php-fpm.confinlistenThe default configuration for is127.0.0.1:9000。 Because I want to use multiple versions herePHP, so it needs to be used insteadUnixSocket.
First find the PHP FPM configuration file:

ps -ef | grep php

As you can see, I have opened two versions of PHP FPM here:

root     13124     1  0 18:33 ?        00:00:00 php-fpm: master process (/etc/opt/remi/php70/php-fpm.conf)
root     28862     1  0 11:31 ?        00:00:00 php-fpm: master process (/etc/opt/remi/php72/php-fpm.conf)

Enter/etc/opt/remi/php70Folder, you can find the relevant configuration of this version of PHP:

drwxr-xr-x. 2 root root     6 Feb 20  2019 opt
drwxr-xr-x. 2 root root  4096 Apr 26 16:39 php.d
-rw-r--r--. 1 root root  4103 Apr 14 16:23 php-fpm.conf
drwxr-xr-x. 2 root root    40 Apr 26 18:29 php-fpm.d
-rw-r--r--. 1 root root 60621 Apr 26 18:32 php.ini
drwxr-xr-x. 2 root root     6 Feb 20  2019 pki
drwxr-xr-x. 5 root root    49 Apr 26 16:19 pm
drwxr-xr-x. 2 root root     6 Feb 20  2019 skel
drwxr-xr-x. 2 root root    20 Apr 26 18:04 sysconfig
drwxr-xr-x. 4 root root    36 Apr 26 16:19 X11
drwxr-xr-x. 3 root root    22 Apr 26 16:19 xdg
drwxr-xr-x. 2 root root     6 Feb 20  2019 xinetd.d

The PHP FPM configuration is in thephp-fpm.dUnder folderwww.confThe contents are as follows:

listen =
;listen.owner = nobody
;listen.group = nobody
;listen.mode = 0660

holdlistenChange it to the name of the sock file. The result is as follows: (I name it here/tmp/php70-php-fpm.sock

listen = /tmp/php70-php-fpm.sock
listen.owner = www
listen.group = www
listen.mode = 0666

Change nginx configuration

First find the configuration file of nginx:

ps -ef | grep nginx

The results are as follows:

root     13114     1  0 18:32 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

see file/usr/local/nginx/conf/nginx.confAfter that, we found that the configuration file of each project on our server is/usr/local/nginx/conf/vhost/xxx.conf, change the configuration after opening, and set thefastcgi_passSet the port to the socket we set earlier, as follows:

location ~ [^/]\.php(/|$)
    try_files $uri =404;
    fastcgi_ pass  unix:/tmp/php70-php- fpm.sock ; (change here)
    fastcgi_index index.php;
    include fastcgi.conf;

Next, just restart PHP FPM and nginx

systemctl restart php70-php-fpm.service
systemctl restart nginx.service

Php-fpm did not generate a socket file

Open the web page and find an error 502. After checking the nginx log, it is found that there is a line of error:

connect() to unix:/tmp/php70-php-fpm.sock failed (2: No such file or directory) while connecting to upstream

see/tmpFolder, found nophp70-php-fpm.sockDocuments.
lookupphp70-php-fpm.serviceto configure:

find / -name "php70-php-fpm.service"

openphp70-php-fpm.service, found thatPrivateTmpSet the item to true and change it to false

Description=The PHP FastCGI Process Manager
After=syslog.target network.target

ExecStart=/opt/remi/php70/root/usr/sbin/php-fpm --nodaemonize
ExecReload=/bin/kill -USR2 $MAINPID


Then remember to restart php-fpm again and see that it has been generated/tmp/php70-php-fpm.sockDocuments. If not, you can refer to the article “php-fpm does not generate socket files” to see where the problem lies.

If an error is found:

connect() to unix:/tmp/php70-php-fpm.sock failed (13: Permission denied) while connecting to upstream

This may be due to a user / user group permission issue, which can be changedPHP-FPMIn the configuration filelisten.ownerandlisten.group