centos 版本至少是8.0 才能按照下面的方式安装 (主要是扩展包兼容性问题)
1、下载 PHP 7.4.14 源码
# 新建用户:
userdel www
groupadd www
useradd -g www -M -d /data/www -s /sbin/nologin www &> /dev/null
# 下载安装包
wget https://www.php.net/distributions/php-7.4.14.tar.gz
2、安装 PHP 7.4.14 依赖包 (最低版本centos8.0)
yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel \
libcurl libcurl-devel libjpeg libjpeg-devel libpng \
libpng-devel freetype freetype-devel gmp gmp-devel \
libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel
#PHP 7.4 新的改动:
yum install libsqlite3x-devel -y
yum install oniguruma-devel -y ## 这个扩展有点麻烦
# 直接安装高版本 libzip扩展
yum install -y http://rpms.remirepo.net/enterprise/7/remi/x86_64/libzip5-1.7.2-1.el7.remi.x86_64.rpm
yum install -y http://rpms.remirepo.net/enterprise/7/remi/x86_64/libzip5-devel-1.7.2-1.el7.remi.x86_64.rpm
# 这里如果找不到,则到 http://rpms.remirepo.net/enterprise/7/remi/x86_64/ 下找对应的高版本安装包。
3、PHP 7.4.14 的编译配置
可以从这一步起参考PHP官方安装说明
提示:prefix
是安装目录,可以安装到你想安装的目录</mark>(类似在 Windows 下运行 setup.exe 安装包时候,让你选的安装目录),比如 、
opt/php/,在Linux下编译安装大部分应用一般先
./configure,然后
make && make install(make 和 make install 也可以分开执行),最后可选
make clean` 的参考步骤。
注意:PHP 7.4.14 开始,已经不支持 --with-gd
,得改成 --enable-gd
</mark>,一些可能的选项变化如下:
configure: WARNING: unrecognized options: --with-libxml-dir, --with-pcre-regex, --with-pcre-dir, --with-gd, --with-jpeg-dir, --with-png-dir, --with-freetype-dir, --enable-mbregex-backtrack, --with-onig, --enable-wddx, --enable-zip
上面这些已不再被 PHP 7.4.X 支持:
#Old 老的 PHP 编译选项...
--with-png-dir=/usr/include/
--with-jpeg-dir=/usr/include/
--with-freetype-dir=/usr/include/
#New - PHP 7.4.X 之后新的 PHP 编译选项
--with-png=/usr/include/
--with-jpeg=/usr/include/
--with-freetype=/usr/include/
-------------------------------------------
# PCRE 高版本依赖(如果需要安装扩展 --with-external-pcre):
wget https://ftp.pcre.org/pub/pcre/pcre2-10.36.tar.gz
tar xvf pcre2-10.36.tar.gz
cd pcre2-10.36
./configure --prefix=/usr/local/pcre2 \
--enable-pcre2-16 \
--enable-pcre2-32 \
--enable-jit \
--enable-jit-sealloc
make && make install
export PKG_CONFIG_PATH=/usr/local/pcre2/lib/pkgconfig/
-------------------------------------------
# GD 库要求大于 gdlib >= 2.1.0:
$ wget https://github.com/libgd/libgd/releases/download/gd-2.3.0/libgd-2.3.0.tar.gz
$ tar xvfz libgd-2.3.0.tar.gz
$ cd libgd-2.3.0
$ ./configure --prefix=/usr/local/libgd/2_3_0
...
** Configuration summary for libgd 2.3.0:
Support for gd/gd2 images: yes
Support for Zlib: yes
Support for PNG library: yes
Support for JPEG library: yes
...
$ make && make install
export PKG_CONFIG_PATH=/usr/local/libgd/2_3_0/lib/pkgconfig
-------------------------------------------
#注意如上,如果有多个的话,按下面去 export,之后可以 echo $PKG_CONFIG_PATH 来验证:
export PKG_CONFIG_PATH=/usr/local/pcre2/lib/pkgconfig/:/usr/local/libgd/2_3_0/lib/pkgconfig:/usr/local/lib64/pkgconfig/
更多请看 PHP 官网说明:PHP:Migration to pkg-config,也可以用 ./configure --help
命令可以列出当前可用的所有参数。
最终编译参数如下: (根据需求删减)
./configure \
--prefix=/Data/apps/php7.4.14 \
--with-config-file-path=/Data/apps/php7.4.14/etc \
--enable-fpm \
--enable-gd \
--with-external-gd \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-libxml \
--with-xmlrpc \
--with-openssl \
--with-external-pcre \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--enable-ftp \
--with-openssl-dir \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--with-xsl \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache
看到以下提示,证明已经正确安装
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+
Thank you for using PHP.
这里可能会报很多 PHP 7.X 编译错误,一个个解决即可。这里有大部分的解决方法,列出几个常见的:
checking for png_write_image in -lpng… yes If configure fails try –with-xpm-dir=
configure: error: freetype.h not found.
解决: Reconfigure your PHP with the following option. --with-xpm-dir=/usr
checking for png_write_image in -lpng… yes configure: error: libXpm.(a|so) not found.
解决: yum install libXpm-devel
checking for bind_textdomain_codeset in -lc… yes checking for GNU MP support…
yes configure: error: Unable to locate gmp.h
解决: yum install gmp-devel
configure: error: Please reinstall the libzip distribution
解决: 安装最新libzip,如果是1.5以上版本的 libzip,需要CMAKE编译安装
configure: error: off_t undefined; check your library configuration
解决:
echo '/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64'>>/etc/ld.so.conf&&ldconfig -v
#如果提示以下错误,此时把这两个编译项从配置中删除即可(上述命令中已删除)
configure: WARNING: unrecognized options: --with-mcrypt, --enable-gd-native-ttf
undefined reference to `libiconv_open' # google搜索下吧
4、编译完成,正式安装
#多核CPU机器make的时候,可以考虑用 make -j8
make && make install
5、配置PHP环境变量
vim /etc/profile
# 在结尾 增加下面两行
export PHP=/usr/local/php
export PATH=$PATH:$PHP/bin:$PHP/sbin
# 保存退出
# 让配置生效
source /etc/profile
6、配置和优化 php-fpm
#php.ini 配置文件
cp php.ini-production /Data/apps/php7.4.14/etc/php.ini
#conf 配置文件
mv ./php7.4.14/etc/php-fpm.conf.default /opt/php/etc/php-fpm.conf
#www.conf 文件
mv ./php7.4.14/etc/php-fpm.d/www.conf.default /opt/php7/etc/php-fpm.d/www.conf
# 修改 php.ini 相关参数:
$ vim /usr/local/php/etc/php.ini
expose_php = Off
short_open_tag = ON
max_execution_time = 300
max_input_time = 300
memory_limit = 128M
post_max_size = 32M
date.timezone = Asia/Shanghai
# 修改php-fpm相关参数
sed -i 's,user = nobody,user=www,g' /opt/php/etc/php-fpm.d/www.conf
sed -i 's,group = nobody,group=www,g' /opt/php/etc/php-fpm.d/www.conf
sed -i 's,^pm.min_spare_servers = 1,pm.min_spare_servers = 5,g' /opt/php/etc/php-fpm.d/www.conf
sed -i 's,^pm.max_spare_servers = 3,pm.max_spare_servers = 64,g' /opt/php/etc/php-fpm.d/www.conf
sed -i 's,^pm.max_children = 5,pm.max_children = 100,g' /opt/php/etc/php-fpm.d/www.conf
sed -i 's,^pm.start_servers = 2,pm.start_servers = 20,g' /opt/php/etc/php-fpm.d/www.conf
sed -i 's,;pid = run/php-fpm.pid,pid = run/php-fpm.pid,g' /opt/php/etc/php-fpm.d/www.conf
sed -i 's,;error_log = log/php-fpm.log,error_log = /data/logs/php/php-fpm.log,g' /opt/php/etc/php-fpm.d/www.conf
sed -i 's,;slowlog = log/$pool.log.slow,slowlog = /data/logs/php/\$pool.log.slow,g' /opt/php/etc/php-fpm.d/www.conf
# 安装启动文件 或 安装服务启动
install -v -m755 ./php7.4.14/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
#添加 php-fpm 执行权限
chmod +x /etc/init.d/php-fpm
/etc/init.d/php-fpm start
把systemctl文件加入开机启动服务
cp ./php7.4.14/sapi/fpm/php-fpm.service /usr/lib/systemd/system/php-fpm.service
systemctl start php-fpm.service //启动fpm
systemctl enable php-fpm.service //开机启动 (centos推荐这种方式)
# 注意一点: /etc/init.d/php-fpm 和 php-fpm.service 启动方式会产生不同的php进程,使用其中一个即可。
# 推荐使用 systemctl 管理php-fpm进程, systemctl 会自动拉起进程 和 supervisor 类似的效果。
配置opcache
设置 OPcache 缓存:
[opcache]
zend_extension=/usr/local/php/lib/php/extensions/no-debug-zts-20160303/opcache.so
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
最后安装其他扩展:
wget http://pecl.php.net/get/swoole-4.4.23.tgz
wget http://pecl.php.net/get/mongodb-1.8.2.tgz
wget http://pecl.php.net/get/mongodb-1.5.4.tgz
wget http://pecl.php.net/get/redis-4.3.0.tgz
wget http://pecl.php.net/get/redis-5.2.2.tgz