# Configurer PHP ([php-fpm](https://cwiki.apache.org/confluence/display/HTTPD/PHP-FPM))(Apache 2.4+):

#### php.ini

Mac intel:

```bash
$ nano /usr/local/etc/php/7.4/php.ini
$ nano /usr/local/etc/php/8.2/php.ini
```

Mac M1:

```bash
$ nano /opt/homebrew/etc/php/7.4/php.ini
$ nano /opt/homebrew/etc/php/8.2/php.ini
```



#### Modifier le httpd.conf:

Mac intel:

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

Mac M1:

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

Dans *httpd.conf*, dé-commenter les lignes:

```http
LoadModule proxy_module libexec/mod_proxy.so 
LoadModule proxy_fcgi_module libexec/mod_proxy_fcgi.so 
LoadModule rewrite_module libexec/mod_rewrite.so
```

et supprimer / commenter les lignes `php_module`:

Mac intel:

```http
#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:

```http
#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
```

Puis:

mac intel, remplacer:

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

par

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

<VirtualHost *:*>
   ProxyPassMatch "^/(.*\.php(/.*)?)$" "fcgi://127.0.0.1:9000/usr/local/var/www/$1"
</VirtualHost>
<FilesMatch \.php$>
    # 2.4.10+ can proxy to unix socket
    # SetHandler "proxy:unix:/var/run/php5-fpm.sock|fcgi://localhost/"
    # Else we can just use a tcp socket:
    SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
```

mac M1, remplacer:

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

par

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

<VirtualHost *:*>
   ProxyPassMatch "^/(.*\.php(/.*)?)$" "fcgi://127.0.0.1:9000/opt/homebrew/local/var/www/$1"
</VirtualHost>
<FilesMatch \.php$>
    # 2.4.10+ can proxy to unix socket
    # SetHandler "proxy:unix:/var/run/php5-fpm.sock|fcgi://localhost/"
    # Else we can just use a tcp socket:
    SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
```



#### Changer le port:

```bash
# mac M1
$ nano /opt/homebrew/etc/php/8.0/php-fpm.d/www.conf

# mac intel
$ nano /usr/local/etc/php/8.0/php-fpm.d/www.conf
```

Remplacer

```ini
user = _www
group = _www

listen = 127.0.0.1:9000
```

par 

```ini
user = bruno
group = staff

listen = 127.0.0.1:9080
```

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

Remplacer

```http
<VirtualHost *:*>
   ProxyPassMatch "^/(.*\.php(/.*)?)$" "fcgi://127.0.0.1:9000/opt/homebrew/local/var/www/$1"
</VirtualHost>
<FilesMatch \.php$>
    # 2.4.10+ can proxy to unix socket
    # SetHandler "proxy:unix:/var/run/php5-fpm.sock|fcgi://localhost/"
    # Else we can just use a tcp socket:
    SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
```

par

```http
<VirtualHost *:*>
   ProxyPassMatch "^/(.*\.php(/.*)?)$" "fcgi://127.0.0.1:9080/opt/homebrew/local/var/www/$1"
</VirtualHost>
<FilesMatch \.php$>
    # 2.4.10+ can proxy to unix socket
    # SetHandler "proxy:unix:/var/run/php5-fpm.sock|fcgi://localhost/"
    # Else we can just use a tcp socket:
    SetHandler "proxy:fcgi://127.0.0.1:9080"
</FilesMatch>
```



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



### PHP interactif:

Executer PHP dans le terminal.

```bash
$ php -a
Interactive shell

php > $var = 'ABCDEFGH:/MNRPQR/';
php > echo "Original: $var<hr />\n";
Original: ABCDEFGH:/MNRPQR/<hr />
```



### Changer de version:

*PHP switcher script*: 

`sphp.sh`

https://gist.github.com/rozsival/10289d1e2006c68009ace0478306ecd2

```bash
#!/usr/bin/env bash
latest="8.0"
versions=("7.4" "8.1" "$latest")
valid=$(printf "|%s" "${versions[@]}")
switch="$1"

ERROR=$(tput setaf 1)
SUCCESS=$(tput setaf 2)

if [ -z "$switch" ]
    then
        echo "${ERROR}✖ PHP version required (${valid:1})"
        exit 1
fi

if [[ ! " ${versions[@]} " =~ " ${switch} " ]]
    then
        printf "${ERROR}✖ Invalid PHP version (valid: ${valid:1})"
        exit 1
fi

printf "⇆ Switching PHP to version $switch\n\n"

php="php@$switch"
if [ "$switch" == "$latest" ] ; then php="php" ; fi

for v in ${versions[*]}
    do    
        service="php@$v"
        pattern="$service"
        if [ "$v" == "$latest" ] ; then pattern="php[^@]" ; fi
        status=$(brew services | grep "$pattern" | grep "started")
        if [ ! -z "$status" ] ; then brew services stop "$service" ; fi
        brew unlink "$service"
done

brew link --overwrite --force "$php"
brew services start "$php"

printf "\n${SUCCESS}✔ PHP switched to version $switch"
exit 0

```

Installation Mac intel:

```bash
# Dans /usr/local/bin/sphp

$ chmod +x /usr/local/bin/sphp
```

Installation Mac M1:

```bash
# /opt/homebrew/bin/sphp

$ chmod +x /opt/homebrew/bin/sphp
```

Utilisation:

```bash
$ sphp 7.4
```

https://kevdees.com/macos-11-big-sur-nginx-setup-multiple-php-versions/
