商城首页欢迎来到中国正版软件门户

您的位置:首页 >编译的php,php编译添加扩展

编译的php,php编译添加扩展

  发布于2026-04-28 阅读(0)

扫一扫,手机访问

编译PHP:从依赖安装到参数详解,一篇搞定

编译PHP这事儿,说难不难,但细节没处理好,就很容易卡壳。今天咱们就把这个流程彻底捋顺,从环境准备到编译参数,一步到位,下次直接复制粘贴就能搞定,再也不用到处找参数了。

编译的php,php编译添加扩展

第一步:安装编译依赖

工欲善其事,必先利其器。编译前,得先把这些基础依赖包给装上:

yum install gcc

yum install libxml2-devel curl-devel libjpeg libjpeg-devel libpng-devel freetype-devel.x86_64 mysql-devel

yum install freetype-devel.x86_64 freetype.x86_64

yum install libjpeg libjpeg-devel

第二步:获取并解压PHP源码

准备好依赖后,就可以下载PHP源码包了。这里以5.3.28版本为例:

wget http://us1.php.net/distributions/php-5.3.28.tar.bz2

tar -jxf php-5.3.28.tar.bz2

cd php-5.3.28

第三步:配置与编译(第一个坑)

接下来是关键的一步——运行configure脚本。这里配置了丰富的扩展支持,为后续应用打好基础:

./configure --prefix=/usr/local/php-5.3.28 --enable-fpm --with-mysql --with-zlib --with-config-file-path=/usr/local/php5.3.28/etc --with-pdo-mysql --with-libdir=lib64 --with-gd --with-jpeg-dir=/usr/lib --with-png-dir=/usr/lib --enable-mbstring --enable-gd-native-ttf --enable-xml --with-freetype-dir --with-curl --enable-ftp --enable-zip --with-mcrypt --enable-mbstring --enable-json

然而,事情并没这么顺利。配置过程中,报错了:

checking for alloca... (cached) yes

checking for 8-bit clean memcmp... yes

checking for stdarg.h... (cached) yes

checking for mcrypt support... yes

configure: error: mcrypt.h not found. Please reinstall libmcrypt

系统明明显示已经安装了libmcrypt:

rpm -qa | grep libmcrypt

libmcrypt-2.5.7-5.el5

libmcrypt-2.5.8-4.el5.centos

问题出在哪?原来,通过yum安装的库有时可能不包含开发头文件。解决办法是手动下载源码编译安装。试试看:

wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz

安装步骤很简单:

tar -zxvf libmcrypt-2.5.7.tar.gz

cd libmcrypt-2.5.7

./configure

make

make install

搞定依赖后,重新编译PHP,这下顺利通过了。

第四步:编译安装与基本配置

# make

# make install

安装完成后,需要复制一些必要的配置文件:

# cp php.ini-production /usr/local/php5.3.28/etc/php.ini

# cp sapi/fpm/php-fpm.conf /usr/local/php5.3.28/etc/

# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

# chmod 755 /etc/init.d/php-fpm

接着,来调整PHP-FPM的配置文件,让它更贴合生产环境:

# cd /usr/local/php5.3.28/etc/

# vi php-fpm.conf

需要修改和取消注释的几个关键位置:

将[global]属性里面的 pid = run/php-fpm.pid 去掉注释。

将[www]属性里面的 user = nobody group = nobody 的nobody改为nginx(假设你的Web服务器用户是nginx)。

将pm.min_spare_servers = 5 去掉注释

将pm.max_spare_servers = 35 去掉注释

将pm.start_servers = 20 去掉注释

在php.ini中,建议设置:

memory_limit = 128M

date.timezone = Asia/Shanghai

最后,别忘了把PHP的可执行文件路径加入系统环境变量:

vi /etc/profile

PATH=$PATH:/usr/local/php5.3.28/bin:/usr/local/php5.3.28/sbin

source /etc/profile

第五步:配置Nginx支持PHP-FPM

要让Nginx能正确解析PHP,还需要在FastCGI参数中添加一行:

vi /etc/nginx/fastcgi_params

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;

第六步:编译参数详解

最后,咱们回过头来详细拆解一下最开始那一长串编译参数,理解了它们,以后自定义编译就心里有底了:

./configure --prefix=/usr/local/php-5.3.28 安装的路径

--enable-fpm 打上PHP-fpm 补丁后才有这个参数,这是以CGI方式安装的启动程序。

--with-mysql 支持MySQL。

--with-zlib 打开zlib压缩库的支持。

--with-config-file-path=/usr/local/php5.3.28/etc 指定php.ini配置文件的位置。

--with-pdo-mysql 启用PDO MySQL驱动。

--with-libdir=lib64 指定系统库文件的目录(针对64位系统)。

--with-gd 打开GD图形库的支持,用于处理图像。

--with-jpeg-dir=/usr/lib 指定jpeg库路径,打开对jpeg图片的支持。

--with-png-dir=/usr/lib 指定png库路径,打开对png图片的支持。

--enable-mbstring 启用多字节字符串函数支持,对中文等多字节语言至关重要。

--enable-gd-native-ttf 支持TrueType字体函数库,允许在图像中使用字体。

--enable-xml 打开XML支持。

--with-freetype-dir 打开对freetype字体库的支持。

--with-curl 打开cURL库的支持,用于处理HTTP请求。

--enable-ftp 支持FTP扩展。

--enable-zip 支持Zip压缩扩展。

--with-mcrypt 支持Mcrypt加密扩展。

--enable-mbstring 再次强调支持mbstring扩展。

--enable-json 支持JSON扩展。

把这些参数和步骤串起来,就是一份完整的PHP编译指南了。下次再遇到类似需求,直接按图索骥,效率能提升不少。

本文转载于:https://blog.csdn.net/weixin_33509203/article/details/115120939 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注