# php



### Installer PHP (homebrew)

#### Installation:

Ajouter le tap:

```bash
$ brew tap shivammathur/php
$ brew update
```

Installer  différentes versions de PHP:

```bash
$ brew install shivammathur/php/php@7.4  # installe également la formule php@7.4
$ brew link --overwrite --force php@7.4
```

```bash
$ brew install shivammathur/php/php  # installe également la formule php (8.0)
$ brew link --overwrite --force php
```



### Configurer php sous serveur Apache:

1. [mod-php](mod-php.md) 

2. [php-fpm](php-fpm.md) 



### Redémarrer PHP:

```bash
brew services restart shivammathur/php/php				# php 8.0
brew services restart shivammathur/php/php@7.4
brew services restart shivammathur/php/php@8.1
brew services restart shivammathur/php/php@8.2
```



### Redémarrer le serveur web:

- pour Apache (httpd): `brew services restart httpd`
- pour Nginx: `brew services restart nginx`

Valider PHP:

```bash
echo "<?php phpinfo();" > ~/Sites/info.php
```

et aller sur http://localhost/info.php

- mod-php: `Server API: Apache 2.0 Handler`
- php-fpm: `Server API: FPM/FastCGI`

Si PHP n'est pas interprété, redémarrer le mac.



### Version de PHP:

```bash
$ php -v
PHP 7.4.22 (cli) (built: Jul 27 2021 15:38:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.22, Copyright (c), by Zend Technologies
    with Xdebug v3.0.4, Copyright (c) 2002-2021, by Derick Rethans
```

#### PHP Info:

```bash
$ php -i
```



### Logs:

```bash
/opt/homebrew/var/log/php-fpm.log
```

/opt/homebrew/var/php_errors.log

### Extensions:

#### Installer une extension (depuis Homebrew):

Ajouter le tap:

```bash
$ brew tap shivammathur/php
$ brew update
```

