Centos Nginx Multi-php Version

Posted by ciciep on Fri, 07 Jun 2019 19:57:19 +0200

Record the process of an installation
System: centos
php version: 5.4 5.6
Purpose: Install two php versions

1: First install the first version of php5.4 via lnmp specific command lnum has specific operations on the official network
2: Modify the configuration of php-fpm
Stop running php-fpm first, check pid of php-fpm first

[root@ etc]# ps aux | grep  php-fpm
root     25903  0.0  0.2 266664  5508 ?        Ss   11:35   0:00 php-fpm: master process (/usr/local/etc/php-fpm-5.4.conf)
www      25904  0.0  0.2 266664  4848 ?        S    11:35   0:00 php-fpm: pool www
www      25905  0.0  0.2 266664  4848 ?        S    11:35   0:00 php-fpm: pool www
www      25906  0.0  0.2 266664  4848 ?        S    11:35   0:00 php-fpm: pool www
www      25907  0.0  0.2 266664  4848 ?        S    11:35   0:00 php-fpm: pool www
www      25908  0.0  0.2 266664  4848 ?        S    11:35   0:00 php-fpm: pool www
www      25909  0.0  0.2 266664  4848 ?        S    11:35   0:00 php-fpm: pool www
www      25910  0.0  0.2 266664  4848 ?        S    11:35   0:00 php-fpm: pool www
www      25911  0.0  0.2 266664  4848 ?        S    11:35   0:00 php-fpm: pool www
www      25912  0.0  0.2 266664  4848 ?        S    11:35   0:00 php-fpm: pool www
www      25913  0.0  0.2 266664  4848 ?        S    11:35   0:00 php-fpm: pool www
root     25915  0.0  0.0 112648   972 pts/0    R+   11:35   0:00 grep --color=auto php-fpm

Look at the command: PS aux | grep php-fpm output shows that the current php-fpm runs a pid of 25903, then closes the current process first by ending the command.
End command kill-INT 25903:
After running, review the process again

[root@iZwz99zpj9k5noj0vtkozjZ etc]# ps aux | grep  php-fpm
root     26059  0.0  0.0 112648   972 pts/0    R+   13:54   0:00 grep --color=auto php-fpm

Php-fpm is indeed turned off.Then we found the configuration file location for php-fpm and started configuring php-fpm under 1.The results after configuration are as follows

[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log //Modified Places
log_level = notice

[www]
listen = 9001 //Modified Places
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 20
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 20
request_terminate_timeout = 100
request_slowlog_timeout = 0
slowlog = var/log/php_5.4_slow.log //Modified Places

After the modification, it's time to restart fpm, but I changed the name of the profile above to distinguish between the two versions of php-fpm.conf and moved it under / usr/local/etc for easy administration.If you install PHP through lnmp, LNMP may have already helped you install php-fpm as a service, so that you can make php-fpm read a new configuration file by default by simply modifying the service's configuration file, which is located in
/etc/rc.d/init.d/php-fpm

prefix=/usr/local/php
exec_prefix=${prefix}
conf=/usr/local/etc //Newly added

php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${conf}/php-fpm-5.4.conf //modify
php_fpm_PID=${prefix}/var/run/php-fpm.pid


php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"

After that, in order to distinguish the two PHP versions, I changed the name of this file to php-fpm-5.4. Okay, now let's start a new php-fpm, using the command service php-fpm-5.4 start

[root@iZwz99zpj9k5noj0vtkozjZ init.d]# service php-fpm-5.4 start
Starting php-fpm  done
[root@iZwz99zpj9k5noj0vtkozjZ init.d]# ps aux | grep  php-fpm
root     26114  0.0  0.2 163072  5508 ?        Ss   14:06   0:00 php-fpm: master process (/usr/local/etc/php-fpm-5.4.conf)
www      26115  0.0  0.2 163072  4848 ?        S    14:06   0:00 php-fpm: pool www
www      26116  0.0  0.2 163072  4848 ?        S    14:06   0:00 php-fpm: pool www
www      26117  0.0  0.2 163072  4848 ?        S    14:06   0:00 php-fpm: pool www
www      26118  0.0  0.2 163072  4848 ?        S    14:06   0:00 php-fpm: pool www
www      26119  0.0  0.2 163072  4848 ?        S    14:06   0:00 php-fpm: pool www
www      26120  0.0  0.2 163072  4848 ?        S    14:06   0:00 php-fpm: pool www
www      26121  0.0  0.2 163072  4848 ?        S    14:06   0:00 php-fpm: pool www
www      26122  0.0  0.2 163072  4852 ?        S    14:06   0:00 php-fpm: pool www
www      26123  0.0  0.2 163072  4852 ?        S    14:06   0:00 php-fpm: pool www
www      26124  0.0  0.2 163072  4852 ?        S    14:06   0:00 php-fpm: pool www
root     26128  0.0  0.0 112648   972 pts/0    R+   14:07   0:00 grep --color=auto php-fpm

After startup, we checked the next process, it seems that the startup was successful, but at this time when we visit the php file, we will find that the error reported is 502. This is because we changed the port of fpm from the default 9000 to 9001, but nginx reads 9000. Of course, this will happen in 502. Now we will modify the configuration of nginx to 9001 to see if it can be changed to 9001.Meritorious access.

server {
        listen  8080;
        server_name localhost;
        charset utf-8;
            root /data/wwwroot/lol;


        location / {
                index  index.shtml index.php index.html index.htm ;
                if (!-e $request_filename) {
                        rewrite  ^(.*)$  /index.php?s=$1  last;
                        break;
                }
        }

        location ~ .+\.php($|/) {
                fastcgi_pass   127.0.0.1:9001;//Modify to9001
                fastcgi_connect_timeout      180;
                fastcgi_read_timeout            600;
                fastcgi_send_timeout            600;
                fastcgi_index  index.php;
                include        fastcgi_params;

                set $path_info "";
                set $real_script_name $fastcgi_script_name;
                if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
                        set $real_script_name $1;
                        set $path_info $2;
                }
                fastcgi_param  SCRIPT_FILENAME  $document_root$real_script_name;
                fastcgi_param SCRIPT_NAME $real_script_name;
                fastcgi_param PATH_INFO $path_info;

        }

        error_log  /data/wwwlogs/lol_error.log;
        access_log  /home/wwwlogs/lol_access.log;
}

