如何在CentOS 7上从头开始安装PHP 7.4的zip扩展?

时间:2020-05-12 11:03:30

标签: php centos zip centos7 libzip

我有一个PHP 7.4.2安装,它是通过从头开始进行编译来安装的。我使用的configure命令如下:

'./configure' '--with-apxs2=/usr/bin/apxs' '--with-curl=/usr' '--with-gd' '--with-gettext' '--with-jpeg-dir=/usr' '--with-freetype-dir=/usr' '--with-openssl' '--with-mcrypt=/usr/local/lib' '--with-mhash' '--with-mysql=mysqlnd' '--with-mysqli=mysqlnd' '--with-pcre-regex' '--with-pear' '--with-png-dir=/usr' '--with-xsl' '--with-zlib' '--with-zlib-dir=/usr' '--with-iconv' '--enable-bcmath' '--enable-calendar' '--enable-exif' '--enable-ftp' '--enable-gd-native-ttf' '--enable-soap' '--enable-sockets' '--enable-zip'

现在,当我尝试使用ZipArchive类时,我意识到没有安装或启用zip扩展名。它没有显示在我的phpinfo()中,并且代码显示了错误

Fatal error: Class 'ZipArchive' not found

我认为添加zip扩展的configure选项在PHP 7.4版本中已更改,我应该使用--with-zip而不是--enable-zip

我尝试使用pecl安装扩展程序,但返回以下错误:

checking libzip... yes checking PHP version... 7.4 checking for pkg-config... /bin/pkg-config checking for libzip... not found configure: error: Please reinstall the libzip distribution

我尝试从pecl存档安装扩展名,并且返回了configure命令,但出现了相同的错误。我在CentOS 7上这样做,一些帖子建议安装libzip-devel软件包。但是它仅作为第三方存储库的一部分提供。因为这是生产环境,所以我也不能这样做。

如果我再次运行configure命令从头开始重新安装PHP,它将安装扩展程序吗?这会影响我现有的任何设置吗?我已经安装并启用了SVN扩展

3 个答案:

答案 0 :(得分:2)

zip扩展需要libzip库。因此,您可以从源代码进行编译。但是libzip库需要zlib库。为了确保您不会错过任何重要的事情,如果我向您展示我的做法,那是最好的选择。

这就是我在centos 7服务器上从源代码编译7.4的方式

首先,我要安装软件包更新并安装缺少的软件包:

<id>assembly-id</id>

安装CMake:

sudo yum update
sudo yum -y install lzip oniguruma oniguruma-devel

# OR if you cant find the packages you can use RPMs - example:
curl https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/o/oniguruma-5.9.5-3.el7.x86_64.rpm --output oniguruma-5.9.5-3.el7.x86_64.rpm
rpm -Uvh oniguruma-5.9.5-3.el7.x86_64.rpm

编译zlib:

cd
# installing compiled cmake
wget -c https://cmake.org/files/LatestRelease/cmake-3.16.0-Linux-x86_64.tar.gz
tar zxvf cmake-3.*

# OR install it from source:
curl -OL https://github.com/Kitware/CMake/archive/v3.16.5.tar.gz
tar zxvf v3.16.5.tar.gz

cd CMake-3.*
./bootstrap --prefix=/usr/local
sudo make -j2
sudo make install
# maybe this is not required:
sudo cp ~/CMake-3.16.5/bin/cmake /usr/bin/

为zip扩展名编译libzip:

wget -c http://www.zlib.net/zlib-1.2.11.tar.gz
tar zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
sudo make -j2
sudo make install

复制构建的文件并将其添加到变量中

cd
wget -c https://libzip.org/download/libzip-1.6.1.tar.gz
tar zxvf libzip-1.6.1.tar.gz
cd libzip*
mkdir build
cd build
cmake ..
sudo make -j2
make test
sudo make install

编译php:

如果已经通过软件包安装程序安装了php,则将其删除。

sudo cp /home/centos/libzip-1.6.1/build/libzip.pc /usr/local/lib64/pkgconfig/libzip.pc

