在安装之后发现并不能解析 php 文件,查询错误信息得到结论:php-fpm 未安装。

检查 php-fpm 是否启动 netstat -ant | grep 9000 , 检查可执行文件 whereis php-fpm ,确认未安装。

之前也从来没有单独安装过 php-fpm ,是不是高版本不需要 php-fpm 了呢?查了一下,并不是,7.* 版本仍然需要并且会自动安装 php-fpm 。

那么就应该是安装 php 的过程中出了问题。

找到源文件目录 /usr/local/src/php-7.3.3 ,这是我下载解压后的目录地址。重新找了一个对应版本的安装教程,

一、安装一些依赖包:

yum -y install libxml2
yum -y install libxml2-devel
yum -y install openssl
yum -y install openssl-devel
yum -y install curl
yum -y install curl-devel
yum -y install libjpeg
yum -y install libjpeg-devel
yum -y install libpng
yum -y install libpng-devel
yum -y install freetype
yum -y install freetype-devel
yum -y install pcre
yum -y install pcre-devel
yum -y install libxslt
yum -y install libxslt-devel
yum -y install bzip2
yum -y install bzip2-devel

二、编译:

./configure --prefix=/usr/local/php --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip

注意:复制时要清楚每行末尾的换行符

--prefix 参数表示新版 php 安装目录,可自定义。--with-apxs2 参数表示 apache 2.x 的 apxs 服务位置,我在参数里没加,但也编译成功了。

如果 php 安装过程中没有自动配置的话,安装成功后需要配置 libphp7.so 模块的位置,并重启 Apache。若重启 Apache 失败,提示缺少 libphp7.so ,可以考虑重新编译,并添加参数 --with-apxs2=/path/to/httpd/bin/apxs

错误解决:

configure: error: Please reinstall the libzip distribution

安装 libzip:

yum -y install libzip
yum -y install libzip-devel

其他缺失的依赖库亦可以如此操作。

error: off_t undefined; check your library configuration
添加路径设置
# 新建文件并编辑
vim /etc/ld.so.conf.d/local.conf
# 插入 /usr/local/lib
ldconfig -v
#或
echo '/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64'>>/etc/ld.so.conf&&ldconfig -v
# `/etc/ld.so.conf` 默认包含 `/etc/ld.so.conf.d` 下的所有文件。
unrecognized options: --enable-gd-native-ttf
在 7.2 之后可能会报警告,可以直接忽略或者去掉。

make test

报错:

...
ext/iconv/.libs/iconv.o: In function `php_iconv_stream_filter_append_bucket':
/alidata/server/php-7.3.15c/ext/iconv/iconv.c:2651: undefined reference to `libiconv'
/alidata/server/php-7.3.15c/ext/iconv/iconv.c:2724: undefined reference to `libiconv'
/alidata/server/php-7.3.15c/ext/iconv/iconv.c:2723: undefined reference to `libiconv'
ext/iconv/.libs/iconv.o: In function `php_iconv_stream_filter_ctor':
/alidata/server/php-7.3.15c/ext/iconv/iconv.c:2607: undefined reference to `libiconv_open'
ext/xmlrpc/libxmlrpc/.libs/encodings.o: In function `convert':
/alidata/server/php-7.3.15c/ext/xmlrpc/libxmlrpc/encodings.c:65: undefined reference to `libiconv_open'
/alidata/server/php-7.3.15c/ext/xmlrpc/libxmlrpc/encodings.c:72: undefined reference to `libiconv'
/alidata/server/php-7.3.15c/ext/xmlrpc/libxmlrpc/encodings.c:88: undefined reference to `libiconv_close'
collect2: ld returned 1 exit status
make: *** [sapi/cli/php] Error 1

大概内容就是缺少 libiconv ,尝试用 yum 安装,找不到。

下载编译版本安装:


# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz
# tar -zxvf libiconv-1.16.1.tar.gz
# cd libiconv-1.16.1
# ./configure --prefix=/usr/local/libiconv
# make
# make install

出现其他测试 bug

=====================================================================
FAILED TEST SUMMARY
---------------------------------------------------------------------
Bug #64267 (CURLOPT_INFILE doesn't allow reset) [ext/curl/tests/bug64267.phpt]
Bug #71523 (Copied handle with new option CURLOPT_HTTPHEADER crashes while curl_multi_exec) [ext/curl/tests/bug71523.phpt]
Bug #77269 (Potential unsigned underflow in gdImageScale) [ext/gd/tests/bug77269.phpt]
Bug #77272 (imagescale() may return image resource on failure) [ext/gd/tests/bug77272.phpt]
Bug #77479 (imagewbmp() segfaults with very large image) [ext/gd/tests/bug77479.phpt]
Bug #65538: SSL context "cafile" disallows URL stream wrappers [ext/openssl/tests/bug65538_002.phpt]
stream context tcp_nodelay fopen [ext/standard/tests/streams/stream_context_tcp_nodelay_fopen.phpt]
=====================================================================

忽略失败提示,继续。

make

错误解决:

zipconf.h:No such file or directory
从源文件中复制该文件到引用文件夹
cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h

make install