If the configuration is okay, you can see the phpinfo information

3: Install another version of php, here I want to install the php5.6 version, directly download the code from the official website to install, specific installation
4: Once installed, we need to create another service to configure version 5.6 of php-fpm, copy a copy of the 5.4 service and rename it php-fpm-5.6 with the command cp/etc/rc.d/init.d/php-fpm-5.4/etc/rc.d/init.d/php-fpm-5.6, and then modify the file configuration inside

prefix=/usr/local/php5.6
exec_prefix=${prefix}
conf=/usr/local/etc

php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${conf}/php-fpm-5.6.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid

Then copy a copy of the fpm configuration file command as

[root@iZwz99zpj9k5noj0vtkozjZ init.d]# cd /usr/local/etc
[root@iZwz99zpj9k5noj0vtkozjZ etc]# ls
php-fpm-5.4.conf  php-fpm-5.4.conf.bak
[root@iZwz99zpj9k5noj0vtkozjZ etc]# cp php-fpm-5.4.conf php-fpm-5.6.conf
[root@iZwz99zpj9k5noj0vtkozjZ etc]# ls
php-fpm-5.4.conf  php-fpm-5.4.conf.bak  php-fpm-5.6.conf
[root@iZwz99zpj9k5noj0vtkozjZ etc]# 

Modify Configuration

[global]
pid = /usr/local/php5.6/var/run/php-fpm.pid
error_log = /usr/local/php5.6/var/log/php-fpm.log
log_level = notice

[www]
listen = 9000
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 20
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 20
request_terminate_timeout = 100
request_slowlog_timeout = 0 
slowlog = var/log/php_5.6_slow.log

Start Server

[root@iZwz99zpj9k5noj0vtkozjZ etc]# service php-fpm-5.6 start
Starting php-fpm  done
[root@iZwz99zpj9k5noj0vtkozjZ etc]# ps aux|grep php
root     26329  0.0  0.2 163072  5500 ?        Ss   14:37   0:00 php-fpm: master process (/usr/local/etc/php-fpm-5.4.conf)
www      26330  0.0  0.3 163072  7088 ?        S    14:37   0:00 php-fpm: pool www
www      26331  0.0  0.3 163072  5928 ?        S    14:37   0:00 php-fpm: pool www
www      26332  0.0  0.3 163072  5928 ?        S    14:37   0:00 php-fpm: pool www
www      26333  0.0  0.3 163072  7088 ?        S    14:37   0:00 php-fpm: pool www
www      26334  0.0  0.3 163072  5928 ?        S    14:37   0:00 php-fpm: pool www
www      26335  0.0  0.2 163072  4844 ?        S    14:37   0:00 php-fpm: pool www
www      26336  0.0  0.2 163072  5064 ?        S    14:37   0:00 php-fpm: pool www
www      26337  0.0  0.3 163072  5916 ?        S    14:37   0:00 php-fpm: pool www
www      26338  0.0  0.3 163072  5932 ?        S    14:37   0:00 php-fpm: pool www
www      26339  0.0  0.3 163072  7088 ?        S    14:37   0:00 php-fpm: pool www
root     28356  0.0  0.2 121740  5128 ?        Ss   15:39   0:00 php-fpm: master process (/usr/local/etc/php-fpm-5.6.conf)
www      28357  0.0  0.2 121740  4580 ?        S    15:39   0:00 php-fpm: pool www
www      28358  0.0  0.2 121740  4580 ?        S    15:39   0:00 php-fpm: pool www
www      28359  0.0  0.2 121740  4580 ?        S    15:39   0:00 php-fpm: pool www
www      28360  0.0  0.2 121740  4580 ?        S    15:39   0:00 php-fpm: pool www
www      28361  0.0  0.2 121740  4584 ?        S    15:39   0:00 php-fpm: pool www
www      28362  0.0  0.2 121740  4584 ?        S    15:39   0:00 php-fpm: pool www
www      28363  0.0  0.2 121740  4584 ?        S    15:39   0:00 php-fpm: pool www
www      28364  0.0  0.2 121740  4584 ?        S    15:39   0:00 php-fpm: pool www
www      28365  0.0  0.2 121740  4584 ?        S    15:39   0:00 php-fpm: pool www
www      28366  0.0  0.2 121740  4584 ?        S    15:39   0:00 php-fpm: pool www
root     28368  0.0  0.0 112648   972 pts/0    R+   15:39   0:00 grep --color=auto php

View the status of the next port

[root@iZwz99zpj9k5noj0vtkozjZ etc]# netstat -atpn
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:9000            0.0.0.0:*               LISTEN      28356/php-fpm: mast 
tcp        0      0 0.0.0.0:9001            0.0.0.0:*               LISTEN      26329/php-fpm: mast 
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      31398/mysqld        
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      26576/nginx: master 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      26576/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      3089/sshd           
tcp        0     52 172.18.42.27:22         14.223.93.183:5727      ESTABLISHED 3686/sshd: root@pts 
tcp        0      0 172.18.42.27:34968      106.11.68.13:80         ESTABLISHED 957/AliYunDun  

This allows multiple php versions of different domain names to coexist

Topics: PHP Nginx CentOS network