系统环境
cat /etc/redhat-releaseCentOS Linux release 7.5.1804 (Core)
查看可用版本
预先安装必要的依赖
yum install -y \wget \gcc \gcc-c++ \autoconf \automake \libtool \make \libxml2 \libxml2-devel \openssl \openssl-devel \sqlite-devel
下载解压
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
设置安装参数
我们可能会安装多个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-zlib \--enable-xml \--disable-rpath \--enable-bcmath \--enable-shmop \--enable-sysvsem \--with-curl \--enable-mbregex \--enable-mbstring \--enable-intl \--enable-ftp \--enable-gd-jis-conv \--with-openssl \--with-mhash \--enable-pcntl \--enable-sockets \--enable-soap \--with-gettext \--enable-fileinfo \--enable-opcache \--with-pear \--with-ldap=shared \--without-gdbm
安装
# 执行安装make && make install# 测试make test
创建快捷方式
# 创建软链接ln -s /usr/local/php/8.0.24/bin/php /usr/local/bin/php8# 测试php8 -vPHP 8.0.24 (cli) (built: Nov 4 2022 14:13:22) ( NTS )Copyright (c) The PHP GroupZend Engine v4.0.24, Copyright (c) Zend Technologies
修改配置文件
# 源码包 php-8.0.24cp php.ini-production /usr/local/php/8.0.24/etccd /usr/local/php/8.0.24/etccp php.ini-production php.inicd /usr/local/php/8.0.24/etccp php-fpm.conf.default php-fpm.confcd /usr/local/php/8.0.24/etc/php-fpm.dcp www.conf.default www.conf# 修改www.conf文件 # 监听方式为sock;listen = 127.0.0.1:9000listen = php-cgi.socklisten.owner = wwwlisten.group = wwwlisten.mode = 0666
开机自启
# 源码包 php-8.0.24/sapi/fpmcp php-fpm.service /usr/lib/systemd/system/php-fpm-8.0.24.service# 启动服务systemctl enable supervisord# 验证一下是否为开机启动systemctl is-enabled supervisord systemctl start supervisordsystemctl status supervisordsystemctl 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
或者
内存不够使用,可以先使用交换分区来解决
dd if=/dev/zero of=/swapfile bs=64M count=16mkswap /swapfileswapon /swapfile
安装完成后取消
swapoff /swapfilerm /swapfile
参考