Home >Backend Development >PHP Tutorial >Detailed implementation of PHP multi-version coexistence solution connected to Apache under CentOS, _PHP tutorial
Integrating php-fpm running in fastCGI mode under apache. There seems to be very little relevant material on the Internet, even There is also little material in English. As long as it is php-fpm, it is basically paired with nginx. I checked a lot of relevant information and wrote this article to summarize it.
There are multiple fastCGI support solutions under apache: at least mod_fcgi, mod_fastcgi (git), mod_proxy_fcgi, etc. Both modules are a bit old, especially mod_fastcgi, which has not been updated since 2007. Let’s ignore it. In fact, it has not been used. The mod_proxy_fcgi module is officially introduced in version 2.4 of httpd, with a simple one-line ProxyPassMatch directive.
mod_fcgi
The mod_fcgi module itself is used for fastCGI process management. Using it does not require php-fpm to manage the process. Core configuration parameters
LoadModule fcgid_module modules/mod_fcgid.so <VirtualHost *:80> DocumentRoot "/var/www/html/site_1" ServerName "www.yourhost.com" DirectoryIndex index.html index.php #php.ini的存放目录,Linux下通常不需要 #FcgidInitialEnv PHPRC "D:/php" # 设置PHP_FCGI_MAX_REQUESTS大于或等于FcgidMaxRequestsPerProcess,防止php-cgi进程在处理完所有请求前退出 FcgidInitialEnv PHP_FCGI_MAX_REQUESTS 1000 #php-cgi每个进程的最大请求数 FcgidMaxRequestsPerProcess 1000 #php-cgi最大的进程数 FcgidMaxProcesses 3 #最大执行时间 FcgidIOTimeout 600 FcgidIdleTimeout 600 #php-cgi的路径 FcgidWrapper /usr/local/php7/bin/php-cgi .php AddHandler fcgid-script .php FcgidInitialEnv PHP_FCGI_MAX_REQUESTS 1000 <Directory "/var/www/html/site_1"> Options +ExecCGI </Directory> </VirtualHost>
Several Fcgid* instructions are used for optimization. Here are only examples. The values must be adjusted according to the actual situation. For details, please refer to mod_fcgi official documentation
Several features of using mod_fcgid
The php-fgi process is started and managed by the apache module, and there is no need to configure php-fpm
In the php-cig process, it runs as the apache user. The permissions of the files written by the php program are owned by the apache user (unlike the files written under php-fpm, which are owned by the php-fpm user, and the default is nobody). In this way, in terms of directory permission management The consistency is higher.
mod_fastcgi
Although Apache 2.2 is used under CentOS 6.x, fortunately someone has successfully transplanted it: https://github.com/ceph/mod-proxy-fcgi We can use it directly; even more fortunately, it has entered the epel source, You can install it directly with yum; if you don’t want to match the epel source, you can also directly download the rpm package and install it (example http://mirrors.ustc.edu.cn/epel/6/x86_64/)
Of course, you can recompile and install apache 2.4, so that mod_proxy_fcgi can be used directly, but here we still keep the original version unchanged to save the workload of compilation.
Refer to the mod_proxy_fcgi official document to integrate the configuration instructions of php-fpm
ProxyPassMatch "^/myapp/.*.php(/.*)?$" "fcgi://localhost:9000/var/www/"
The syntax is very simple, similar to configuring a reverse proxy, and can be modified according to actual needs. In fact, the syntax is consistent with the mod_proxy module, except that the http protocol is changed to the fcig protocol.
The above is fastCGI running in apache integrated php-fpm mode. Next, we need to make configuration modifications to the php installed by yum.
The php configuration file /etc/httpd/conf.d/php.conf installed by yum, which contains the following line
AddHandler php5-script .php
We need to enable different php for different sites. The above line assigns the global .php file to the php module for processing. We comment out this line. Instead, enable different php running modes on each site.
The above is how to handle it.
[Known issue]: Naked directory address forwarding
There is a troublesome problem that has not been solved, it feels a bit like a module bug:
For a directory configured with DirectoryIndex index.php, if its subdirectory does not have index.php, the above ProxyPassMatch will still perform fastCGI forwarding. At this time, you will see a 404 response from php-fpm instead of a 403 page from apache. But the previous rule does not forward the URL of this bare directory, so it feels like a bug
Furthermore, for the directories matched by ProxyPassMatch, the apache automatic indexing function fails. (Of course, it doesn’t matter if you don’t turn on autoindex. It’s usually not turned on in a production environment)
Others, there don’t seem to be any serious consequences, or I haven’t realized it yet (?).
Solution: Place an index.html in each directory to prevent fpm-php from processing empty requests