Skip to content

Instantly share code, notes, and snippets.

@ewilan-riviere
Last active April 18, 2024 21:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ewilan-riviere/3f4efd752905abe24fd1cd44412d9db9 to your computer and use it in GitHub Desktop.
Save ewilan-riviere/3f4efd752905abe24fd1cd44412d9db9 to your computer and use it in GitHub Desktop.
PHP: install swoole, pcov, redis, imagick, rar, sqlsrv

PHP install Swoole, PCOV, ImageMagick, WinRAR and SQL Server

For Debian, installation use apt, to use pecl, you could use this package.

sudo apt install -y php-pear

For macOS, installation use Homebrew, you could use some packages.

brew install pkg-config zlib

For Windows, you could use this guide: https://gist.github.com/ewilan-riviere/8210ba3dc4cf77e57c4069ae0008d3f1


About

PHP extensions can be installed with PECL or directly from repository compilation. If you change your PHP version, you will have to reinstall all extensions you need once (you could switch between PHP versions and extensions will stay installed, by version).

Today, some extensions are maintained and some are not, check errors if you have it and check issues on extensions GitHub repositories to find solutions. For extensions listed here, you can install it with PHP 8.2.

Pear

Check pear.php.net.

PEAR is a framework and distribution system for reusable PHP components.

PHP can use Pear with PECL package pear.

Installation

Debian

sudo apt install -y php-pear

macOS

brew install pkg-config zlib
curl -O http://pear.php.net/go-pear.phar
sudo php -d detect_unicode=0 go-pear.phar
sudo pecl install pear/pear-core-minimal

Swoole

Check www.swoole.com.

Swoole is an event-driven, asynchronous, coroutine-based concurrency library with high performance for PHP.

PHP can use Swoole with PECL package swoole.

Installation

Debian & macOS

Install with default options.

sudo apt install -y g++ make
git clone https://github.com/swoole/swoole-src.git && \
cd swoole-src
phpize
./configure
sudo make
sudo make install

You can use pecl install swoole, install from source is recommanded from GitHub readme for advanced users.

Add manually extension.

Check if installed

php -m | grep swoole

PCOV

Check github.com.

PCOV - CodeCoverage compatible driver for PHP

PHP can use ImageMagick with PECL package imagick.

Installation

Make sure you have pcre2 installed on macOS, you can check 'pcre2.h' file not found if you have errors.

git clone https://github.com/krakjoe/pcov.git
cd pcov
phpize
./configure --enable-pcov
make
make test
make install # or sudo make install

Now you have to add extension to your php.ini, you can check Add manually extension.

You can check if extension is installed with php -m | grep pcov.

Warning

If you don't have pcov in php -m, you have to add extension=pcov to your php.ini file.

Windows

Use this guide https://gist.github.com/ewilan-riviere/8210ba3dc4cf77e57c4069ae0008d3f1#pcov.


Redis

Check redis.io.

The open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker.

PHP can use Redis with PECL package redis or composer package predis/predis.

Installation

Debian from source

Download redis-stable

wget https://download.redis.io/redis-stable.tar.gz

Compile redis-stable

tar -xzvf redis-stable.tar.gz
cd redis-stable
make

Install redis-stable

sudo make install

Run redis-server

redis-server

Create redis.conf

sudo cp ./redis.conf /etc/redis.conf

Create service

sudo vim /etc/systemd/system/redis.service
[Unit]
Description=Redis Server
After=network.target

[Service]
ExecStart=/usr/local/bin/redis-server /etc/redis.conf
ExecStop=/usr/local/bin/redis-server shutdown
Restart=always

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable redis.service
sudo systemctl start redis.service
sudo systemctl status redis.service

Debian

sudo apt install lsb-release curl gpg -y
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt update
sudo apt install redis -y
sudo service redis start

macOS

brew install redis
brew services start redis

Extension

You can install extension OR composer package.

Extension

git clone https://github.com/phpredis/phpredis.git
cd phpredis
phpize
./configure [--enable-redis-igbinary] [--enable-redis-msgpack] [--enable-redis-lzf [--with-liblzf[=DIR]]] [--enable-redis-zstd]
make && make install
sudo echo "extension=redis.so" >> /etc/php.ini

Composer package

Predis is a Redis client written entirely in PHP and does not require any additional extensions.

composer require predis/predis

Check

Check if Redis is installed (only for PECL extension)

php -m | grep redis

ImageMagick

Check imagemagick.org.

ImageMagick, invoked from the command line as magick, is a free and open-source cross-platform software suite for displaying, creating, converting, modifying, and editing raster images. Created in 1987 by John Cristy, it can read and write over 200 image file formats. It is widely used in open-source applications.

PHP can use ImageMagick with PECL package imagick.

Installation

Debian

sudo apt install -y imagemagick ghostscript libmagickwand-dev
git clone https://github.com/Imagick/imagick
cd imagick
phpize && ./configure
make
make install # or sudo make install

macOS

brew install imagemagick ghostscript

Add to path

echo 'export MAGICK_HOME=/opt/homebrew/opt/imagemagick/' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt/imagemagick/bin:$PATH"' >> ~/.zshrc
git clone https://github.com/Imagick/imagick
cd imagick
phpize && ./configure
make
make install # or sudo make install

Windows

Use this guide https://gist.github.com/ewilan-riviere/8210ba3dc4cf77e57c4069ae0008d3f1#imagick.


Check if extension is installed with php -m | grep imagick.

Warning

If you have an error like this

