How to install zip extension for PHP 7.4 compiled from scratch on CentOS 7?

Happy Coder picture Happy Coder · May 12, 2020 · Viewed 13.9k times · Source

I have a PHP 7.4.2 installation, which is installed by compiling from the scratch. The configure command I used is as given below :

'./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'

Now when I tried to use the ZipArchive class, I realized that the zip extension is not installed or enabled. It is not showing in my phpinfo() and the code is showing an error

Fatal error: Class 'ZipArchive' not found

I think the configure option for adding the zip extension has been changed in PHP 7.4 versions and I should have used --with-zip instead of --enable-zip.

I tried using pecl to install the extension and it is returning in the following error:

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

I tried to install the extension from the pecl archive and the configure command returned in the same error. I am doing this on CentOS 7 and some posts are suggesting to install libzip-devel package. But it is only available as part of third party repository. As this is a production environment I can't do that either.

If I reinstall PHP from scratch running the configure command again, will it install the extension ? Will it affect any of my existing setups ? I have installed and enabled SVN extnsion

Answer

Random Name picture Random Name · May 15, 2020

The zip extension requires the libzip library. So you can compile it from source. However the libzip library requires the zlib library. To make sure that you won't miss anything important, the best if I show you how I'm doing it.

This is how I compile 7.4 from source on our centos 7 servers

First I'm installing package update and installing the missing packages:

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

Installing the CMake:

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/

Compile the zlib:

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

Compile the libzip for the zip extension:

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

Copy the built file and add it to the variables

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/

Compiling php:

if php is already installed through a package installer, then delete it.

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

Extra info for compiling: https://shaunfreeman.name/compiling-php-7-on-centos

# 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

the make -j2 defines how much core threads you would like to use to compile.

The other line you should notice is this one:

--with-zlib=~/libzip-1.6.1/build/ \

During the configuration use the package of your own need.

php is running primarily from here: /usr/bin/php

And your built php will be located here: /usr/local/php7/bin/php

so you might will need overwrite it:

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

That's not all, but it covers every important part with the zip extension.