php-8.0.0-everywhere.jpg

I. 升级至 PHP 8.0.0

Google pagespeed results

博主一向以最简洁的方法平滑升级 PHP;源码编译安装很是舒服,其中遇到内存不足的问题也通过增加 swap(交换分区)解决了,Redis 扩展的安装 涉及到安装目录的一些问题也得到了很好的运用;

此外,Typecho 已支持 PHP8(最新开发版),但 handsome 主题似乎尚未跟上;

查看当前版本PHP 的编译参数,复制并去掉多余的'符号;

root@localhost:~# /usr/local/php/sbin/php-fpm -i |grep configure
Configure Command =>  './configure'  '--prefix=/usr/local/php' '--with-config-file-path=/usr/local/php/etc' '--with-config-file-scan-dir=/usr/local/php/conf.d' '--enable-fpm' '--with-fpm-user=www' '--with-fpm-group=www' '--enable-mysqlnd' '--with-mysqli=mysqlnd' '--with-pdo-mysql=mysqlnd' '--with-iconv-dir' '--with-freetype-dir=/usr/local/freetype' '--with-jpeg-dir' '--with-png-dir' '--with-zlib' '--with-libxml-dir=/usr' '--enable-xml' '--disable-rpath' '--enable-bcmath' '--enable-shmop' '--enable-sysvsem' '--enable-inline-optimization' '--with-curl' '--enable-mbregex' '--enable-mbstring' '--enable-intl' '--enable-ftp' '--with-gd' '--with-openssl' '--with-mhash' '--enable-pcntl' '--enable-sockets' '--with-xmlrpc' '--enable-zip' '--enable-soap' '--with-gettext' '--disable-fileinfo' '--enable-opcache' '--with-xsl'

备份当前版本PHP php-fpm 并安装 PHP 8.0.0;

cp /usr/local/php/sbin/php-fpm /usr/local/php/sbin/php-fpm.bak #备份 php-fpm
wget https://www.php.net/distributions/php-8.0.0.tar.gz
tar -zxvf php-8.0.0.tar.gz && cd php-8.0.0

./configure  --prefix=/usr/local/php-8.0.0 --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/conf.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv=/opt/local --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-intl --enable-ftp --with-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache --with-xsl

make && make install
lnmp php-fpm stop #暂时关闭 PHP
cp /usr/local/php-8.0.0/sbin/php-fpm /usr/local/php/sbin/php-fpm  #新版本覆盖旧版本文件
lnmp php-fpm start # 开启

II. iconv 错误

为了进一步优化服务器性能,想着升级至 PHP 8.0.0,遇到一些问题:

configure: error: iconv does not support errno

我在 https://bugs.php.net/ 找到了一个确实可靠的解决办法;

Bug #47695  --with-iconv configure option always fails in Leopard
Submitted:  2009-03-17 18:02 UTC    Modified:   2009-04-30 13:03 UTC    
From:   p2409 at hotmail dot com    Assigned:   scottmac (profile)
Status: Not a bug   Package:    Compile Failure
PHP Version:    5.2.9   OS: Mac OS X 10.5.5
Private report: No  CVE-ID: None

Thank you for the report, and for helping us make PHP better.
--with-iconv=/opt/local not --with-iconv-dir this is only for xmlrpc and not for the regular iconv.

via https://bugs.php.net

意思就是说可以使用 --with-iconv=/opt/local;这样就可以规避错误了;

./configure --prefix=/usr/local/php8.0.0 --with-iconv=/opt/local

III. 内存不足的问题

1C1G 编译 PHP8.0.0 源码还是非常勉强的,一不小心就内存爆炸了;最后不得不增加一些 swap(交互分区)以解决内存太小的问题;

报错如下:

g++: fatal error: Killed signal terminated program cc1plus
compilation terminated.

解决方案:

#获取要增加的2G的SWAP文件块
dd if=/dev/zero of=/swapfile bs=1k count=2048000
#创建SWAP文件,挂载在根目录下
mkswap /swapfile
#激活SWAP文件
swapon /swapfile   
#查看SWAP信息是否正确
swapon -s  
#添加到fstab文件中让系统引导时自动启动
echo "/var/swapfile swap swap defaults 0 0" >> /etc/fstab

swapfile文件的路径在/var/下,编译完后, 如果不想要交换分区了, 可以删除。

删除交换分区:

swapoff /swapfile
rm -rf /swapfile

via PHP报错 Killed signal terminated program cc1

IV. 安装Redis扩展

由于之前已经安装了 Redis 扩展,但 PHP 8.0.0 时不能直接使用,所以我们需要重新进行安装;在此需要注意的是,因为我们是直接复制新版本的 php-fpm 文件覆盖 旧版本的 php-fpm ,所以在为 PHP 8.0.0 安装 Redis 扩展时,应调用 /usr/local/php8.0.0 文件下的资源;

redis 扩展下载 https://pecl.php.net/package/redis

$ wget https://pecl.php.net/get/redis-5.3.2.tgz # 你可按需下载最新版本
$ tar -zxvf redis-5.3.2.tgz
$ cd redis-5.3.2                   # 进入 redis-5.3.2 目录
$ /usr/local/php8.0.0/bin/phpize             # php8.0.0 安装后的路径
$ ./configure --with-php-config=/usr/local/php8.0.0/bin/php-config
$ make && make install

以上。

最后修改:2021 年 04 月 11 日 05 : 50 AM