Centos8编译安装PHP8

更新软件到最新

yum update

下载php源码并解压

wget https://www.php.net/distributions/php-8.0.0.tar.gztar-xvzf php-8.0.0.tar.gzcdphp-8.0.0

安装gcc软件

yum install gcc gcc-c++ make

安装php依赖软件

yum install libxml2 libxml2-devel openssl openssl-devel freetype freetype-devel libzip libzip-devel sqlite sqlite-devel gd gd-devel libcurl-devel libicu libicu-devel libxslt libxslt-devel

安装oniguruma软件

yum install https://rpms.remirepo.net/enterprise/8/remi/x86_64/oniguruma5php-6.9.6-1.el8.remi.x86_64.rpm
yum install https://rpms.remirepo.net/enterprise/8/remi/x86_64/oniguruma5php-devel-6.9.6-1.el8.remi.x86_64.rpm

配置编译

./configure --prefix=/usr/local/php \
--with-zlib-dir \
--enable-mbstring \
--enable-soap \
--enable-calendar \
--with-curl \
--disable-rpath \
--enable-gd \
--enable-gd-jis-conv \
--with-bz2 \
--with-zlib \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--enable-mbregex \
--enable-exif \
--enable-bcmath \
--with-mhash \
--with-pdo-mysql \
--with-mysqli \
--with-openssl \
--with-fpm-user=www \
--with-fpm-group=www \
--with-libdir=/lib/x86_64-linux-gnu/ \
--enable-ftp \
--with-gettext \
--with-xsl \
--enable-opcache \
--enable-fpm \
--with-iconv \
--with-zip \
--with-pear \
--with-freetype \
--enable-intl

开始编译

make -j 4

安装php

make install

添加php.ini配置文件

cp php.ini-production /usr/local/php/lib/php.ini

以下操作为配置php

添加用户

user add www

修改php-fpm.conf

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

编辑www.conf文件,根据自己的服务器配置修改以下配置

pm.max_children=10
pm.start_servers=5
pm.min_spare_servers=4
pm.max_spare_servers=7

移除前面的注释

;env[HOSTNAME] = $HOSTNAME
;env[PATH] = /usr/local/bin:/usr/bin:/bin
;env[TMP] = /tmp
;env[TMPDIR] = /tmp
;env[TEMP] = /tmp

修改php配置文件

cd /usr/local/php/lib
vim php.ini

修改内存限制

memory_limit=512M

修改上传文件限制

post_max_size=512M
upload_max_filesize=512M

启用OPcache 和 JIT

;zend_extension=opcache
[opcache]
opcache.enable=1;
opcache.memory_consumption=128;
opcache.interned_strings_buffer=8;
opcache.max_accelerated_files=10000;
opcache.validate_timestamps=1

添加jit支持

opcache.jit_buffer_size=128M

注意:虚拟机里可能启动时报错。

设置php-fpm开机启动

vim /usr/lib/systemd/system/php-fpm.service

其内容为

[Unit]
Description=The PHP FastCGI Process Manager
After=network.target
[Service]
Type=simple
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

保存退出后执行

systemctl enable php-fpm
systemctl start php-fpm

查看运行状态

systemctl status php-fpm