The document discusses configuring Nginx and PHP-FPM for high performance websites. Some key points:
- Nginx is a lightweight and fast HTTP server that is well-suited for high traffic loads. It can be used as a web server, reverse proxy, load balancer, and more.
- PHP-FPM (PHP FastCGI Process Manager) runs PHP processes as a pool that is separate from the web server for better isolation and performance. Nginx communicates with PHP-FPM via FastCGI.
- Benchmark results show Nginx performing better than Apache, especially under high concurrency loads. Caching with Nginx and Memcached can further improve
Scale your database traffic with Read & Write split using MySQL Router
Running php on nginx
1. 1 #Dynatrace
Proper
configura-on
for
high
performance
websites
Harald
Zeitlhofer
February
2015
Boost
your
website
by
running
PHP
on
Nginx
@HZeitlhofer
harald.zeitlhofer@dynatrace.com
3. 3 #Dynatrace
• Lightweight
HTTP
server
• Fast
especially
at
high
load
• Open
Source
project
(BSD)
by
Igor
Sysoev
• Nginx,
Inc.
founded
in
2011
• Nginx+:
enhanced
func-onality
and
enterprise
support
• Load
balancer
• Media
server
9. 9 #Dynatrace
• Apache
Module
• used
for
most
PHP
environments
• CGI
• Command
Line
Interface
(CLI)
• FastCGI
(PHP-‐FPM)
• Available
since
5.3.3,
stable
since
5.4.1
• Run
mul-ple
PHP
worker
processes
to
serve
CGI
requests
• Mul-ple
connec-on
pools
(different
uid/gid
environments
possible)
• Isola-on
from
webserver
(fastcgi_finish_request)
PHP
run
modes
10. 10 #Dynatrace
• Installa-on
• Pool
configura-on
/etc/php5/fpm/pool.d/www.conf
PHP-‐FPM
[www]
user = www-data
group = www-data
listen = 127.0.0.1:9000 # for Unix socket: unix:/var/run/php5-fpm.sock;
root@hzvm01:/etc/nginx/sites-enabled# ps -ef | grep php
root 6435 1 0 14:39 ? 00:00:32 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)
spelix 6439 6435 0 14:39 ? 00:00:00 php-fpm: pool batch
spelix 6440 6435 0 14:39 ? 00:00:00 php-fpm: pool batch
www-data 10576 6435 1 18:45 ? 00:00:48 php-fpm: pool www
www-data 10920 6435 1 18:47 ? 00:00:47 php-fpm: pool www
www-data 10927 6435 1 18:47 ? 00:00:46 php-fpm: pool www
sudo apt-get install php5-fpm
11. 11 #Dynatrace
• Pool
configura-on
/etc/php5/fpm/pool.d/www.conf
PHP-‐FPM
[pool_name]
...
pm = [dynamic/static]
pm.max_children = 10
;only used for dynamic:
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_children
=
total
available
memory
/
memory
used
by
1
PHP
process
14. 14 #Dynatrace
• Communica-on
via
sockets
• TCP
vs
Unix
• Unix
slightly
faster
when
used
on
localhost
• Use
TCP
for
high
load
location ~* .php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
fastcgi_pass unix:/var/run/php5-fpm.sock;
35. 35 #Dynatrace
Server Software: Apache/2.4.7
Server Hostname: test.hzvm01
Server Port: 88
Document Path: /index.php
Document Length: 10 bytes
Concurrency Level: 100
Time taken for tests: 7.296 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 985000 bytes
HTML transferred: 50000 bytes
Requests per second: 685.31 [#/sec] (mean)
Time per request: 145.920 [ms] (mean)
Time per request: 1.459 [ms] (mean, across all concurrent requests)
Transfer rate: 131.84 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 6 6.5 4 56
Processing: 30 138 57.2 120 938
Waiting: 30 135 56.4 117 937
Total: 55 144 55.8 126 938
PHP,
5k
requests,
concurrency
100
36. 36 #Dynatrace
Server Software: Nginx + PHP
Server Hostname: test.hzvm01
Server Port: 80
Document Path: /index.php
Document Length: 10 bytes
Concurrency Level: 100
Time taken for tests: 4.514 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 625000 bytes
HTML transferred: 50000 bytes
Requests per second: 1107.62 [#/sec] (mean)
Time per request: 90.284 [ms] (mean)
Time per request: 0.903 [ms] (mean, across all concurrent requests)
Transfer rate: 135.21 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 3.2 1 38
Processing: 22 88 13.1 87 148
Waiting: 21 87 13.1 86 148
Total: 42 89 12.5 88 148
PHP,
5k
requests,
concurrency
100
37. 37 #Dynatrace
Server Software: Nginx + Memcached
Server Hostname: test.hzvm01
Server Port: 82
Document Path: /index.php
Document Length: 23 bytes
Concurrency Level: 100
Time taken for tests: 3.058 seconds
Complete requests: 5000
Failed requests: 0
Total transferred: 865000 bytes
HTML transferred: 115000 bytes
Requests per second: 1634.92 [#/sec] (mean)
Time per request: 61.165 [ms] (mean)
Time per request: 0.612 [ms] (mean, across all concurrent requests)
Transfer rate: 276.21 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 7 24 6.0 24 59
Processing: 8 36 7.8 36 75
Waiting: 6 28 7.4 29 69
Total: 32 60 9.3 60 101
PHP,
5k
requests,
concurrency
100