web开发环境搭建(4) CentOS8.3编译安装php7.4.20


准备工作

1、删除centos自带的php

[root@localhost src]# rpm -qa|grep php
php-process-5.4.16-42.el7.x86_64
php-pear-1.9.4-21.el7.noarch
php-common-5.4.16-42.el7.x86_64
php-xml-5.4.16-42.el7.x86_64
php-devel-5.4.16-42.el7.x86_64
php-cli-5.4.16-42.el7.x86_64
[root@localhost src]# rpm -e php-devel-5.4.16-42.el7.x86_64
如果有的话,请一个一个的删除完

2、安装相关库和工具

yum -y install gcc-c++ autoconf curl curl-devel openssl openssl-devel sqlite-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 e2fsprogs e2fsprogs-devel 

3、安装libmcrypt

libmcrypt 是加密算法扩展库,支持 DES、3DES、RIJNDAEL、Twofish、IDEA、GOST、CAST-256、ARCFOUR、SERPENT、SAFER+ 等算法

[root@localhost src]# wget http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
[root@localhost src]# tar -zxf libmcrypt-2.5.8.tar.gz
[root@localhost src]# cd libmcrypt-2.5.8
[root@localhost libmcrypt-2.5.8]#./configure
[root@localhost libmcrypt-2.5.8]# make && make install

4、安装libzip

安装cmake3

[root@localhost src]# yum remove libzip
[root@localhost src]# yum install -y cmake3

cmake3如果用yum安装不上,可以使用源码安装

[root@localhost src]# wget https://cmake.org/files/v3.18/cmake-3.18.4.tar.gz
[root@localhost src]# tar -zxf cmake-3.18.4.tar.gz
[root@localhost src]# cd cmake-3.18.4
[root@localhost cmake-3.18.4]# ./bootstrap --prefix=/usr/local/cmake
[root@localhost cmake-3.18.4]# gmake && gmake install
[root@localhost cmake-3.18.4]# ln -s /usr/local/cmake/bin/cmake /usr/bin/cmake
[root@localhost cmake-3.18.4]# cmake --version

安装libzip

[root@localhost src]# wget https://libzip.org/download/libzip-1.7.3.tar.gz
[root@localhost src]# tar -zxf libzip-1.7.3.tar.gz
[root@localhost libzip-1.7.3]# cd libzip-1.7.3
[root@localhost libzip-1.7.3]# mkdir build && cd build
[root@localhost build]# cmake ..
[root@localhost build]# make && make install
在/etc/profile文件末尾添加,需要看安装的位置是lib还是lib64
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
或者
export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig
执行
source /etc/profile

5、安装oniguruma

wget https://github.com/kkos/oniguruma/archive/refs/tags/v6.9.7.1.tar.gz -O oniguruma-6.9.7.tar.gz
tar -zxf oniguruma-6.9.7.tar.gz
cd oniguruma-6.9.7.1
yum -y install libtool
./autogen.sh
./configure --prefix=/usr --libdir=/lib64
make && make install

安装PHP

下载地址

https://www.php.net/distributions/php-7.4.30.tar.gz

http://mirrors.sohu.com/php/php-7.4.30.tar.gz

编译安装

[root@localhost src]# wget -c https://www.php.net/distributions/php-7.4.30.tar.gz
[root@localhost src]# tar -zxf php-7.4.30.tar.gz
[root@localhost src]# cd php-7.4.30
[root@localhost php-7.4.30]# ./buildconf --force
[root@localhost php-7.4.30]# ./configure \
--prefix=/usr/local/php \
--exec-prefix=/usr/local/php \
--with-pdo-mysql=mysqlnd \
--with-mysql-sock=/tmp/mysql.sock \
--enable-gd \
--with-jpeg \
--with-freetype \
--with-zlib \
--with-iconv \
--enable-bcmath \
--enable-shmop \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-mbstring \
--enable-ftp \
--with-openssl \
--enable-opcache \
--enable-pcntl \
--enable-sockets \
--with-mhash \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--with-xmlrpc \
--with-zip \
--with-pear \
--with-curl
[root@localhost php-7.4.30]# make
[root@localhost php-7.4.30]# make install

问题

问题一

error: off_t undefined; check your library configuration
https://www.jianshu.com/p/f73cc37b1053

问题二

