Files
mkdocs/docs/macos/webserver/php.md
2021-08-06 13:32:29 +02:00

8.7 KiB

Installer PHP (homebrew)

Installation:

Ajouter le tap:

$ brew tap shivammathur/php
$ brew update

Installer différentes versions de PHP:

$ brew install shivammathur/php/php@7.4
$ brew link --overwrite --force php@7.4
$ brew install shivammathur/php/php@8.0
$ brew link --overwrite --force php@8.0

Configurer PHP:

Mac intel:

$ nano /usr/local/etc/php/7.4/php.ini
$ nano /usr/local/etc/php/8.0/php.ini

Mac M1:

$ nano /opt/homebrew/etc/php/7.4/php.ini
$ nano /opt/homebrew/etc/php/8.0/php.ini
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Europe/Paris


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Error handling and logging ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

error_log = php_errors.log


;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

memory_limit = 512M

Modifier le httpd.conf:

Mac intel:

$ nano /usr/local/etc/httpd/httpd.conf

Mac M1:

$ nano /opt/homebrew/etc/httpd/httpd.conf

Dans httpd.conf, ajouter les lignes:

Mac intel:

LoadModule php7_module /usr/localopt/php@7.4/lib/httpd/modules/libphp7.so
#LoadModule php_module /usr/local/opt/php@8.0/lib/httpd/modules/libphp.so

Mac M1:

LoadModule php7_module /opt/homebrew/opt/php@7.4/lib/httpd/modules/libphp7.so
#LoadModule php_module /opt/homebrew/opt/php@8.0/lib/httpd/modules/libphp.so

intel / M1:

Remplacer:

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

par

<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

Redémarrer le serveur web:

  • pour Apache (httpd): brew services restart httpd
  • pour Nginx: brew services restart nginx

Valider PHP:

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

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

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

Changer de version:

Méthode 1:

$ brew unlink php && brew link --overwrite --force php@7.4

Méthode 2:

PHP switcher script

Installation Mac intel:

$ curl -L https://gist.githubusercontent.com/rhukster/f4c04f1bf59e0b74e335ee5d186a98e2/raw/0c36a5067fbd63e6a36700a6aaa119df0836bdfc/sphp.sh > /usr/local/bin/sphp
$ chmod +x /usr/local/bin/sphp

Installation Mac M1:

$ curl -L https://raw.githubusercontent.com/vadimbk/sphp/main/sphp > /opt/homebrew/bin/sphp
$ chmod +x /opt/homebrew/bin/sphp

Utilisation:

$ sphp 7.4

Version de PHP:

$ 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:

$ php -i

Logs:

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

Extensions:

Installer une extension (depuis Homebrew):

Ajouter le tap:

$ brew tap shivammathur/php
$ brew update

Installer les différentes extensions:

$ 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
$ nano /opt/homebrew/etc/php/8.0/conf.d/xdebug.ini
[xdebug]
zend_extension="/opt/homebrew/opt/xdebug@8.0/xdebug.so"
xdebug.mode=debug
$ brew install apcu@8.0

# Fichier config
/opt/homebrew/etc/php/8.0/conf.d/apcu.ini
$ nano /opt/homebrew/etc/php/8.0/conf.d/apcu.ini
[apcu]
extension="/opt/homebrew/opt/apcu@8.0/apcu.so"
apc.enabled=1
apc.shm_size=64M
apc.ttl=7200
apc.enable_cli=1
$ brew install imagick@8.0

# Fichier config
/opt/homebrew/etc/php/8.0/conf.d/imagick.ini
$ nano /opt/homebrew/etc/php/8.0/conf.d/imagick.ini
[imagick]
extension="/opt/homebrew/opt/imagick@8.0/imagick.so"
$ brew install memcached@8.0

# Fichier config
/opt/homebrew/etc/php/7.4/conf.d/memcached.ini
$ nano /opt/homebrew/etc/php/7.4/conf.d/memcached.ini
[memcached]
extension="/opt/homebrew/opt/memcached@7.4/memcached.so"

Installer une extension (PECL)

On peut également installer des extensions avec PECL:

$ pecl channel-update pecl.php.net

mcrypt:

$ brew install mcrypt

$ pecl install mcrypt
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
$ 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

Yaml:

$ brew install libyaml

$ pecl install yaml
configure: error: Please install libyaml
$ 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

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:

/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:

/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:

$ 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):

ssh2:

# 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/
$ nano /opt/homebrew/etc/php/7.4/conf.d/ssh2.ini
[ssh2]
extension="/opt/homebrew/Cellar/php@7.4/7.4.22/pecl/20190902/ssh2.so"

Yaml:

# 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/
$ nano /opt/homebrew/etc/php/7.4/conf.d/yaml.ini
[yaml]
extension="/opt/homebrew/Cellar/php@7.4/7.4.22/pecl/20190902/yaml.so"

mcrypt:

# 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/
$ nano /opt/homebrew/etc/php/7.4/conf.d/mcrypt.ini
[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:

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

Mac M1:

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