# Also add this to the env variables for each session - later in the guide
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib64/pkgconfig/

有关编译的其他信息:https://shaunfreeman.name/compiling-php-7-on-centos

sudo yum-config-manager --disable remi-php74
sudo yum remove php php-cli php-common php-opcache php-mcrypt php-gd php-curl php-fpm php-dom php-intl php-pecl-mongodb php-mbstring php-xml php-pear php-devel php-pecl-zip

make -j2定义您要使用多少核心线程进行编译。

您应该注意的另一行是

# for some reason libzip-last wont do what you need, so you need to compile it as it's written above
sudo yum install httpd-devel git gcc gcc-c++ readline-devel libxml2-devel libzip-last libxslt-devel pkgconfig openssl-devel bzip2-devel curl-devel libpng-devel libjpeg-devel libXpm-devel freetype-devel gmp-devel libmcrypt-devel mariadb-devel aspell-devel recode-devel autoconf bison re2c libicu-devel
sudo mkdir /usr/local/php7
cd

# OPTION A
git clone https://github.com/php/php-src.git
cd php-src
git checkout PHP-7.4.5

# OPTION B
# use the release tar instead of the source branch
curl -OL https://github.com/php/php-src/archive/php-7.4.5.tar.gz
tar zxvf php-7.4.5.tar.gz

cd php-src-php-7.4.5
./buildconf --force
./configure --prefix=/usr/local/php7 \
    --with-config-file-path=/usr/local/php7/etc \
    --with-config-file-scan-dir=/usr/local/php7/etc/conf.d \
    --with-apxs2=/usr/bin/apxs \
    --enable-bcmath \
    --enable-fpm \
    --with-bz2 \
    --with-curl \
    --disable-phpdbg \
    --disable-phpdbg-webhelper \
    --enable-filter \
    --enable-gd \
    --with-freetype \
    --with-jpeg \
    --enable-intl \
    --with-mysql-sock=/var/lib/mysql/mysql.sock \
    --with-openssl \
    --enable-simplexml \
    --enable-xmlreader \
    --enable-xmlwriter \
    --enable-pcntl \
    --enable-shmop \
    --enable-sysvmsg \
    --enable-sysvsem \
    --enable-sysvshm \
    --with-xsl \
    --enable-opcache \
    --enable-calendar \
    --enable-sockets \
    --enable-mbstring \
    --with-readline \
    --with-zlib=~/libzip-1.6.1/build/ \
    --with-zip \
    --enable-exif \
    --with-gettext \
    --without-sqlite3 \
    --with-mhash \
    --enable-maintainer-zts
sudo make -j2
sudo make install
sudo mkdir /usr/local/php7/etc/conf.d
sudo cp -v ./php.ini-production /usr/local/php7/lib/php.ini
sudo cp -v ./sapi/fpm/www.conf /usr/local/php7/etc/php-fpm.d/www.conf
sudo cp -v ./sapi/fpm/php-fpm.conf /usr/local/php7/etc/php-fpm.conf
sudo vim /usr/local/php7/etc/conf.d/modules.ini

在配置过程中,使用您自己需要的软件包。

php主要从此处运行:--with-zlib=~/libzip-1.6.1/build/ \

您构建的php将位于此处:/usr/bin/php

所以您可能需要覆盖它:

sudo mv / usr / local / php7 / bin / php / usr / bin / php

这还不是全部,但是它涵盖了zip扩展名的每个重要部分。

答案 1 :(得分:1)

在CentOS中,您有yum

您可以执行以下操作,它应该可以工作。我只是用PHP 7.4在新的CentOS中发短信。

您不需要重新安装PHP,只需要添加扩展名即可。

安装依赖项(您将需要以root身份运行)

yum install pcre-devel gcc zlib zlib-devel  libzip-devel make

使用PECL安装zip

pecl install zip

编辑/etc/php.ini以注册新模块

(您将需要以root身份打开)

extension=zip.so;

答案 2 :(得分:0)

尝试一下:

yum install php74-libzip.x86_64
yum install php-pecl-zip