提示信息如下:

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20180731/
Installing PHP CLI binary:        /usr/local/php/bin/
Installing PHP CLI man page:      /usr/local/php/php/man/man1/
Installing PHP FPM binary:        /usr/local/php/sbin/
Installing PHP FPM defconfig:     /usr/local/php/etc/
Installing PHP FPM man page:      /usr/local/php/php/man/man8/
Installing PHP FPM status page:   /usr/local/php/php/php/fpm/
Installing phpdbg binary:         /usr/local/php/bin/
Installing phpdbg man page:       /usr/local/php/php/man/man1/
Installing PHP CGI binary:        /usr/local/php/bin/
Installing PHP CGI man page:      /usr/local/php/php/man/man1/
Installing build environment:     /usr/local/php/lib/php/build/
Installing header files:          /usr/local/php/include/php/
Installing helper programs:       /usr/local/php/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php/lib/php/
[PEAR] Archive_Tar    - already installed: 1.4.4
[PEAR] Console_Getopt - already installed: 1.4.1
[PEAR] Structures_Graph- already installed: 1.1.1
[PEAR] XML_Util       - already installed: 1.4.3
[PEAR] PEAR           - already installed: 1.10.7
Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/usr/local/src/php-7.3.3/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers:           /usr/local/php/include/php/ext/pdo/

找一下 php-fpm ,有了。

[root@iZuf php-7.3.3]# whereis php-fpm
php-fpm: /usr/local/php/sbin/php-fpm

用 service 或者 systemctl 启动,提示找不到服务,后面可以将其转化成服务,也可以直接启用

php 安装后的配置( php 和 php-fpm )

拷贝配置文件

cp php.ini-development /usr/local/php/lib/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp sapi/fpm/php-fpm /usr/local/bin

然后设置 php.ini,使用: vim /usr/local/php/lib/php.ini 打开php配置文件找到 cgi.fix_pathinfo 配置项,这一项默认被注释并且值为 1,根据官方文档的说明,这里为了当文件不存在时,阻止 Nginx 将请求发送到后端的 PHP-FPM 模块,从而避免恶意脚本注入的攻击,所以此项应该去掉注释并设置为 0。

设置完毕保存并退出

另外注意一个地方就是 php.ini 配置文件的位置可以在编译前配置参数中设置,编译参数可以写成:--with-config-file-path=/usr/local/php 这样的话php就回去指定的目录下读取 php.ini 配置文件,如果不加这个参数默认位置就是 php 安装目录下的 lib 目录,具体也可以在 phpinfo() 输出界面查看,如果 php.ini 放到其他位置,php 读取不到,那么所有的配置修改后都是不生效的,这点要注意

此时首先应该创建web用户:

groupadd www-data
useradd -g www-data www-data

vim /usr/local/php/etc/php-fpm.conf,翻到 php-fpm.conf 最后一行会发现如下内容(如果编译时添加 --prefix 选项以下位置会自动补全,默认是下面是空的,要注意):

配置php-fpm.conf

这里引入了 php-fpm.d 目录下所有的 conf 配置文件,但是NONE需要修改为我们的实际目录 /usr/local/php ,当然我这里编译好是下面这样的:

配置后的额

默认情况下 etc/php-fpm.d/ 下有一个名为 www.conf.defalut 的配置用户的文件,执行下面命令复制一个新文件并且打开:

cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
vim /usr/local/php/etc/php-fpm.d/www.conf

默认 user 和 group 的设置为 nobody ,将其改为 www-data :

配置 php-fpm 用户和用户组
 
修改完成之后,保存并退出,然后执行以下命令启动 php-fpm 服务:

/usr/local/bin/php-fpm
# 关闭可用ps + kill 指令组合

启动完毕之后,php-fpm 服务默认使用 9000 端口,使用 netstat -tln | grep 9000 可以查看端口使用情况:

查看9000端口占用情况

9000 端口正常使用,说明 php-fpm 服务启动成功

然后执行 vim /usr/local/nginx/nginx.conf 编辑 nginx 配置文件,具体路径根据实际的 nginx.conf 配置文件位置编辑,下面主要修改 nginx 的 server {} 配置块中的内容,修改 location 块,追加 index.php 让 nginx 服务器默认支持 index.php 为首页:

默认索引文件名

然后配置 php 请求被传送到后端的 php-fpm 模块,默认情况下php配置块是被注释的,此时去掉注释并修改为以下内容:

php请求传递配置

这里面很多都是默认的,root 是配置 php 程序放置的根目录,主要修改的就是 fastcgi_param 中的 /scripts 为 $document_root

修改完上面的,回到 nginx.conf 第一行,默认是 #user nobody; 这里要去掉注释改为 user www-data; 或者 user www-data www-data; 表示 nginx 服务器的权限为 www-data

修改完这些保存并退出,然后重启nginx:

/usr/local/nginx/nginx -s stop
/usr/local/nginx/nginx 

接下来编辑一个测试的php程序,在nginx下的html目录下创建test.php文件,打印一下php配置:

<?php
    phpinfo();
?>

然后打开浏览器输入对应的地址进行访问,看到输出页面,说明 nginx 和 php 都配置成功了:
phpinfo 输出内容

后部分配置参考:freeweb - cnblog

文章目录