系统环境

cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)

查看可用版本

​https://www.php.net/releases/​

1、下载解压

wget https://www.php.net/distributions/php-8.0.24.tar.gz && \
tar -zxvf php-8.0.24.tar.gz && \
cd php-8.0.24

2、设置安装参数

我们可能会安装多个PHP版本,所以提前规划好安装目录

例如:

所有的PHP解释器都安装到一个PHP目录下,按照版本取划分目录

/usr/local/php/8.0.24
/usr/local/php/7.1.0

configure

# 可查看帮助
./configure --help

# 配置
./configure --prefix=/usr/local/php/8.0.24 \
--with-config-file-path=/usr/local/php/8.0.24/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-mysqlnd-compression-support \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--with-libmbfl \
--enable-ftp \
--with-gd \
--enable-gd-jis-conv \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--enable-fileinfo \
--enable-opcache \
--with-pear \
--enable-maintainer-zts \
--with-ldap=shared \
--without-gdbm

安装

# install
make && make install

创建快捷方式

# 创建软链接
ln -s /usr/local/php/8.0.24/bin/php /usr/local/bin/php8

# 测试
php8 -v
PHP 8.0.24 (cli) (built: Nov 4 2022 14:13:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.24, Copyright (c) Zend Technologies

修改配置文件

# 源码包 php-8.0.24
cp php.ini-production /usr/local/php/8.0.24/etc/php.ini

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

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

# 修改www.conf文件
# 监听方式为sock
;listen = 127.0.0.1:9000
listen = php-cgi.sock

开机自启

# 源码包 php-8.0.24/sapi/fpm
cp php-fpm.service /usr/lib/systemd/system/php-fpm-8.0.24.service

# 启动服务
systemctl enable supervisord

# 验证一下是否为开机启动
systemctl is-enabled supervisord

systemctl start supervisord

systemctl status supervisord

systemctl stop supervisord

Nginx 部署 Laravel

server {
listen 80;
listen [::]:80;

server_name example.com;
root /srv/example.com/public;

add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";

index index.php;

charset utf-8;

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }

error_page 404 /index.php;

location ~ \.php$ {
fastcgi_pass unix:/usr/local/php/8.0.24/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}

location ~ /\.(?!well-known).* {
deny all;
}
}

需要给 php-fpm.sock 访问权限

chown www:www /usr/local/php/8.0.24/php-fpm.sock

报错及解决

报错1

configure: error: Cannot find ldap libraries in /usr/lib
cp -frp /usr/lib64/libldap* /usr/lib/

报错2

make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1
--disable-fileinfo

参考