attempt to perform an operation not allowed by the security policy `PDF' @ error/constitute.c/IsCoderAuthorized/408.

Check you have ghostscript installed with gs --version And if it's okay, open /etc/ImageMagick-6/policy.xml to remove some lines

<!-- disable ghostscript format types -->
<!-- <policy domain="coder" rights="none" pattern="PS" /> -->
<!-- <policy domain="coder" rights="none" pattern="PS2" /> -->
<!-- <policy domain="coder" rights="none" pattern="PS3" /> -->
<!-- <policy domain="coder" rights="none" pattern="EPS" /> -->
<!-- <policy domain="coder" rights="none" pattern="PDF" /> -->
<!-- <policy domain="coder" rights="none" pattern="XPS" /> -->

WinRAR

Check www.win-rar.com

WinRAR is a trialware file archiver utility for Windows, developed by Eugene Roshal of win.rar GmbH. It can create and view archives in RAR or ZIP file formats, and unpack numerous archive file formats.

PHP can use WinRAR with PECL package rar.

WARNING rar extension is not actively maintained. With recent PHP versions, you will have some errors on installation. You have to compile by yourself the extension. Check Troubles section.

Download rar

With recent PHP versions, you could have some problem with installation from package managers.

To install package, you have to compile the package, you can find an example here.

Debian

You have to add non-free into your sources.list file, like deb http://... <distrib> main non-free and execute apt update.

sudo apt install -y rar
cd ~
git clone https://github.com/cataphract/php-rar
cd php-rar
phpize
./configure
make
make install # or sudo make install

macOS

brew install rar unrar
cd ~
git clone https://github.com/cataphract/php-rar
cd php-rar
phpize
./configure
make
make install # or sudo make install

Windows

Use this guide https://gist.github.com/ewilan-riviere/8210ba3dc4cf77e57c4069ae0008d3f1#rar.


Setup rar

You will find PHP extension here: ~/php-rar/modules/rar.so. To use it, find PECL extension directory.

pecl_path=$(pecl config-get ext_dir) # Find PECL extension directory
phpini_path=$(php -i | grep /.+/php.ini -oE) # Find php.ini path
sudo cp ~/php-rar/modules/rar.so $pecl_path # Copy extension to PECL extension directory
echo "extension=rar.so" | sudo tee -a $phpini_path # Add extension to php.ini

Now you remove local php-rar.

rm -r ~/php-rar

Note: PHP 8.2 For PHP 8.2, even manual compilation, you will have maybe some errors. A pull request offer to fix this problem, in some months PR could be accepted. But even with this bug, PHP and rar extension will work.

php -m | grep rar

If you see rar, extension is ready.


SQL Server

Check www.microsoft.com

Microsoft SQL Server is a relational database management system developed by Microsoft. As a database server, it is a software product with the primary function of storing and retrieving data as requested by other software applications—which may run either on the same computer or on another computer across a network.

PHP can use SQL Server with PECL package sqlsrv.

Download sqlsrv

Debian

sudo pecl install sqlsrv pdo_sqlsrv

macOS

brew install autoconf automake libtool

Install Microsoft ODBC

brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
HOMEBREW_ACCEPT_EULA=Y brew install msodbcsql18 mssql-tools
macOS Intel
sudo pecl install sqlsrv pdo_sqlsrv

To upgrade

sudo pecl upgrade sqlsrv
sudo pecl upgrade pdo_sqlsrv
macOS Apple Silicon
sudo CXXFLAGS="-I/opt/homebrew/opt/unixodbc/include/" LDFLAGS="-L/opt/homebrew/lib/" pecl install sqlsrv
sudo CXXFLAGS="-I/opt/homebrew/opt/unixodbc/include/" LDFLAGS="-L/opt/homebrew/lib/" pecl install pdo_sqlsrv

To upgrade

sudo CXXFLAGS="-I/opt/homebrew/opt/unixodbc/include/" LDFLAGS="-L/opt/homebrew/lib/" pecl upgrade sqlsrv
sudo CXXFLAGS="-I/opt/homebrew/opt/unixodbc/include/" LDFLAGS="-L/opt/homebrew/lib/" pecl upgrade pdo_sqlsrv

Setup sqlsrv

php -m | grep sqlsrv

If you see sqlsrv, extension is ready.

You can check pecl package versions

pecl list

Troubles

'pcre2.h' file not found

On macOS you need to install pcre2 and link it to current PHP version.

brew install pcre2

Check current versions

ls /opt/homebrew/Cellar/pcre2/
ls /opt/homebrew/Cellar/php/

Link pcre2 to current php.

ln -s /opt/homebrew/Cellar/pcre2/YOU_VERSION/include/pcre2.h /opt/homebrew/Cellar/php/YOU_VERSION/include/php/ext/pcre/pcre2.h

With script

php_version=$(php -v | grep ^PHP | awk '{print $2}')
pcre2_version=$(ls /opt/homebrew/Cellar/pcre2/)
sudo ln -s /opt/homebrew/Cellar/pcre2/$pcre2_version/include/pcre2.h /opt/homebrew/Cellar/php/$php_version/include/php/ext/pcre/pcre2.h

You can now compile again.

Add manually extension

After compilation, you have to add extension to your PHP installation.

Find your php.ini file.

php --ini

Use any editor to add extension.

sudo vim /your/path/to/php.ini

In your php.ini, add this to the first line.

extension="<extension>.so"

With script

pecl_path=$(pecl config-get ext_dir)
phpini_path=$(php -i | grep /.+/php.ini -oE)
sudo cp ./modules/<extension>.so $pecl_path
sudo echo "extension=<extension>.so" > $phpini_path

Check extension

Check if PHP can use extension.

php -m | grep <extension>

If you see extension, extension is ready.

IDE

VSCode

With PHP Intelephense, you can add imagick to your settings.json.

{
    "intelephense.stubs": ["imagick", "rar"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment