在centos系统上编译安装PHP是一个涉及多个步骤的过程,包括下载安装包、安装依赖库、配置编译选项以及最终的编译和安装,以下是详细的步骤和注意事项:
一、准备工作
1、更新系统并安装必要的开发工具

- yum update y
- yum groupinstall 'Development Tools' y
2、安装PHP编译所需的依赖库
- yum install libxml2devel openssldevel bzip2devel libcurldevel libjpegdevel libpngdevel freetypedevel gmpdevel libmcryptdevel readlinedevel libxsltdevel zlibdevel glibcdevel glib2devel ncursesdevel curldevel db4devel libXpmdevel libX11devel gddevel gmpdevel expatdevel xmlrpcc xmlrpccdevel libicudevel libmcryptdevel libmemcacheddevel y
二、下载PHP源码
1、创建目录并进入
- mkdir /package
- cd /package
2、下载PHP源码
以PHP 8.0.0为例,可以从PHP官方网站下载对应版本的源码包。
- wget https://www.php.net/distributions/php8.0.0.tar.gz
3、解压源码包
- tar zxvf php8.0.0.tar.gz
- cd php8.0.0
三、配置编译选项
1、配置编译参数

在配置前,建议先查看configure
脚本的帮助信息,以便了解可用的编译选项。
- ./configure help
2、执行配置命令
根据需求添加相应的编译选项,以下是一个常见的配置示例:
- ./configure \
- prefix=/usr/local/php \
- withconfigfilepath=/usr/local/php/etc \
- enablefpm \
- withfpmuser=www \
- withfpmgroup=www \
- withmysqli \
- withpdomysql \
- withopenssl \
- withcurl \
- withgd \
- withjpegdir \
- withpngdir \
- withfreetypedir \
- withzlib \
- withbz2 \
- enablembstring \
- enablembregex \
- withmhash \
- enablesoap \
- enablesockets \
- enableopcache \
- enableinlineoptimization \
- withpear \
- withgettext \
- withiconvdir \
- withkerberos \
- withlibdir=lib64 \
- withlibxmldir \
- enablexml \
- enablezip \
- enablebcmath \
- enablesession \
- enablesysvsem \
- enablesysvshm \
- enablepcntl \
- enableshmop \
- withpcreregex \
- withxsl \
- withxmlrpc
四、编译与安装
1、编译源码
- make && make install
2、检查安装结果
- /usr/local/php/bin/php v
五、配置环境变量
将PHP的可执行文件路径添加到系统的环境变量中,以便在终端中直接使用php
命令,编辑/etc/profile
文件,添加以下内容:
- export PATH=/usr/local/php/bin:$PATH
然后使更改生效:
- source /etc/profile
六、配置PHPFPM
1、复制配置文件
- cp php.iniproduction /usr/local/php/etc/php.ini
- cp sapi/fpm/phpfpm.conf.default /usr/local/php/etc/phpfpm.conf
- cp sapi/fpm/phpfpm.d/www.conf.default /usr/local/php/etc/phpfpm.d/www.conf
2、启动PHPFPM
- /usr/local/php/sbin/phpfpm fpmconfig /usr/local/php/etc/phpfpm.conf
七、配置Nginx(可选)
如果使用Nginx作为Web服务器,需要配置Nginx以支持PHP,编辑Nginx的配置文件(如/etc/nginx/nginx.conf
),添加以下内容以支持FastCGI:
- location ~ \.php$ {
- root html;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
然后重启Nginx服务:
- systemctl restart nginx
八、常见问题及解决方案
1、缺少libzip依赖:如果遇到“checking for libzip... configure: error: system libzip must be upgraded to version >= 0.11”错误,需要先安装或升级libzip。
- wget https://nih.at/libzip/libzip1.2.0.tar.gz
- tar zxvf libzip1.2.0.tar.gz
- cd libzip1.2.0
- ./configure && make && make install
2、缺少sqlite3依赖:如果遇到“error: Package requirements (sqlite3 > 3.7.4) were not met”,需要安装libsqlite3xdevel。
- yum install libsqlite3xdevel y
3、off_t undefined错误:如果在配置时遇到“configure: error: off_t undefined; check your library configuration”,可能是由于缺少某些库文件,可以尝试将以下路径添加到ld.so.conf
文件中:
- echo '/usr/local/lib64' >> /etc/ld.so.conf
- echo '/usr/lib64' >> /etc/ld.so.conf
- ldconfig
4、zipconf.h文件缺失:如果遇到“fatal error: zipconf.h: No such file or directory”错误,可以尝试复制缺失的文件:
- cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h
九、相关问答FAQs
Q1: 为什么在CentOS上编译安装PHP而不是使用包管理器?
A1: 编译安装PHP可以提供更高的灵活性和定制性,允许用户选择特定的PHP版本和扩展,而不受操作系统默认包管理器提供的软件包限制,编译安装还可以确保PHP及其依赖库是最新的,从而提高性能和安全性,编译安装过程相对复杂,需要一定的技术背景和经验,对于生产环境,建议仔细测试编译安装的PHP版本以确保其稳定性和兼容性。