CentOS下与Apache连接的PHP多版本共存方案实现详解,
CentOS下与Apache连接的PHP多版本共存方案实现详解,
在apache下整合fastCGI模式运行的php-fpm,似乎网上很少相关材料,就连英文版材料也少。只要是php-fpm,基本上都是与nginx搭配。查了一大批相关资料,写本文总结一下。
apache下有多个fastCGI的支持方案:至少有mod_fcgi、mod_fastcgi(git)、mod_proxy_fcgi等。这两个模块都有点老,尤其mod_fastcgi自从2007年以来就没有更新,略掉不谈,事实上没用过用。mod_proxy_fcgi模块是httpd 2.4+的版本正式引入,通过简洁的一行 ProxyPassMatch 指令即可。
mod_fcgi
mod_fcgi模块本身是做fastCGI进程管理的,使用它就不需要使用php-fpm管理进程了。核心配置参数
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>
几个Fcgid*指令,是优化之用,这里仅示例,要按实际情况调整数值。具体参看mod_fcgi官方文档
使用mod_fcgid的几个特点
php-fgi进程是由apache模块启动并管理,不需要配置php-fpm
在php-cig进程以apache用户身份运行,php程序写的文件,其权限为apache用户(而不像php-fpm下写文件为php-fpm用户所有,默认是nobody),这样在目录权限管理方面一致性高些。
mod_fastcgi
虽然CentOS 6.x下是apache 2.2,但所幸已经有人成功移植: https://github.com/ceph/mod-proxy-fcgi 我们可以直接使用;更幸运的是它已经进入epel源,直接yum安装即可;不想匹配epel源的,直接下载rpm包安装也可以(示例 http://mirrors.ustc.edu.cn/epel/6/x86_64/)
当然可以重新编译安装apache 2.4, 这样直接有mod_proxy_fcgi可以使用,但这里还是保持原版本不变,省掉编译的工作量。
参考mod_proxy_fcgi官方文档,整合php-fpm的配置指令
ProxyPassMatch "^/myapp/.*\.php(/.*)?$" "fcgi://localhost:9000/var/www/"
语法很简单,跟配置反向代理类似,可以按实际需要做修改。事实上与mod_proxy模块语法一致的,不同处是将http协议改成fcig协议。
以上是apache整合php-fpm模式运行的fastCGI,接下来要对yum安装的php做下配置修改。
yum安装的php配置文件 /etc/httpd/conf.d/php.conf ,其中有如下一行
AddHandler php5-script .php
我们要对不同的站点启用不同的php,上面一行是对全局的.php文件分配给php模块处理,我们把这一行注释掉。而是在每个站点启用不同的php运行模式。
以上即是处理方式。
[已知问题]:裸目录地址转发
有一个困扰的问题没有解决,感觉有点像模块bug:
对于配置了DirectoryIndex index.php的目录,如果其子目录没有index.php,上述ProxyPassMatch还是会做fastCGI转发,这时会看到php-fpm的404响应,而不是apache的响应403页面。但前面的规则并不转发这裸空目录的url,所以感觉像bug
再者就是,对于ProxyPassMatch匹配的目录,apache自动索引功能失效。(当然如果不开启autoindex就无所谓了。生产环境下通常不开启的)
其它,似乎也没有什么严重后果,或者我没还意识到(?)。
解决方法:每个目录下,都放置一个index.html,避免fpm-php处理空请求
您可能感兴趣的文章:
- Linux系统中设置多版本PHP共存配合Nginx服务器使用

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.
