nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端。
nginx一般是把请求发fastcgi管理进程处理,fascgi管理进程选择cgi子进程处理结果并返回被nginx
本文以php-fpm为例介绍如何使nginx支持PHP
一、编译安装php-fpm
什么是PHP-FPM
PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的,可以在 http://php-fpm.org/download下载得到.
PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用。
新版PHP已经集成php-fpm了,不再是第三方的包了,推荐使用。PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置,比spawn-fcgi具有更多有点,所以被PHP官方收录了。在./configure的时候带 –enable-fpm参数即可开启PHP-FPM,其它参数都是配置php的,具体选项含义可以查看这里。
安装前准备
centos下执行
yum -y install gcc automake autoconf libtool make
yum -y install gcc gcc-c++ glibc
yum -y install libmcrypt-devel mhash-devel libxslt-devel\
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel\
zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2 -devel\
ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel\
krb5 krb5-devel libidn libidn-devel openssl openssl-devel
|
新版php-fpm安装(推荐安装方式)
wget http: //cn2 .php.net /distributions/php-5 .4.7. tar .gz
tar zvxf php-5.4.7. tar .gz
cd php-5.4.7
. /configure --prefix= /usr/local/php -- enable -fpm --with-mcrypt --with-zlib\
-- enable -mbstring --disable-pdo --with-curl --disable-debug --disable-rpath\
-- enable -inline-optimization --with-bz2 --with-zlib -- enable -sockets\
-- enable -sysvsem -- enable -sysvshm -- enable -pcntl -- enable -mbregex\
--with-mhash -- enable -zip --with-pcre-regex --with-mysql\
--with-gd --with-jpeg
make all install
|
旧版手动打补丁php-fpm安装(旧版程序已经没有了,大家新版的吧,这里做个展示)
wget http: //cn2 .php.net /get/php-5 .2.17. tar .gz
wget http: //php-fpm .org /downloads/php-5 .2.17-fpm-0.5.14. diff .gz
tar zvxf php-5.2.17. tar .gz
gzip - cd php-5.2.17-fpm-0.5.14. diff .gz | patch -d php-5.2.17 -p1
cd php-5.2.17
. /configure --prefix= /usr/local/php -with-config- file -path= /usr/local/php/etc \
-with-mysql= /usr/local/mysql \
-with-mysqli= /usr/local/mysql/bin/mysql_config -with-openssl - enable -fpm - enable -mbstring\
-with-freetype- dir -with-jpeg- dir -with-png- dir -with-zlib- dir -with-libxml- dir = /usr - enable -xml\
-with-mhash -with-mcrypt - enable -pcntl - enable -sockets -with-bz2 -with-curl -with-curlwrappers\
- enable -mbregex -with-gd - enable -gd-native-ttf - enable -zip - enable -soap -with-iconv - enable -bcmath\
- enable -shmop - enable -sysvsem - enable -inline-optimization -with-ldap -with-ldap-sasl - enable -pdo\
-with-pdo-mysql
make all install
|
以上两种方式都可以安装php-fpm,安装后内容放在/usr/local/php目录下
以上就完成了php-fpm的安装。
下面是对php-fpm运行用户进行设置
cd /usr/local/php
cp etc /php-fpm .conf.default etc /php-fpm .conf
vi etc /php-fpm .conf
|
修改
user = www-data
group = www-data
二、编译安装nginx
然后按照http://www.nginx.cn/install 安装nginx
三、修改nginx配置文件以支持php-fpm
nginx安装完成后,修改nginx配置文件为,nginx.conf
其中server段增加如下配置,注意标红内容配置,否则会出现No input file specified.错误
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
四、创建测试php文件
创建php文件
在/usr/local/nginx/html下创建index.php文件,输入如下内容
<? echo phpinfo(); ?>
五、启动服务
启动php-fpm和nginx
/usr/local/php/sbin/php-fpm (手动打补丁的启动方式/usr/local/php/sbin/php-fpm start)
sudo /usr/local/nginx/nginx
php-fpm关闭重启见文章结尾
六、浏览器访问
访问http://你的服务器ip/index.php,皆可以见到php信息了。
安装php-fpm时可能遇到的错误:
1. php configure时出错
configure: error: XML configuration could not be found
apt-get install libxml2 libxml2-dev (ubuntu下)
yum -y install libxml2 libxml2-devel(centos下)
2. Please reinstall the BZip2 distribution
wget http://www.bzip.org/1.0.5/bzip2-1.0.5.tar.gz
tar -zxvf bzip2-1.0.5.tar.gz
cd bzip2-1.0.5
make
make install
3. php的配置文件中有一行--with-mysql=/usr。安装的时候提示:
configure: error: Cannot find MySQL header files under yes.
Note that the MySQL client library is not bundled anymore.
这是由于安装mysql时没有安装mysql头文件,或者是路径指定不正确,php找不到mysql的头文件引起的错误提示。
解决方法。
(1.) 查看你的系统有没有安装mysql header
find / -name mysql.h
如果有。请指定--with-mysql=/跟你的正常路径。
如果没有。请看下一步。
(2.)redhat安装
rpm -ivh MySQL-devel-4.1.12-1.i386.rpm
(3.)ubuntu安装
apt-get install libmysqlclient15-dev
(4.)最后一步php的配置选项添加--with-mysql=/usr即可!
4.No input file specified.
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
5. 如果php configure时缺库,可以先安装库(ubuntu下)
sudo apt-get install make bison flex gcc patch autoconf subversion locate
sudo apt-get install libxml2-dev libbz2-dev libpcre3-dev libssl-dev zlib1g-dev libmcrypt-dev libmhash-dev libmhash2 libcurl4-openssl-dev libpq-dev libpq5 libsyck0-dev
6. mcrypt.h not found. Please reinstall libmcrypt
apt-get install libmcrypt-dev
或者
wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz
tar -zxvf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
mkdir -p /usr/local/libmcrytp
./configure prefix=/usr/local/libmcrytp/
make
make install
7. php-fpm 5.4.7 如何关闭 重启?
php 5.4.7 下的php-fpm 不再支持 php-fpm 以前具有的 /usr/local/php/sbin/php-fpm (start|stop|reload)等命令,需要使用信号控制:
master进程可以理解以下信号
INT, TERM 立刻终止 QUIT 平滑终止 USR1 重新打开日志文件 USR2 平滑重载所有worker进程并重新载入配置和二进制模块
示例:
php-fpm 关闭:
kill -INT `cat /usr/local/php/var/run/php-fpm.pid`
php-fpm 重启:
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
查看php-fpm进程数:
ps aux | grep -c php-fpm
相关推荐
docker-compose php7.3.4-fpm+nginx+mysql配置
- 创建额外的sock文件,并在Nginx配置中通过`upstream`模块将请求负载均衡到多个sock文件对应的php-fpm实例上。这种方式可以有效分散负载,提高系统的整体吞吐量。 #### 三、php-fpm参数调优 php-fpm的性能调优...
解决nginx+php-fpm无法上传文件问题需要同时修改nginx配置文件和php.ini配置文件。在nginx配置文件中,我们需要设置client_max_body_size变量以允许上传更大的文件,而在php.ini配置文件中,我们需要设置post_max_...
2. 调整PHP-FPM配置:在php-fpm配置文件中设置`pm.max_requests`参数。该参数定义了一个子进程处理的最大请求数,达到这个数量后,子进程会被销毁,从而释放其占用的内存。例如,将`pm.max_requests`设置为5,意味着...
在互联网服务领域,Nginx、PHP-FPM(PHP FastCGI Process Manager)和 Alternative PHP Cache (APC) 的结合已经成为一种高效的Web服务器配置模式,尤其适用于高流量网站。这种组合能够优化PHP的性能,降低资源消耗,...
安装Nginx服务器,配置Nginx以使用PHP-FPM处理PHP请求。在Nginx配置文件中添加FastCGI的监听设置,指向PHP-FPM监听的端口。 4. **配置PHP-FPM**: 配置PHP-FPM的池设置,如进程数量、内存限制等,确保其能够有效...
php-fpm。放到/etc/init.d/目录。然后执行:chmod a+x php-fpm 然后就可以/etc/init.d/php-fpm start 或者systemctl start php-fpm
在IT行业中,构建高效、可扩展的Web服务是至关重要的,而`Nginx`、`PHP-FPM`和`HTTP Push`技术是实现这一目标的关键组件。本教程将详细介绍如何在`CentOS 5.2`操作系统上,通过源码安装`Nginx`、`PHP-FPM`,并实现`...
在IT领域,尤其是在服务器管理与Web开发中,安装和配置Nginx以及PHP-FPM是常见的操作。这里我们将深入探讨如何在Debian系统上,特别是友善之臂NanoPC-T2这款嵌入式设备上,进行这项工作。 首先,让我们了解Nginx。...
4. **配置Nginx与PHP-FPM通信**:在`nginx.conf`中,添加一个location块,设置对.php文件的处理规则,通过fastcgi_pass指定PHP-FPM监听的端口。 5. **启动服务**:使用`RunHiddenConsole.exe`来启动Nginx和PHP-FPM...
- 配置Nginx以使用PHP-FPM处理PHP请求,编辑`/etc/nginx/nginx.conf`或相应的站点配置文件,添加类似以下内容: ``` location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_...
支持让我们加密SSL配置,自定义nginx配置,用于运行首选项的核心nginx / PHP变量替代,用于本地卷支持的X-Forwarded-For标头和UID映射。 如果您有任何改进或建议,请在GitHub项目页面上打开问题或提出请求。 版本...
**PHP-FPM配置文件路径** PHP-FPM (FastCGI Process Manager) 是PHP的一个组件,负责处理PHP脚本。PHP-FPM的配置文件通常位于PHP安装路径的`etc`目录下,如`/usr/local/php/etc/php-fpm.conf`。然而,根据需求,...
Ubuntu 10.04配置Nginx+PHP-FPM详解 在Ubuntu 10.04中,配置Nginx与PHP-FPM以提升Web服务器的性能和管理效率成为了一种流行的选择。Nginx(发音为engine x)是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/...
PHP-FPM是PHP FastCGI的进程管理器,它允许Web服务器(如Apache或Nginx)通过FastCGI接口与PHP解释器进行通信,从而处理PHP脚本。在Web开发中,PHP通常被用作服务器端编程语言,用于生成动态网页内容。 PHP-FPM的...
该模板使用php-fpm为nginx创建OpenShift资源带有nginx和php-fpm的Pod的DeploymentConfig 适用于php-fpm的BuiltConfig和ImageStream(能够自定义php-fpm版本并包括自定义PHP模块) 用于nginx配置的ConfigMap 为Nginx...
在这个主题中,我们将深入探讨如何利用Zabbix监控Nginx、PHP-FPM和Apache这三种常见的Web服务软件。 1. **Zabbix监控Nginx**: Nginx是一款高性能的HTTP和反向代理服务器,因其高效稳定的特点,被广泛应用。Zabbix...
本文将详细讲解如何安装配置php-fpm,以及如何与Nginx配合,构建一个生产级别的PHP环境。 首先,理解php-fpm的核心概念。PHP-FPM,全称FastCGI Process Manager,是一个用于管理PHP FastCGI进程的工具。它的出现...
5. **配置PHP-FPM**:编辑`/etc/php-fpm.d/www.conf`,确保`listen = /var/run/php-fpm.sock`,并将`listen.owner`和`listen.group`设置为`nginx`用户。 6. **启动与测试**:启动Nginx和PHP-FPM服务,`sudo ...