Relecture du HowtoPHP (nécessite encore du travail, notamment merge de HowtoLAMP/PHP)
This commit is contained in:
parent
c997fa899b
commit
239933f46a
227
HowtoPHP.md
Normal file
227
HowtoPHP.md
Normal file
|
@ -0,0 +1,227 @@
|
|||
---
|
||||
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-cli php-curl php-mysql php-pgsql php-mcrypt php-ldap php-imap php-gd php-ssh2 php-gettext composer
|
||||
|
||||
$ 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-cli php5-curl php5-mysql php5-pgsql php5-mcrypt php5-ldap php5-imap php5-gd php5-ssh2 php-gettext
|
||||
> ~~~
|
||||
|
||||
|
||||
### 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/"
|
||||
~~~
|
114
HowtoPHPFPM.md
114
HowtoPHPFPM.md
|
@ -1,114 +0,0 @@
|
|||
# Howto PHP-FPM
|
||||
|
||||
PHP-FPM (FastCGI Process Manager) est une façon alternative d'utiliser PHP avec [Apache](HowtoApache) ou [Nginx](HowtoNginx).
|
||||
|
||||
## Installation
|
||||
|
||||
~~~
|
||||
# apt install php5-fpm
|
||||
~~~
|
||||
|
||||
## Configuration
|
||||
|
||||
<http://php.net/manual/fr/install.fpm.configuration.php>
|
||||
|
||||
La configuration de PHP-FPM se trouve dans `/etc/php5/fpm/php-fpm.conf`.
|
||||
|
||||
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
|
||||
~~~
|
||||
|
||||
## Configuration avancée
|
||||
|
||||
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 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 avec Apache
|
||||
|
||||
~~~
|
||||
# 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/"
|
||||
~~~
|
Loading…
Reference in a new issue