283 lines
7.6 KiB
Markdown
283 lines
7.6 KiB
Markdown
---
|
||
categories: web
|
||
title: Howto PHP
|
||
...
|
||
|
||
* Documentation : <http://php.net/manual/en/>
|
||
* Rôle Ansible : <https://forge.evolix.org/projects/ansible-roles/repository/show/php>
|
||
|
||
[PHP](http://php.net/) est un langage de programmation populaire pour le développement web.
|
||
PHP peut s'utiliser en CLI (ligne de commande), via mod_php (module intégré à [Apache](HowtoApache)) ou via FPM (FastCGI Process Manager).
|
||
|
||
|
||
## Installation
|
||
|
||
~~~
|
||
# apt install php php-cli php-curl php-mysql php-pgsql php-mcrypt php-ldap php-imap php-gd php-ssh2 php-gettext composer libphp-phpmailer
|
||
|
||
$ php -v
|
||
PHP 7.0.19-1 (cli) (built: May 11 2017 14:04:47) ( NTS )
|
||
Copyright (c) 1997-2017 The PHP Group
|
||
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
|
||
with Zend OPcache v7.0.19-1, Copyright (c) 1999-2017, by Zend Technologies
|
||
|
||
$ composer -V
|
||
Composer version 1.2.2 2016-11-03 17:43:15
|
||
~~~
|
||
|
||
> *Note* : Pour Debian 8, on a PHP 5.6 qu'il faut installer ainsi (et il n'y a pas de package pour _composer_) :
|
||
>
|
||
> ~~~
|
||
> # apt install php5 php5-cli php5-curl php5-mysql php5-pgsql php5-mcrypt php5-ldap php5-imap php5-gd php5-ssh2 php-gettext libphp-phpmailer
|
||
> ~~~
|
||
|
||
|
||
### PHP-FPM
|
||
|
||
PHP-FPM (FastCGI Process Manager) est une façon alternative d'utiliser PHP avec [Apache](HowtoApache) ou [Nginx](HowtoNginx).
|
||
|
||
~~~
|
||
# apt install php-fpm
|
||
|
||
# systemctl status php7.0-fpm
|
||
● php7.0-fpm.service - The PHP 7.0 FastCGI Process Manager
|
||
Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor preset: enabled)
|
||
Docs: man:php-fpm7.0(8)
|
||
Main PID: 1516 (php-fpm7.0)
|
||
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
|
||
CGroup: /system.slice/php7.0-fpm.service
|
||
├─1516 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
|
||
├─1517 php-fpm: pool www
|
||
└─1518 php-fpm: pool www
|
||
~~~
|
||
|
||
> *Note* : Pour Debian 8, il faut installer ainsi :
|
||
>
|
||
> ~~~
|
||
> # apt install php5-fpm
|
||
> ~~~
|
||
|
||
|
||
### mod_php
|
||
|
||
~~~
|
||
# apt install libapache2-mod-php
|
||
~~~
|
||
|
||
> *Note* : Pour Debian 8, il faut installer ainsi :
|
||
>
|
||
> ~~~
|
||
> # apt install libapache2-mod-php5
|
||
> ~~~
|
||
|
||
|
||
|
||
## Configuration
|
||
|
||
<http://php.net/manual/en/install.fpm.configuration.php>
|
||
|
||
Fichiers de configuration :
|
||
|
||
~~~
|
||
/etc/php/7.0/
|
||
├── apache2
|
||
│ ├── conf.d
|
||
│ │ ├── [...]
|
||
│ │ └── 20-xsl.ini -> /etc/php/7.0/mods-available/xsl.ini
|
||
│ └── php.ini
|
||
├── cli
|
||
│ ├── conf.d
|
||
│ │ ├── [...]
|
||
│ │ └── 20-xsl.ini -> /etc/php/7.0/mods-available/xsl.ini
|
||
│ └── php.ini
|
||
├── fpm
|
||
│ ├── conf.d
|
||
│ │ ├── [...]
|
||
│ │ └── 20-xsl.ini -> /etc/php/7.0/mods-available/xsl.ini
|
||
│ ├── php-fpm.conf
|
||
│ ├── php.ini
|
||
│ └── pool.d
|
||
│ └── www.conf
|
||
└── mods-available
|
||
├── [...]
|
||
└── xsl.ini
|
||
~~~
|
||
|
||
### Configuration de base (php.ini)
|
||
|
||
Il existe un fichier `php.ini` distinct suivant l’utilisation de PHP : cli, mod_php et FPM.
|
||
Par exemple `/etc/php/7.0/fpm/php.ini` pour FPM.
|
||
|
||
Le fichier `/etc/php/7.0/fpm/conf.d/z-evolinux-defaults.ini` contient nos optimisations basiques :
|
||
|
||
~~~
|
||
short_open_tag = Off
|
||
expose_php = Off
|
||
display_errors = Off
|
||
log_errors = On
|
||
html_errors = Off
|
||
allow_url_fopen = Off
|
||
memory_limit = 128M
|
||
max_execution_time = 10
|
||
open_basedir = /home
|
||
disable_functions = exec, shell-exec, system, passthru, putenv, popen
|
||
~~~
|
||
|
||
### Configuration FPM
|
||
|
||
Voici les directives de base :
|
||
|
||
~~~
|
||
[global]
|
||
pid = /run/php5-fpm.pid
|
||
error_log = /var/log/php5-fpm.log
|
||
~~~
|
||
|
||
On définit ensuite un ou plusieurs *pools* FPM via `/etc/php5/fpm/pool.d/*.conf` :
|
||
|
||
~~~
|
||
[www]
|
||
listen = /var/run/php5-fpm.sock
|
||
;listen = 127.0.0.1:9000
|
||
user = www-data
|
||
group = www-data
|
||
pm = dynamic
|
||
|
||
php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f return-path@example.com
|
||
php_flag[display_errors] = off
|
||
php_admin_value[error_log] = /var/log/fpm-php.www.log
|
||
php_admin_flag[log_errors] = on
|
||
php_admin_value[memory_limit] = 32M
|
||
~~~
|
||
|
||
L'option **pm** (process manager) permet de choisir comment seront contrôlés les process fils : static, ondemand ou dynamic.
|
||
|
||
Le mode standard est **dynamic** : FPM va préparer des process en attente (au minimum 1) et les faire varier en fonction de la demande
|
||
(de façon similaire à ce que peut faire [Apache](HowtoApache#configuration-de-base)).
|
||
|
||
~~~
|
||
pm = dynamic
|
||
pm.max_children = 100
|
||
pm.start_servers = 50
|
||
pm.min_spare_servers = 20
|
||
pm.max_spare_servers = 30
|
||
pm.max_requests = 100
|
||
~~~
|
||
|
||
Avec de nombreux pools, on optera pour le mode **ondemand** qui ne prépare pas de process mais les crée à chaque demande.
|
||
D'après nos tests, les performances restent (assez étonnament) tout à fait acceptables.
|
||
|
||
~~~
|
||
pm = ondemand
|
||
pm.max_children = 100
|
||
pm.process_idle_timeout = 10s
|
||
~~~
|
||
|
||
D'autres options de FPM sont intéressantes :
|
||
|
||
~~~
|
||
slowlog = log/$pool.log.slow
|
||
request_slowlog_timeout = 5s
|
||
|
||
pm.status_path = /fpm_status
|
||
request_terminate_timeout = 60s
|
||
chroot = /home/foo
|
||
access.log = log/$pool.access.log
|
||
~~~
|
||
|
||
|
||
#### Configuration FPM avec Nginx
|
||
|
||
On configure PHP-FPM avec Nginx, par exemple dans un Virtualhost :
|
||
|
||
~~~
|
||
server {
|
||
listen 80;
|
||
server_name www.example.com example.com;
|
||
root /home/foo/www;
|
||
index index.php;
|
||
|
||
location ~ \.php$ {
|
||
try_files $uri =404;
|
||
#fastcgi_pass 127.0.0.1:9000;
|
||
fastcgi_pass unix:/var/run/php5-fpm.sock;
|
||
fastcgi_param SCRIPT_FILENAME /home/foo/www$fastcgi_script_name;
|
||
include fastcgi_params;
|
||
}
|
||
}
|
||
~~~
|
||
|
||
#### Configuration FPM avec Apache
|
||
|
||
Cela nécessite le module proxy_fcgi :
|
||
|
||
~~~
|
||
# a2enmod proxy_fcgi
|
||
Considering dependency proxy for proxy_fcgi:
|
||
Enabling module proxy.
|
||
Enabling module proxy_fcgi.
|
||
~~~
|
||
|
||
On configure PHP-FPM avec Apache, par exemple dans un VirtualHost :
|
||
|
||
~~~{.apache}
|
||
DocumentRoot /home/bench/www/
|
||
#ProxyPassMatch "^/(.*\.php(/.*)?)$" "fcgi://127.0.0.1:9000//home/foo/www/$1"
|
||
ProxyPassMatch "^/(.*\.php(/.*)?)$" "unix:/var/run/php5-fpm.sock|fcgi://localhost/home/foo/www/"
|
||
~~~
|
||
|
||
#### Configuration mod_php
|
||
|
||
## Composer
|
||
|
||
* Documentation : <https://getcomposer.org/doc/>
|
||
|
||
[Composer](https://getcomposer.org/) est un outil moderne de gestion des bibliothèques PHP, qui tend à remplacer le vieil outil [PEAR](https://pear.php.net/). Il est disponible à partir de Debian 9.
|
||
|
||
Composer s'utilise sans les droits _root_.
|
||
Il s'appuye sur la présence d'un fichier `composer.json` qui déclare les bibliothèques à installer.
|
||
|
||
~~~
|
||
$ composer about
|
||
Composer - Package Management for PHP
|
||
Composer is a dependency manager tracking local dependencies of your projects and libraries.
|
||
See https://getcomposer.org/ for more information.
|
||
|
||
echo '{"require": {"twig/extensions": "~1.0"}}' > composer.json
|
||
|
||
$ composer update
|
||
Loading composer repositories with package information
|
||
Updating dependencies (including require-dev)
|
||
- Installing symfony/polyfill-mbstring (v1.5.0)
|
||
Loading from cache
|
||
|
||
- Installing twig/twig (v2.4.3)
|
||
Loading from cache
|
||
|
||
- Installing twig/extensions (v1.5.1)
|
||
Loading from cache
|
||
|
||
twig/extensions suggests installing symfony/translation (Allow the time_diff output to be translated)
|
||
Writing lock file
|
||
Generating autoload files
|
||
|
||
$ composer show
|
||
symfony/polyfill-mbstring v1.5.0 Symfony polyfill for the Mbstring extension
|
||
twig/extensions v1.5.1 Common additional features for Twig that do not directly belong in core
|
||
twig/twig v2.4.3 Twig, the flexible, fast, and secure template language for PHP
|
||
~~~
|
||
|
||
### Composer sous Debian 8
|
||
|
||
~~~
|
||
$ wget https://getcomposer.org/installer
|
||
$ sha384sum installer
|
||
$ mkdir bin
|
||
$ php -d allow_url_fopen=On installer --install-dir=bin --filename=composer
|
||
$ php bin/composer --version
|
||
PHP Warning: putenv() has been disabled for security reasons
|
||
Composer version 1.5.1 2017-08-09 16:07:22
|
||
~~~
|
||
|
||
|