Installer  les [différentes extensions](https://github.com/shivammathur/homebrew-extensions):

```bash
$ brew install xdebug@8.0

# Fichier config
/opt/homebrew/etc/php/8.0/conf.d/xdebug.ini	# M1
/usr/local/etc/php/8.0/conf.d/xdebug.ini		# intel
```

```bash
$ nano /opt/homebrew/etc/php/8.0/conf.d/xdebug.ini
```

```php
[xdebug]
zend_extension="/opt/homebrew/opt/xdebug@8.0/xdebug.so"
xdebug.mode=debug
```



```bash
$ brew install apcu@8.0

# Fichier config
/opt/homebrew/etc/php/8.0/conf.d/apcu.ini	# M1
/usr/local/etc/php/8.0/conf.d/apcu.ini		# intel
```

```bash
$ nano /opt/homebrew/etc/php/8.0/conf.d/apcu.ini
```

```php
[apcu]
extension="/opt/homebrew/opt/apcu@8.0/apcu.so"
apc.enabled=1
apc.shm_size=64M
apc.ttl=7200
apc.enable_cli=1
```



```bash
$ brew install imagick@8.0

# Fichier config
/opt/homebrew/etc/php/8.0/conf.d/imagick.ini	# M1
/usr/local/etc/php/8.0/conf.d/imagick.ini		  # intel
```

```bash
$ nano /opt/homebrew/etc/php/8.0/conf.d/imagick.ini
```

```php
[imagick]
extension="/opt/homebrew/opt/imagick@8.0/imagick.so"
```



```bash
$ brew install shivammathur/extensions/memcached@8.1

# Fichier config
/opt/homebrew/etc/php/8.1/conf.d/30-memcached.ini	# M1
/usr/local/etc/php/8.1/conf.d/memcached.ini		  	# intel

# Memcached dépend de 2 extensions
/opt/homebrew/etc/php/8.1/conf.d/20-igbinary.ini
/opt/homebrew/etc/php/8.1/conf.d/20-msgpack.ini
# qui doivent être chargées avant, d'où la priorité 20- pour igbinary et msgpack et 30- pour memcached.
```

Si la priorité n'est pas respectée:

```bash
/opt/homebrew/etc/php/8.1/conf.d ls

/opt/homebrew/etc/php/8.1/conf.d/igbinary.ini
/opt/homebrew/etc/php/8.1/conf.d/msgpack.ini
/opt/homebrew/etc/php/8.1/conf.d/memcached.ini
```

on a une erreur:

```php
PHP Startup: Unable to load dynamic library '/opt/homebrew/opt/memcached@8.1/memcached.so'
symbol not found in flat namespace '_php_msgpack_serialize')

PHP Startup: Unable to load dynamic library '/opt/homebrew/opt/memcached@8.1/memcached.so'
symbol not found in flat namespace '_php_igbinary_serialize')
```



```bash
$ nano /opt/homebrew/etc/php/8.1/conf.d/30-memcached.ini
```

```php
[memcached]
extension="/opt/homebrew/opt/memcached@78.1/memcached.so"
```



#### Installer une extension (PECL)

On peut également installer des extensions avec [PECL](https://pecl.php.net):

```bash
$ pecl channel-update pecl.php.net
```

<u>mcrypt:</u>

```bash
$ brew install mcrypt

$ pecl install mcrypt
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
```

```bash
$ pecl install mcrypt

libmcrypt prefix? [autodetect] : /opt/homebrew

Build process completed successfully
Installing '/opt/homebrew/Cellar/php/8.0.8_2/pecl/20200930/mcrypt.so'
install ok: channel://pecl.php.net/mcrypt-1.0.4
Extension mcrypt enabled in php.ini
```

<u>Yaml:</u>

```bash
$ brew install libyaml

$ pecl install yaml
configure: error: Please install libyaml
```

```bash
$ pecl install yaml

Please provide the prefix of libyaml installation [autodetect] : /opt/homebrew

Build process completed successfully
Installing '/opt/homebrew/Cellar/php/8.0.8_2/pecl/20200930/yaml.so'
install ok: channel://pecl.php.net/yaml-2.2.1
Extension yaml enabled in php.ini
```

```bash
echo $(brew --prefix yaml) | pecl install yaml
```



PECL a un souci avec l'installation homebrew des macs M1:

https://stackoverflow.com/a/68138560/12431632



Chaque version de PHP a son propre répertoire d'extensions:

Mac intel:

```bash
/usr/local/lib/php/pecl/20160303 ← 7.1
/usr/local/lib/php/pecl/20170718 ← 7.2
/usr/local/lib/php/pecl/20180731 ← 7.3
/usr/local/lib/php/pecl/20190902 ← 7.4
/usr/local/lib/php/pecl/20200930 ← 8.0
```

Mac M1:

```bash
/opt/homebrew/lib/php/pecl/20160303 ← 7.1
/opt/homebrew/lib/php/pecl/20170718 ← 7.2
/opt/homebrew/lib/php/pecl/20180731 ← 7.3
/opt/homebrew/lib/php/pecl/20190902 ← 7.4
/opt/homebrew/lib/php/pecl/20200930 ← 8.0
```

Désinstaller une extension:

```bash
$ pecl uninstall mcrypt
Extension mcrypt disabled in php.ini
uninstall ok: channel://pecl.php.net/mcrypt-1.0.4
```



#### Installer une extension (depuis les sources):

<u>ssh2:</u>

```bash
# Installation de ssh2:
# https://pecl.php.net/package/ssh2

$ brew install libssh2

$ cd ~/Downloads
$ wget https://pecl.php.net/get/ssh2-1.3.1.tgz
$ tar zxvf ssh2-1.3.1.tgz
$ cd ssh2-1.3.1
$ phpize
$ ./configure

checking for ssh2 files in default path... not found
configure: error: The required libssh2 library was not found.  You can obtain that package from http://sourceforge.net/projects/libssh2/

# libssh2 est dans /opt/homebrew mais attendue dans /usr/local

$ ./configure --with-ssh2=$(brew --prefix libssh2)

$ make
...
Libraries have been installed in:
   /Users/bruno/Downloads/ssh2-1.3.1/modules

$ make install
Installing shared extensions:     /opt/homebrew/Cellar/php@7.4/7.4.22/pecl/20190902/
```

```bash
$ nano /opt/homebrew/etc/php/7.4/conf.d/ssh2.ini
```

```php
[ssh2]
extension="/opt/homebrew/Cellar/php@7.4/7.4.22/pecl/20190902/ssh2.so"
```



<u>Yaml:</u>

```bash
# Installation de yaml:
# https://pecl.php.net/package/yaml

$ brew install libyaml

$ cd ~/Downloads
$ wget https://pecl.php.net/get/yaml-2.2.1.tgz
$ tar zxvf yaml-2.2.1.tgz
$ cd yaml-2.2.1/
$ phpize
$ ./configure

checking for yaml headers... not found
configure: error: Please install libyaml

# libyaml est dans /opt/homebrew mais attendue dans /usr/local

$ ./configure --with-yaml=$(brew --prefix libyaml)

$ make
...
Libraries have been installed in:
   /Users/bruno/Downloads/yaml-2.2.1/modules

$ make install
Installing shared extensions:     /opt/homebrew/Cellar/php@7.4/7.4.22/pecl/20190902/
```

```bash
$ nano /opt/homebrew/etc/php/7.4/conf.d/yaml.ini
```

```php
[yaml]
extension="/opt/homebrew/Cellar/php@7.4/7.4.22/pecl/20190902/yaml.so"
```



<u>mcrypt</u>:

```bash
# Installation de mcrypt:
# https://pecl.php.net/package/mcrypt

$ brew install libmcrypt

$ cd ~/Downloads
$ wget https://pecl.php.net/get/mcrypt-1.0.4.tgz
$ tar zxvf mcrypt-1.0.4.tgz
$ cd mcrypt-1.0.4/
$ phpize
$ ./configure

configure: error: mcrypt.h not found. Please reinstall libmcrypt.

# libmcrypt est dans /opt/homebrew mais attendue dans /usr/local

./configure --with-mcrypt=$(brew --prefix libmcrypt)

$ make
...
Libraries have been installed in:
   /Users/bruno/Downloads/mcrypt-1.0.4/modules

$ make install
Installing shared extensions:     /opt/homebrew/Cellar/php@7.4/7.4.22/pecl/20190902/
```

```bash
$ nano /opt/homebrew/etc/php/7.4/conf.d/mcrypt.ini
```

```php
[mcrypt]
extension="/opt/homebrew/Cellar/php@7.4/7.4.22/pecl/20190902/mcrypt.so"
```



Chaque version de PHP a son propre répertoire d'extensions:

Mac intel:

```bash
/usr/local/Cellar/php@7.4/7.4.22/pecl/20190902/
```

Mac M1:

```bash
/opt/homebrew/Cellar/php@7.4/7.4.22/pecl/20190902/
```