usr/local/include/zip.h:59:21: fatal error: zipconf.h: No such file or dire
#解决方法:手动复制过去
cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h

配置php.ini

一、查看php.ini配置文件的路径​

[root@localhost php-7.4.30]# /usr/local/php/bin/php --ini

Configuration File (php.ini) Path: /usr/local/php/lib

[root@localhost php-7.4.30]# cp php.ini-production /usr/local/php/lib/php.ini

二、配置php.ini

A.关闭在http头中显示php版本信息

expose_php = Off

B.错误的处理

error_reporting = E_ALL​ & ~E_NOTICE

display_errors = Off

log_errors = On

error_log = /data/log/php/php_errors.log

注意:一定要保证php_errors.log有可写权限,有两种方式:

下面我们会配置php-fpm运行的用户和用户组为nginx,所以,我们这里可以将php_errors.log的所有者和所属组改成nginx

mkdir -p /data/log/php
touch /data/log/php/php_errors.log
chown nginx:nginx /data/log/php/php_errors.log

C. 设置时区

date.timezone = PRC

linux的配置是不用去开启curl、mysql之类的扩展的,configure配置编译的时候已经默认开启了

php-fpm

配置

一、创建日志目录

mkdir -p /data/log/php-fpm

二、php-fpm.conf

[root@localhost php-7.4.30]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf


① 去掉;pid = run/php-fpm.pid  前面的分号,使之生效

② 设置错误日志路径

error_log = /data/log/php-fpm/php-fpm.log

三、www.conf

[root@localhost php-7.4.30]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

;设置用户和用户组
user = nginx
group = nginx

;设置监听
;listen = 127.0.0.1:9000
listen = /dev/shm/php-fpm.sock

listen.owner = nginx​
listen.group = nginx
listen.mode = 0660

;进程相关,这个得根据硬件情况来设置
pm.max_children = 100
pm.start_servers = 50
pm.min_spare_servers = 40
pm.max_spare_servers = 60

;开启慢日志
slowlog = /data/log/php-fpm/$pool-slow.log
request_slowlog_timeout = 10s

;开启抓去php的错误输出
catch_workers_output = yes

原因是php-fpm的配置文件php-fpm.conf中默认是关闭worker进程的错误输出,直接把他们重定向到/dev/null,所以我们在nginx的error log 和php-fpm的errorlog都看不到php的错误日志,直接开启catch_workers_output = yes 即可

检测php-fpm配置文件

[root@localhost php-7.4.30]# /usr/local/php/sbin/php-fpm -t
[30-Mar-2017 12:20:36] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful

设置php-fpm开机自启动

[root@localhost php-7.4.30]# cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-7.4.30]# chmod 0744 /etc/init.d/php-fpm
[root@localhost php-7.4.30]# chkconfig --add php-fpm
[root@localhost php-7.4.30]# chkconfig --level 35 php-fpm on
[root@localhost php-7.4.30]# chkconfig --list 来查看是否将php-fpm添加到了自启动​

启动php-fpm

方式一:

当然,第3步我们已经把php-fpm加入了开机自启动,也就是加入了服务,所有,我就可以用这样的命令来控制php-fpm了:

[root@localhost php-7.4.30]# service php-fpm start|restart|stop|reload|status|force-quit

方式二:

[root@localhost php-7.4.30]# /etc/init.d/php-fpm start|restart|stop|reload|status|force-quit

配置nginx支持php脚本

Nginx + PHP-FPM设置虚拟主机 + 开启PATHINFO模式

安装composer

php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
mv composer.phar /usr/local/bin/composer

可能的问题

#SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL
#安装ca证书工具并更新证书
yum install ca-certificates -y
update-ca-trust

配置阿里云的源

https://developer.aliyun.com/composer

安装pear

wget https://pear.php.net/go-pear.phar 
/usr/local/php/bin/php go-pear.phar

一路回车,yes就安装完毕了,就可以pecl install xlswriter来安装xlswriter了

安装mysqlnd

在编译安装php时没有设置--with-pdo-mysql=mysqlnd,才会走这一步

cd /usr/local/src/php7.3.23/ext/mysqlnd
cp config9.m4 config.m4
phpize
export PHP_OPENSSL_DIR=yes
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install

进入ext/mysql,编译出mysql.so
cd /usr/local/src/php7.3.23/ext/pdo_mysql
phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=mysqlnd
make && make install