php编译

php编译

源码地址:

相关预编译选项:

具体支持的选项需要根据当前版本查看:

./configure --help

--with-zlib		开启zlib扩展,不指定路径就是用系统的
--enable-fpm		开启fpm
--enable-mysqlnd	开启mysql配置
--with-mysqli[路径]	指定mysql_config的路径,写mysqlnd就是用系统的
--with-pdo-mysql=目录	指定mysql的安装目录,写mysqlnd就是用系统的
--with-openssl		开启openssl
--with-config-file-path=路径		指定php.ini的位置
--with-config-file-scan-dir=路径		指定附加的ini配置文件的目录,此目录下的ini文件会被追加到php.ini配置中
--enable-pcntl		开启pcntl
--with-curl		开启curl功能
--with-gd
--with-gdbm
--enable-gd-jis-conv
--enable-zip
--with-libzip
--enable-mbstring
--with-mcrypt
--enable-shmop		共享内存
--enable-sockets
--with-xsl
--enable-maintainer-zts		开启线程安全
--with-pear			启用pecl扩展安装工具

编译

1)安装相关依赖包

ubuntu18:

apt install -y libxml2-dev libssl-dev libsqlite3-dev libbz2-dev libjpeg-dev libpng-dev  libfreetype6-dev libzip-dev curl libcurl4 libcurl4-openssl-dev libonig2 libonig-dev libmcrypt-dev

centos7:

yum install -y http://rpmfind.net/linux/epel/7/x86_64/Packages/o/oniguruma-6.8.2-1.el7.x86_64.rpm http://rpmfind.net/linux/epel/7/x86_64/Packages/o/oniguruma-devel-6.8.2-1.el7.x86_64.rpm
yum install -y sqlite sqlite-devel bzip2 bzip2-devel libpng libpng-devel libjpeg-turbo libjpeg-turbo-devel freetype freetype-devel gettext gettext-devel xml-devel libxml2 libxml2-devel libmcrypt libmcrypt-devel libzip libzip-devel zip unzip libcurl libcurl-devel libicu libicu-devel libxslt libxslt-devel autoconf libzstd libzstd-devel libmemcached libmemcached-devel libevent libevent-devel libsodium libsodium-devel libargon2 libargon2-devel ImageMagick ImageMagick-devel gd gd-devel libgcc libwebp libwebp-devel libXpm libXpm-devel openssl-devel

centos8:

yum install -y http://rpmfind.net/linux/centos/8-stream/AppStream/x86_64/os/Packages/oniguruma-6.8.2-2.el8.x86_64.rpm http://rpmfind.net/linux/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
yum install -y sqlite sqlite-devel bzip2 bzip2-devel libpng libpng-devel libjpeg-turbo libjpeg-turbo-devel freetype freetype-devel gettext gettext-devel libxml2 libxml2-devel libxcrypt libxcrypt-devel libzip libzip-devel zip unzip libcurl libcurl-devel libicu libicu-devel libxslt libxslt-devel autoconf libzstd libzstd-devel libmemcached libmemcached-libs libevent libevent-devel  gd gd-devel libgcc libwebp libwebp-devel libXpm libXpm-devel openssl-devel
2)预编译php7.3

以我公司php编译为例
在编译7.4的时候会有其他的依赖库问题,以及libiconv的编译报错,我这里没写,如果需要,可评论留言,我有时间把相关错误解决方法整理出来

wget https://www.php.net/distributions/php-7.3.30.tar.gz
tar xf php-7.3.30.tar.gz
cd php-7.3.30
./configure --prefix=/opt/php --with-config-file-path=/opt/php/etc --with-config-file-scan-dir=/opt/php/etc/php.d --with-fpm-user=www --with-fpm-group=www --enable-fpm --disable-opcache --disable-fileinfo --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-zlib --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-exif --enable-sysvsem --enable-inline-optimization --with-curl=/usr/local/curl --enable-mbregex --enable-mbstring --with-password-argon2 --with-sodium --with-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-ftp --enable-intl --with-xsl --with-gettext --enable-zip --without-libzip --enable-soap --disable-debug --enable-gd-jis-conv
make -j4 && make -j4 install
3)配置修改
#环境配置准备
echo "export PATH=/opt/php/bin:/opt/php/sbin:$PATH" >> /etc/profile.d/php.sh
. /etc/profile
cd php-7.3.30
cp php.ini-production /opt/php/etc/php.ini
cd /opt/php/etc
mv php-fpm.conf.default php-fpm.conf
mv php-fpm.d/www.conf.default php-fpm.d/www.conf

#fpm配置修改
cd /opt/php/etc
vim php-fpm.conf		#修改以下即可,如需更多配置,可查看博主关于fpm配置文件文章
  listen = 9000
  pm = dynamic		#动态进程
  pm.max_children = 50	#最大开启子线程
  pm.start_servers = 5	#当前启动线程
  pm.min_spare_servers = 2	#最小空闲线程
  pm.max_spare_servers = 8		#最大空闲
4)准备fpm服务启动文件
#写入fpm服务启动文件
cat > /etc/systemd/system/php-fpm.service <<-eof
[Unit]
After=network.target

[Service]
Type=simple
PIDFile=/opt/php/var/run/php-fpm.pid
ExecStart=/opt/php/sbin/php-fpm --nodaemonize -y /opt/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
eof

#启动fpm服务
systemctl daemon-reload
systemctl restart php-fpm.service

安装php扩展

可以用自带的工具pecl安装扩展
7.4版本以上需要手动开启,不会默认开启,也就是需要预编译时指定

例1:安装swoole扩展

7.1及以下版本安装swoole扩展必须手动编译,7.2以上才能pecl自动安装

/opt/php/bin/pecl channel-update pecl.php.net	#更新一下镜像源,避免下载新版本出错
/opt/php/bin/pecl install swoole
例2:手动编译安装redis扩展
wget http://pecl.php.net/get/redis-5.3.7.tgz
tar xf redis-5.3.7
cd redis-5.3.7
/opt/php/bin/phpize
./configure --with-php-config=/opt/php/bin/php-config
make -j4 && make -j4 install

#开启扩展,并验证
echo -e "[swoole]\nextension=swoole.so\nswoole.use_shortname=off\n" > /opt/php/etc/php.d/swoole.ini
php --ri swoole

image

例3:编译php内置的扩展

已经在运行的php有时候不能再重新编译,可以找到同版本的源码,然后编译需要的内置扩展

cd /opt/php-7.3.30/ext/opcache
/opt/php/bin/phpize
./configure --with-php-config=/opt/php/bin/php-config
make -j4 && make -j4 install
echo -e "[opcache]\nzend_extension=opcache.so\n" > /opt/php/etc/php.d/opcache.ini
php -i|grep opcache.enable

image

posted @ 2022-06-09 11:49  suyanhj  阅读(220)  评论(0)    收藏  举报