今天早上看有公众号里文章说 PHP8 发布了,于是决定将服务器上装的 7.4.11 升级到 8.0。
由于 VPS 上使用的是 lnmp 一键安装包,所以打算通过脚本直接升级到 PHP8.0。但是当执行了升级脚本后,不但 PHP 没有升级成功,反而已经安装的 PHP 被删掉了,所以只能通过下载PHP源码编译安装了。
系统版本:
CentOS Linux release 7.8.2003
1.下载
首先从官网下载 PHP8 的安装包:
[root@racknerd soft]# wget https://www.php.net/distributions/php-8.0.0.tar.gz
2.解压
然后解压安装包并进入对应的安装包目录:
[root@racknerd soft]# tar -zxvf php-8.0.0.tar.gz [root@racknerd soft]# cd php-8.0.0
3.编译安装
可以根据自己的实际情况,在编译时添加相应的参数以开启相应的扩展,具体参数详见: 核心配置选项列表
[root@racknerd php-8.0.0]@./configure \ --prefix=/usr/local/php \ --with-config-file-path=/etc \ --enable-fpm \ --with-fpm-user=nginx \ --with-fpm-group=nginx \ --enable-inline-optimization \ --disable-debug \ --disable-rpath \ --enable-shared \ --enable-soap \ --with-libxml-dir \ --with-xmlrpc \ --with-openssl \ --with-mcrypt \ --with-mhash \ --with-pcre-regex \ --with-sqlite3 \ --with-zlib \ --enable-bcmath \ --with-iconv=/usr/local/libiconv \ --with-bz2 \ --enable-calendar \ --with-curl \ --with-cdb \ --enable-dom \ --enable-exif \ --enable-fileinfo \ --enable-filter \ --with-pcre-dir \ --enable-ftp \ --with-gd \ --with-openssl-dir \ --with-jpeg-dir \ --with-png-dir \ --with-zlib-dir \ --with-freetype \ --with-freetype-dir \ --enable-gd \ --enable-gd-native-ttf \ --enable-gd-jis-conv \ --with-gettext \ --with-gmp \ --with-mhash \ --enable-json \ --enable-mbstring \ --enable-mbregex \ --enable-mbregex-backtrack \ --with-libmbfl \ --with-onig \ --enable-pdo \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-zlib-dir \ --with-pdo-sqlite \ --with-readline \ --enable-session \ --enable-shmop \ --enable-simplexml \ --enable-sockets \ --enable-sysvmsg \ --enable-sysvsem \ --enable-sysvshm \ --enable-wddx \ --with-libxml-dir \ --with-xsl \ --with-zip \ --enable-zip \ --enable-mysqlnd-compression-support \ --with-pear \ --enable-static \ --disable-ipv6 \ --enable-intl \ --enable-opcache
在上面编译过程中,出现了两个错误,下面为出现的错误及对应的解决办法:
错误1:
configure: error: iconv does not support errno
解决方法:
[root@racknerd php-8.0.0]# cd ~/soft
[root@racknerd soft]# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz
[root@racknerd soft]# tar zxvf libiconv-1.15.tar.gz
[root@racknerd soft]# cd libiconv-1.15
[root@racknerd libiconv-1.15]# ./configure --prefix=/usr/local/libiconv
[root@racknerd libiconv-1.15]# make && make install
错误2:
configure: error: Package requirements (libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0) were not met: Requested 'libzip >= 0.11' but version of libzip is 0.10.1 Consider adjusting the PKG_CONFIG_PATH environment variable if you installed software in a non-standard prefix. Alternatively, you may set the environment variables LIBZIP_CFLAGS and LIBZIP_LIBS to avoid the need to call pkg-config
从提示信息可以看出,安装的 libzip 版本过低,需要安装更高版本的 libzip , 解决方法:
[root@racknerd php-8.0.0]# cd ~/soft
#先卸载 libzip
[root@racknerd soft]# yum remove libzip
# 重新安装 libzip
[root@racknerd soft]# wget https://libzip.org/download/libzip-1.2.0.tar.gz
[root@racknerd soft]# tar -zxvf libzip-1.2.0.tar.gz
[root@racknerd soft]# cd libzip-1.2.0
[root@racknerd libzip-1.2.0]# ./configure
[root@racknerd libzip-1.2.0]# make && make install
[root@racknerd libzip-1.2.0]# export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/"
解决完上面两个错误后再执行编译命令,即可成功编译,然后执行下面命令对 PHP 进行安装:
[root@racknerd php-8.0.0]# make && make install
当出现如下信息时,表示 PHP 安装完成:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20200930/
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 - installed: 1.4.9
[PEAR] Console_Getopt - installed: 1.4.3
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util - installed: 1.4.5
warning: pear/PEAR dependency package "pear/Archive_Tar" installed version 1.4.9 is not the recommended version 1.4.4
[PEAR] PEAR - installed: 1.10.12
Warning! a PEAR user config file already exists from a previous PEAR installation at '/root/.pearrc'. You may probably want to remove it.
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
/root/soft/php-8.0.0/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin/phar.phar
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers: /usr/local/php/include/php/ext/pdo/
4.修改默认配置
修改 php-fpm 的配置文件及将 php.ini 复制到编译时指定的目录:
[root@racknerd php-8.0.0]# cp php.ini-production /etc/php.ini [root@racknerd php-8.0.0]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf [root@racknerd php-8.0.0]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf [root@racknerd php-8.0.0]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [root@racknerd php-8.0.0]# chmod +x /etc/init.d/php-fpm
至此,PHP8.0已安装完成,通过下面命令启动 php-fpm:
[root@racknerd php-8.0.0]# /etc/init.d/php-fpm start
此时会启动失败,出现如下错误:
Starting php-fpm [27-Nov-2020 11:09:26] ERROR: [pool www] cannot get uid for user 'nginx'
[27-Nov-2020 11:09:26] ERROR: FPM initialization failed
原因是默认的 php-fpm 启动使用的用户和用户组都是 nginx ,由于系统中不存在 nginx 用户及用户组,默认使用的用户和用户组都是 www,所以需要修改 php-fpm 的配置文件 /usr/local/php/etc/php-fpm.d/www.conf ,将用户和用户组修改如下如下:
[www]
user = www
group = www
现在再次执行 php-fpm 的启动命令时即可正常启动 php-fpm:
[root@racknerd php-8.0.0]# /etc/init.d/php-fpm start
Starting php-fpm done
[root@racknerd php-8.0.0]# php -v
PHP 8.0.0 (cli) (built: Nov 27 2020 11:03:54) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies
现在 PHP8 已安装完成并可成功运行。由于 VPS 上只有这一个版本的 PHP ,所以对于所说的性能的提高还有待测试。