商城首页欢迎来到中国正版软件门户

您的位置:首页 >nginx之php、php-fpm相关配置

nginx之php、php-fpm相关配置

  发布于2026-05-03 阅读(0)

扫一扫,手机访问

想用nginx支持php项目?先搞定安装这关

要让Nginx顺利跑起PHP项目,第一步自然是安装Nginx本身,这个步骤比较常规,这里就不展开细说了。

咱们重点聊聊PHP和PHP-FPM的安装。这里有个经验之谈:尽量避免使用yum直接安装。为什么?因为通过yum安装后,你很可能找不到安装目录的具体位置,一旦安装过程出现问题,排查和后续调整会变得相当麻烦。

下面是一份经过整理的实操指南,供你参考:

1、下载PHP源码包

官方下载地址:http://www.php.net/downloads.php

2、安装PHP

tar -xvf php-5.5.13.tar.bz2
cd php-5.5.13
./configure --prefix=/usr/local/php --with-config-file-path=/etc --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-opcache --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-gettext --enable-mbstring --with-iconv --with-mcrypt --with-mhash --with-openssl --enable-bcmath --enable-soap --with-libxml-dir --enable-pcntl --enable-shmop --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-sockets --with-curl --with-zlib --enable-zip --with-bz2 --with-readline --without-sqlite3 --without-pdo-sqlite --with-pear

执行配置命令时,可能会遇到一些依赖缺失的错误,别慌,逐一解决就好:

错误: configure error: xml2-config not found.

解决办法: 执行命令安装依赖:sudo yum install libxml2-devel。安装后可以通过 find / -name "xml2-config" 来确认是否安装成功。

错误: configure error: Cannot find OpenSSL's

解决办法: 安装OpenSSL开发包并创建软链接:

yum install openssl openssl-devel
ln -s /usr/lib64/libssl.so /usr/lib/

错误: configure: error: Please reinstall the BZip2 distribution

解决办法: yum install bzip2 bzip2-devel

错误: configure: error: Please reinstall the libcurl distribution -easy.h should be in /include/curl/

解决办法: yum -y install curl-devel

错误: configure: error: mcrypt.h not found. Please reinstall libmcrypt.

解决办法: sudo yum install libmcrypt libmcrypt-devel mcrypt mhash

错误: configure: error: Please reinstall readline - I cannot find readline.h

解决办法: sudo yum install readline-devel

解决完所有依赖问题后,就可以开始编译安装了:

make
make install

3、添加PHP命令到环境变量

编辑配置文件:vim /etc/profile

在文件末尾加入以下两行:

PATH=$PATH:/usr/local/php/bin
export PATH

要使改动立即生效,执行:. /etc/profilesource /etc/profile

可以分别通过以下命令查看环境变量和PHP版本,以验证是否成功:

echo $PATH
php -v

成功的话,php -v会输出类似如下信息:

PHP 5.5.13 (cli) (built: Jun 20 2014 11:11:26)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies

4、配置并启动php-fpm

cd /usr/local/php/etc
cp php-fpm.conf.default php-fpm.conf
# 启动php-fpm
sudo /usr/local/php/sbin/php-fpm

5、修改Nginx配置文件以支持PHP

编辑Nginx的站点配置文件(例如:/etc/nginx/conf.d/default.conf),找到处理PHP请求的部分:

location / {
    root web根目录;
    index index.html index.htm index.php;
}

location ~ \.php$ {
    root html;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME web根目录$fastcgi_script_name;
    include fastcgi_params;
}

请注意: 务必将上述配置中的“web根目录”替换为你项目实际的根目录路径。

修改完成后,重启Nginx使配置生效:/etc/init.d/nginx restart

6、测试PHP解析

最后,在你的Web根目录下创建一个index.php文件,写入。然后在浏览器中访问你的服务器地址,如果能看到详细的PHP信息页面,恭喜你,Nginx已经成功支持PHP了。

本文转载于:https://blog.csdn.net/u012599988/article/details/49616121 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注