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
3
Answers
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:
Installing the CMake:
Compile the zlib:
Compile the libzip for the zip extension:
Copy the built file and add it to the variables
Compiling php:
if php is already installed through a package installer, then delete it.
Extra info for compiling: https://shaunfreeman.name/compiling-php-7-on-centos
the make -j2 defines how much core threads you would like to use to compile.
The other line you should notice is this one:
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.
In CentOS you have
yum
You can do the following and it should work. I just texted it in a fresh CentOS with PHP 7.4.
You don’t need to reinstall PHP, you just need to add the extension.
Install dependencies (you will need to run as root)
Install zip using PECL
Edit
/etc/php.ini
to register the new module(you will need to open as root)
Give a try on this: