SlideShare a Scribd company logo
1
Tips	
  and	
  tricks	
  for	
  high	
  performance	
  websites	
  
Harald	
  Zeitlhofer	
  
Boost	
  your	
  website	
  by	
  running	
  
PHP	
  on	
  Nginx	
  
@HZeitlhofer	
  
harald.zeitlhofer@dynatrace.com	
  
	
  
2
• Technology	
  Strategist	
  at	
  Dynatrace	
  
• Database	
  and	
  Web	
  Development	
  
• PHP	
  for	
  more	
  than	
  15	
  years	
  
• Love	
  to	
  discover	
  new	
  things	
  
Harald	
  Zeitlhofer	
  
3
Tips and tricks for high performance websites
4
Web	
  ApplicaIons	
  
5
6
Modern	
  Web	
  Pages:	
  lots	
  of	
  staIc	
  content	
  
434	
  Resources	
  in	
  total	
  on	
  that	
  page:	
  
230	
  JPEGs,	
  75	
  PNGs,	
  50	
  GIFs,	
  …	
  
more	
  than	
  20MB	
  page	
  size	
  
7
Fifa.com	
  during	
  Worldcup	
  2014	
  
hXp://blog.dynatrace.com/2014/05/21/is-­‐the-­‐fifa-­‐world-­‐cup-­‐website-­‐ready-­‐for-­‐the-­‐tournament/	
  
largest	
  item	
  on	
  page:	
  
favicon.ico	
  with	
  370	
  KB!!!	
  
but	
  also	
  some	
  heavyweight	
  
CSS	
  and	
  JS	
  files	
  with	
  up	
  to	
  288	
  KB!!!	
  
8
cached	
  content	
  
can	
  sIll	
  create	
  roundtrips	
  	
  
to	
  the	
  network!	
  
9
Web	
  Request	
  handling	
  
10
Web	
  Request	
  handling	
  
11
PHP	
  
run	
  modes	
  
Apache	
  Module	
  
–  tradiIonal	
  approach	
  
–  used	
  for	
  most	
  PHP	
  environments	
  
PHP-­‐FPM	
  
–  fast	
  process	
  manager	
  
–  run	
  mulIple	
  PHP	
  worker	
  processes	
  to	
  
serve	
  FastCGI	
  requests	
  
HHVM	
  
–  Virtual	
  machine	
  for	
  HipHop	
  
–  fast	
  PHP	
  engine	
  
–  can	
  serve	
  FastCGI	
  requests	
  	
  
	
  
12
PHP-­‐FPM	
  
FastCGI	
  Process	
  Manager	
  
	
  
	
  
Available	
  since	
  5.3.3	
  
Stable	
  since	
  5.4.1	
  
	
  
	
  
13
• InstallaIon	
  
	
  
• Pool	
  configuraIon	
  
/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
14
HHVM	
  
HipHop	
  Virtual	
  Machine	
  
Facebook's	
  PHP	
  engine	
  
JIT	
  compiler	
  
supports	
  PHP	
  and	
  Hack	
  
	
  
15
• InstallaIon	
  
	
  
• start	
  server	
  
cd /your/root/folder
hhvm --mode server -vServer.Type=fastcgi -vServer.Port=9000
echo deb http://dl.hhvm.com/ubuntu trusty main | sudo tee /etc/apt/sources.list.d/hhvm.list
sudo apt-get update
sudo apt-get install hhvm
hhvm --mode server -vServer.Type=fastcgi –vServer.FileSocket=/var/run/hhvm.sock
16
Nginx	
  
Lightweight	
  HTTP	
  server	
  
Event	
  based	
  request	
  handling	
  
Open	
  Source	
  project	
  (BSD)	
  	
  
Development	
  started	
  in	
  2002	
  by	
  Igor	
  Sysoev	
  to	
  solve	
  the	
  c10k	
  problem	
  
Commercial	
  version	
  NGINX	
  Plus	
  
17
Leading	
  among	
  	
  
top	
  100.000	
  websites	
  
18
/etc/nginx/nginx.conf	
  
	
  
	
  
# max_clients = worker_processes * worker_connections
worker_processes 8; # number of CPUs
pcre_jit on; # enable JIT for regex
events {
worker_connections 1024;
multi_accept on;
}
19
•  StaIc	
  content	
  served	
  by	
  Nginx	
  
•  Dynamic	
  requests	
  sent	
  to	
  PHP	
  
IntegraIon	
  
server {
listen 80;
server_name www.yourdomain.com;
root /var/www/test;
index index.php index.html index.htm;
location ~* .(html|js|css|gif|jpg|jpe|jpeg|png|bmp|tif|pdf|ico)$ {
try_files $uri =404;
}
location / {
try_files $uri $uri/ =404;
}
location ~* .php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1;
include fastcgi_params;
}
}
20
CommunicaIon	
  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;
21
TransacIon	
  flow	
  
22
hXp://www.mysite.com/news/browse/2014	
  
è	
  to	
  be	
  processed	
  by	
  index.php	
  
URL	
  rewrite	
  
...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php
...
23
hXp://www.mysite.com/news/browse/2014	
  
è	
  to	
  be	
  processed	
  by	
  index.php	
  
URL	
  rewrite	
  
upstream php {
server unix:/var/run/php5-fpm.sock;
}
server {
listen 80;
root /var/www;
index index.php index.html index.htm;
server_name www.mysite.com;
location / {
try_files $uri $uri/ @missing;
}
location @missing {
rewrite (.*) /index.php;
}
location ~ .php$ {
fastcgi_index index.php;
include fastcgi_params;
fastcgi_pass php;
}
}
24
using	
  Nginx/PHP-­‐FPM	
  there’s	
  a	
  beXer	
  way:	
  
URL	
  rewrite	
  
upstream php {
server unix:/var/run/php5-fpm.sock;
}
server {
listen 80;
root /var/www;
index index.php index.html index.htm;
server_name www.mysite.com;
location /images {
try_files $uri =404;
}
location /scripts {
try_files $uri =404;
}
location / {
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/index.php;
fastcgi_pass php;
}
}
<?php
$params = explode('/', $_SERVER["DOCUMENT_URI"]);
...
25
• Easy	
  way	
  to	
  deploy	
  your	
  applicaIon	
  
• All	
  source	
  files	
  packed	
  into	
  one	
  *.phar	
  file	
  
PHAR	
  –	
  PHP	
  Archives	
  
location ~* .(php|phar)$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/myapp.phar;
}
26
PHAR	
  example	
  
root@hzvm01:/var/www/app/src# ls -lrtR
.:
total 8
drwxrwxr-x 2 root root 4096 Oct 10 16:27 lib
-rw-r--r-- 1 root root 27 Oct 10 16:27 index.php
./lib:
total 4
-rw-r--r-- 1 root root 87 Oct 10 16:27 App.php
root@hzvm01:/var/www/app/src# cat index.php
<?php
$app = new App();
?>
root@hzvm01:/var/www/app/src# cat lib/App.php
<?php
class App {
function __construct() {
echo "Starting Applicationn";
}
}
27
PHAR	
  example	
  
location /myapp {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/myapp.phar;
}
root@hzvm01:/var/www/app# phar pack -f myapp.phar src
lib/App.php
index.php
root@hzvm01:/var/www/app# l myapp.phar
-rw-r--r-- 1 root root 6923 Oct 10 19:51 myapp.phar
28
PHAR	
  execuIon	
  
29
Nginx	
  and	
  Caching	
  
30
• Part	
  of	
  Nginx'	
  FastCGI	
  module	
  
Nginx	
  FastCGI	
  cache	
  
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=APPKEY:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
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_cache APPKEY;
fastcgi_cache_valid 200 60m;
}
31
FastCGI	
  cache	
  in	
  acIon	
  
32
FastCGI	
  cache	
  in	
  acIon	
  
<?php
echo time()."n";
?>
33
FastCGI	
  cache	
  in	
  acIon	
  
<?php
echo time()."n";
?>
34
Full	
  page	
  cache	
  with	
  Memcached	
  
<?php
...
function __construct () {
$this->c = new Memcached();
$this->c->addServer('localhost',11211);
}
function setCache ($key, $content) {
$this->c->set($key, $content);
}
...
// get HTML content
$html = $this->renderPage();
$this->setCache($_SERVER['REQUEST_URI'], $html);
...
?>
35
Data	
  cache	
  with	
  Memcached	
  
<?php
...
function __construct () {
$this->c = new Memcached();
$this->c->addServer('localhost',11211);
}
function setCache ($key, $content) {
$this->c->set($key, $content);
}
...
// get data structure
$newslist = $this->getNewsList();
$this->setCache('/data/news/getlist', json_encode($newslist));
...
?>
36
• ngx_hXp_memcached_module	
  
Full	
  page	
  /	
  data	
  cache	
  with	
  Nginx	
  and	
  Memcached	
  
upstream php {
server unix:/var/run/php5-fpm.sock;
}
server {
location / {
set $memcached_key "$uri";
memcached_pass localhost:11211;
error_page 404 502 504 = @notincache;
}
location @notincache {
fastcgi_pass php;
}
}
37
PHP,	
  5k	
  requests,	
  concurrency	
  100	
  
0	
  
1	
  
2	
  
3	
  
4	
  
5	
  
6	
  
7	
  
8	
  
Apache+PHP	
   Nginx+PHP	
   Nginx+Memcached	
  
<?php
echo "Hello World";
?>
7,28	
   4,6	
   3,05	
  
total	
  Ime	
  in	
  sec	
  
38
• set	
  HTTP	
  response	
  expires	
  header	
  
Caching	
  StaIc	
  Content	
  
location ~ .(html|js|css|gif|jpg|jpe|jpeg|png|bmp|tif|pdf|ico)$ {
expires 365d;
access_log off;
error_log off;
log_not_found off;
add_header Cache-Control "public";
try_files $uri =404;
}
39
• keep	
  handlers	
  for	
  requested	
  staIc	
  files	
  open	
  
Filehandle	
  Caching	
  
open_file_cache max=1000 inactive=5m;
open_file_cache_valid 60s;
open_file_cache_min_uses 5;
open_file_cache_errors off;
40
Load	
  balancing	
  
upstream php {
ip_hash;
server unix:/var/run/php5-fpm.sock weight=5;
server 192.168.56.12:9000 weight=2;
server 192.168.56.13:9000;
server 192.168.56.14:9000 backup;
}
server {
listen 80;
root /home/www/test;
server_name test.hzvm01;
location / {
try_files $uri =405;
}
location ~ .php$ {
fastcgi_pass php;
fastcgi_index index.php;
include fastcgi_params;
}
}
41
• Methods	
  
•  round-­‐robin	
  
•  least-­‐connected	
  (least_conn)	
  
•  session	
  persistence	
  (ip_hash)	
  
• Weighted	
  load	
  balancing	
  
• Health	
  checks	
  
•  max_fails	
  
•  fail_Imeout	
  
Load	
  balancing	
  
42
more	
  performance	
  tuning	
  
location ~ .(html|js|css|gif|jpg|jpe|jpeg|png|bmp|tif|pdf|ico)$ {
expires 365d;
access_log off;
error_log off;
log_not_found off;
add_header Cache-Control "public";
try_files $uri =404;
}
43
news	
  from	
  the	
  NGINX	
  front	
  
44
• remove	
  the	
  spdy	
  parameter	
  from	
  all	
  listen	
  direcIves	
  !!!	
  
HTTP/2	
  
server {
listen 443 ssl http2 default_server;
ssl_certificate server.crt;
ssl_certificate_key server.key;
...
}
45
• first	
  preview	
  announced	
  at	
  nginx.conf	
  2015	
  
• custom	
  JS	
  engine	
  	
  
• one	
  VM	
  for	
  each	
  request	
  
• JS	
  snippets	
  embedded	
  in	
  NGINX	
  configuraIon	
  
• evaluated	
  at	
  runIme	
  
nginScript	
  
46
nginScript	
  –	
  js_set	
  
http {
js_set $hello_world "
var str = 'Hello World!';
// JavaScript
str;
";
server {
...
location /{
return 200 $hello_world;
}
}
}
47
nginScript	
  –	
  js_run	
  
location / {
js_run "
var res;
res = $r.response;
res.status = 200;
res.send('Hello World!');
res.finish();
";
}
48
nginScript	
  –	
  request	
  parameters	
  
http {
js_set $summary "
var a, s, h;
s += 'Method: ' + $r.method + 'n' + 'HTTP version: ' + $r.httpVersion + 'n';
s += 'Host: ' + $r.headers.host + 'n' + 'Remote Address: ' + $r.remoteAddress + 'n';
s += 'URI: ' + $r.uri + 'n';
s += 'Headers:n';
for (h in $r.headers) {
s += ' header "' + h + '" is "' + $r.headers[h] + '"n';
}
s += 'Args:n';
for (a in $r.args) {
s += ' arg "' + a + '" is "' + $r.args[a] + '"n';
}
s;
";
server {
listen 8000;
location /summary {
return 200 $summary;
}
}
49
• easy	
  distribuIon	
  of	
  3rd	
  party	
  modules	
  to	
  end	
  users	
  
• migraIon	
  of	
  exisIng	
  modules	
  (rebuild)	
  
• only	
  cerIfied	
  modules	
  loadable	
  in	
  NGINX	
  Plus	
  
Dynamic	
  modules	
  
50
• Nginx	
  running	
  with	
  default	
  sepngs	
  
• Apache	
  
•  AllowOverride	
  None	
  
•  MulI-­‐process	
  (prefork)	
  mode	
  	
  
to	
  allow	
  usage	
  of	
  mod_php	
  
Benchmarking	
  Nginx	
  vs	
  Apache	
  
51
StaIc	
  HTML,	
  10k	
  requests	
  
0	
  
1	
  
2	
  
3	
  
4	
  
5	
  
6	
  
7	
  
8	
  
9	
  
100	
   500	
   1000	
   2000	
  
Apache/2.4.9	
  
nginx/1.1.19	
  
concurrency	
  
Total	
  response	
  Ime	
  [sec]	
  
52
Performance	
  Monitoring	
  
53
• Load	
  Generator	
  	
  
(Apache	
  Benchmark,	
  Selenium,	
  JMeter)	
  
• Firebug,	
  Google	
  Developer	
  Tools	
  
Dynatrace	
  Ajax	
  EdiIon	
  
• Dynatrace	
  Free	
  Trial	
  
•  Free	
  trial	
  license	
  for	
  30	
  days	
  
•  Free	
  personal	
  license	
  for	
  developers	
  
My	
  favorite	
  performance	
  tools	
  
hXp://bit.ly/dXrial	
  
54
www.dynatrace.com	
  
Thank you !!!
Harald	
  Zeitlhofer	
  
Performance	
  Advocate	
  
#HZeitlhofer	
  
harald.zeitlhofer@dynatrace.com	
  
blog.dynatrace.com	
  
Dynatrace	
  Free	
  Trial	
  
Free	
  Personal	
  License	
  
hDp://bit.ly/dDrial	
  

More Related Content

What's hot

04 web optimization
04 web optimization04 web optimization
04 web optimization
Nguyen Duc Phu
 
Building Scalable Websites with Perl
Building Scalable Websites with PerlBuilding Scalable Websites with Perl
Building Scalable Websites with Perl
Perrin Harkins
 
Caching
CachingCaching
Caching
Nascenia IT
 
Background Tasks in Node - Evan Tahler, TaskRabbit
Background Tasks in Node - Evan Tahler, TaskRabbitBackground Tasks in Node - Evan Tahler, TaskRabbit
Background Tasks in Node - Evan Tahler, TaskRabbit
Redis Labs
 
Introduction to performance tuning perl web applications
Introduction to performance tuning perl web applicationsIntroduction to performance tuning perl web applications
Introduction to performance tuning perl web applications
Perrin Harkins
 
(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014
(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014
(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014
Amazon Web Services
 
Query optimization: from 0 to 10 (and up to 5.7)
Query optimization: from 0 to 10 (and up to 5.7)Query optimization: from 0 to 10 (and up to 5.7)
Query optimization: from 0 to 10 (and up to 5.7)
Jaime Crespo
 
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, HerokuPostgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
Redis Labs
 
Reverse proxy & web cache with NGINX, HAProxy and Varnish
Reverse proxy & web cache with NGINX, HAProxy and VarnishReverse proxy & web cache with NGINX, HAProxy and Varnish
Reverse proxy & web cache with NGINX, HAProxy and Varnish
El Mahdi Benzekri
 
PyGotham 2014 Introduction to Profiling
PyGotham 2014 Introduction to ProfilingPyGotham 2014 Introduction to Profiling
PyGotham 2014 Introduction to Profiling
Perrin Harkins
 
Caching basics in PHP
Caching basics in PHPCaching basics in PHP
Caching basics in PHP
Anis Ahmad
 
Open Source Backup Conference 2014: Migration from bacula to bareos, by Danie...
Open Source Backup Conference 2014: Migration from bacula to bareos, by Danie...Open Source Backup Conference 2014: Migration from bacula to bareos, by Danie...
Open Source Backup Conference 2014: Migration from bacula to bareos, by Danie...
NETWAYS
 
WordPress Need For Speed
WordPress Need For SpeedWordPress Need For Speed
WordPress Need For Speed
pdeschen
 
Nginx: Accelerate Rails, HTTP Tricks
Nginx: Accelerate Rails, HTTP TricksNginx: Accelerate Rails, HTTP Tricks
Nginx: Accelerate Rails, HTTP Tricks
Adam Wiggins
 
PHP projects beyond the LAMP stack
PHP projects beyond the LAMP stackPHP projects beyond the LAMP stack
PHP projects beyond the LAMP stack
Codemotion
 
Using memcache to improve php performance
Using memcache to improve php performanceUsing memcache to improve php performance
Using memcache to improve php performance
Sudar Muthu
 
High performance WordPress
High performance WordPressHigh performance WordPress
High performance WordPress
Mikel King
 
Cassandra as Memcache
Cassandra as MemcacheCassandra as Memcache
Cassandra as Memcache
Edward Capriolo
 
NginX - good practices, tips and advanced techniques
NginX - good practices, tips and advanced techniquesNginX - good practices, tips and advanced techniques
NginX - good practices, tips and advanced techniques
Claudio Borges
 

What's hot (19)

04 web optimization
04 web optimization04 web optimization
04 web optimization
 
Building Scalable Websites with Perl
Building Scalable Websites with PerlBuilding Scalable Websites with Perl
Building Scalable Websites with Perl
 
Caching
CachingCaching
Caching
 
Background Tasks in Node - Evan Tahler, TaskRabbit
Background Tasks in Node - Evan Tahler, TaskRabbitBackground Tasks in Node - Evan Tahler, TaskRabbit
Background Tasks in Node - Evan Tahler, TaskRabbit
 
Introduction to performance tuning perl web applications
Introduction to performance tuning perl web applicationsIntroduction to performance tuning perl web applications
Introduction to performance tuning perl web applications
 
(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014
(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014
(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014
 
Query optimization: from 0 to 10 (and up to 5.7)
Query optimization: from 0 to 10 (and up to 5.7)Query optimization: from 0 to 10 (and up to 5.7)
Query optimization: from 0 to 10 (and up to 5.7)
 
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, HerokuPostgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
Postgres & Redis Sitting in a Tree- Rimas Silkaitis, Heroku
 
Reverse proxy & web cache with NGINX, HAProxy and Varnish
Reverse proxy & web cache with NGINX, HAProxy and VarnishReverse proxy & web cache with NGINX, HAProxy and Varnish
Reverse proxy & web cache with NGINX, HAProxy and Varnish
 
PyGotham 2014 Introduction to Profiling
PyGotham 2014 Introduction to ProfilingPyGotham 2014 Introduction to Profiling
PyGotham 2014 Introduction to Profiling
 
Caching basics in PHP
Caching basics in PHPCaching basics in PHP
Caching basics in PHP
 
Open Source Backup Conference 2014: Migration from bacula to bareos, by Danie...
Open Source Backup Conference 2014: Migration from bacula to bareos, by Danie...Open Source Backup Conference 2014: Migration from bacula to bareos, by Danie...
Open Source Backup Conference 2014: Migration from bacula to bareos, by Danie...
 
WordPress Need For Speed
WordPress Need For SpeedWordPress Need For Speed
WordPress Need For Speed
 
Nginx: Accelerate Rails, HTTP Tricks
Nginx: Accelerate Rails, HTTP TricksNginx: Accelerate Rails, HTTP Tricks
Nginx: Accelerate Rails, HTTP Tricks
 
PHP projects beyond the LAMP stack
PHP projects beyond the LAMP stackPHP projects beyond the LAMP stack
PHP projects beyond the LAMP stack
 
Using memcache to improve php performance
Using memcache to improve php performanceUsing memcache to improve php performance
Using memcache to improve php performance
 
High performance WordPress
High performance WordPressHigh performance WordPress
High performance WordPress
 
Cassandra as Memcache
Cassandra as MemcacheCassandra as Memcache
Cassandra as Memcache
 
NginX - good practices, tips and advanced techniques
NginX - good practices, tips and advanced techniquesNginX - good practices, tips and advanced techniques
NginX - good practices, tips and advanced techniques
 

Similar to Running PHP on Nginx

Running PHP on nginx
Running PHP on nginxRunning PHP on nginx
Running PHP on nginx
Harald Zeitlhofer
 
Nginx + PHP
Nginx + PHPNginx + PHP
Nginx + PHP
Wataru OKAMOTO
 
10 Million hits a day with WordPress using a $15 VPS
10 Million hits a day  with WordPress using a $15 VPS10 Million hits a day  with WordPress using a $15 VPS
10 Million hits a day with WordPress using a $15 VPS
Paolo Tonin
 
InspiringCon14: ElePHPants on speed: Running TYPO3 Flow on HipHop VM
InspiringCon14: ElePHPants on speed: Running TYPO3 Flow on HipHop VMInspiringCon14: ElePHPants on speed: Running TYPO3 Flow on HipHop VM
InspiringCon14: ElePHPants on speed: Running TYPO3 Flow on HipHop VM
mhelmich
 
Php through the eyes of a hoster confoo
Php through the eyes of a hoster confooPhp through the eyes of a hoster confoo
Php through the eyes of a hoster confoo
Combell NV
 
php & performance
 php & performance php & performance
php & performance
simon8410
 
Less and faster – Cache tips for WordPress developers
Less and faster – Cache tips for WordPress developersLess and faster – Cache tips for WordPress developers
Less and faster – Cache tips for WordPress developers
Seravo
 
PHP & Performance
PHP & PerformancePHP & Performance
PHP & Performance
毅 吕
 
2012 coscup - Build your PHP application on Heroku
2012 coscup - Build your PHP application on Heroku2012 coscup - Build your PHP application on Heroku
2012 coscup - Build your PHP application on Heroku
ronnywang_tw
 
Php through the eyes of a hoster
Php through the eyes of a hosterPhp through the eyes of a hoster
Php through the eyes of a hoster
Combell NV
 
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHPNortheast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Jonathan Klein
 
Php through the eyes of a hoster phpbnl11
Php through the eyes of a hoster phpbnl11Php through the eyes of a hoster phpbnl11
Php through the eyes of a hoster phpbnl11
Combell NV
 
Apache and PHP: Why httpd.conf is your new BFF!
Apache and PHP: Why httpd.conf is your new BFF!Apache and PHP: Why httpd.conf is your new BFF!
Apache and PHP: Why httpd.conf is your new BFF!
Jeff Jones
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
Wim Godden
 
Use Xdebug to profile PHP
Use Xdebug to profile PHPUse Xdebug to profile PHP
Use Xdebug to profile PHP
Seravo
 
Automatic testing and quality assurance for WordPress plugins and themes
Automatic testing and quality assurance for WordPress plugins and themesAutomatic testing and quality assurance for WordPress plugins and themes
Automatic testing and quality assurance for WordPress plugins and themes
Otto Kekäläinen
 
WordPress Security - ওয়ার্ডপ্রেসের সিকিউরিটি
WordPress Security - ওয়ার্ডপ্রেসের সিকিউরিটিWordPress Security - ওয়ার্ডপ্রেসের সিকিউরিটি
WordPress Security - ওয়ার্ডপ্রেসের সিকিউরিটি
Faysal Shahi
 
Web Front End Performance
Web Front End PerformanceWeb Front End Performance
Web Front End Performance
Chris Love
 
Site Performance - From Pinto to Ferrari
Site Performance - From Pinto to FerrariSite Performance - From Pinto to Ferrari
Site Performance - From Pinto to Ferrari
Joseph Scott
 
Hacking with hhvm
Hacking with hhvmHacking with hhvm
Hacking with hhvm
Elizabeth Smith
 

Similar to Running PHP on Nginx (20)

Running PHP on nginx
Running PHP on nginxRunning PHP on nginx
Running PHP on nginx
 
Nginx + PHP
Nginx + PHPNginx + PHP
Nginx + PHP
 
10 Million hits a day with WordPress using a $15 VPS
10 Million hits a day  with WordPress using a $15 VPS10 Million hits a day  with WordPress using a $15 VPS
10 Million hits a day with WordPress using a $15 VPS
 
InspiringCon14: ElePHPants on speed: Running TYPO3 Flow on HipHop VM
InspiringCon14: ElePHPants on speed: Running TYPO3 Flow on HipHop VMInspiringCon14: ElePHPants on speed: Running TYPO3 Flow on HipHop VM
InspiringCon14: ElePHPants on speed: Running TYPO3 Flow on HipHop VM
 
Php through the eyes of a hoster confoo
Php through the eyes of a hoster confooPhp through the eyes of a hoster confoo
Php through the eyes of a hoster confoo
 
php & performance
 php & performance php & performance
php & performance
 
Less and faster – Cache tips for WordPress developers
Less and faster – Cache tips for WordPress developersLess and faster – Cache tips for WordPress developers
Less and faster – Cache tips for WordPress developers
 
PHP & Performance
PHP & PerformancePHP & Performance
PHP & Performance
 
2012 coscup - Build your PHP application on Heroku
2012 coscup - Build your PHP application on Heroku2012 coscup - Build your PHP application on Heroku
2012 coscup - Build your PHP application on Heroku
 
Php through the eyes of a hoster
Php through the eyes of a hosterPhp through the eyes of a hoster
Php through the eyes of a hoster
 
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHPNortheast PHP - High Performance PHP
Northeast PHP - High Performance PHP
 
Php through the eyes of a hoster phpbnl11
Php through the eyes of a hoster phpbnl11Php through the eyes of a hoster phpbnl11
Php through the eyes of a hoster phpbnl11
 
Apache and PHP: Why httpd.conf is your new BFF!
Apache and PHP: Why httpd.conf is your new BFF!Apache and PHP: Why httpd.conf is your new BFF!
Apache and PHP: Why httpd.conf is your new BFF!
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
 
Use Xdebug to profile PHP
Use Xdebug to profile PHPUse Xdebug to profile PHP
Use Xdebug to profile PHP
 
Automatic testing and quality assurance for WordPress plugins and themes
Automatic testing and quality assurance for WordPress plugins and themesAutomatic testing and quality assurance for WordPress plugins and themes
Automatic testing and quality assurance for WordPress plugins and themes
 
WordPress Security - ওয়ার্ডপ্রেসের সিকিউরিটি
WordPress Security - ওয়ার্ডপ্রেসের সিকিউরিটিWordPress Security - ওয়ার্ডপ্রেসের সিকিউরিটি
WordPress Security - ওয়ার্ডপ্রেসের সিকিউরিটি
 
Web Front End Performance
Web Front End PerformanceWeb Front End Performance
Web Front End Performance
 
Site Performance - From Pinto to Ferrari
Site Performance - From Pinto to FerrariSite Performance - From Pinto to Ferrari
Site Performance - From Pinto to Ferrari
 
Hacking with hhvm
Hacking with hhvmHacking with hhvm
Hacking with hhvm
 

More from Harald Zeitlhofer

Running PHP on Nginx / PHP wgtn
Running PHP on Nginx / PHP wgtnRunning PHP on Nginx / PHP wgtn
Running PHP on Nginx / PHP wgtn
Harald Zeitlhofer
 
PHP App Performance / Sydney PHP
PHP App Performance / Sydney PHPPHP App Performance / Sydney PHP
PHP App Performance / Sydney PHP
Harald Zeitlhofer
 
PHP application performance
PHP application performancePHP application performance
PHP application performance
Harald Zeitlhofer
 
PHP Application Performance
PHP Application PerformancePHP Application Performance
PHP Application Performance
Harald Zeitlhofer
 
Nginx performance monitoring with Dynatrace
Nginx performance monitoring with DynatraceNginx performance monitoring with Dynatrace
Nginx performance monitoring with Dynatrace
Harald Zeitlhofer
 
Nginx, PHP, Apache and Spelix
Nginx, PHP, Apache and SpelixNginx, PHP, Apache and Spelix
Nginx, PHP, Apache and Spelix
Harald Zeitlhofer
 
Nginx, PHP and Node.js
Nginx, PHP and Node.jsNginx, PHP and Node.js
Nginx, PHP and Node.js
Harald Zeitlhofer
 
Performance optimisation - scaling a hobby project to serious business
Performance optimisation - scaling a hobby project to serious businessPerformance optimisation - scaling a hobby project to serious business
Performance optimisation - scaling a hobby project to serious business
Harald Zeitlhofer
 

More from Harald Zeitlhofer (8)

Running PHP on Nginx / PHP wgtn
Running PHP on Nginx / PHP wgtnRunning PHP on Nginx / PHP wgtn
Running PHP on Nginx / PHP wgtn
 
PHP App Performance / Sydney PHP
PHP App Performance / Sydney PHPPHP App Performance / Sydney PHP
PHP App Performance / Sydney PHP
 
PHP application performance
PHP application performancePHP application performance
PHP application performance
 
PHP Application Performance
PHP Application PerformancePHP Application Performance
PHP Application Performance
 
Nginx performance monitoring with Dynatrace
Nginx performance monitoring with DynatraceNginx performance monitoring with Dynatrace
Nginx performance monitoring with Dynatrace
 
Nginx, PHP, Apache and Spelix
Nginx, PHP, Apache and SpelixNginx, PHP, Apache and Spelix
Nginx, PHP, Apache and Spelix
 
Nginx, PHP and Node.js
Nginx, PHP and Node.jsNginx, PHP and Node.js
Nginx, PHP and Node.js
 
Performance optimisation - scaling a hobby project to serious business
Performance optimisation - scaling a hobby project to serious businessPerformance optimisation - scaling a hobby project to serious business
Performance optimisation - scaling a hobby project to serious business
 

Recently uploaded

Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
Wouter Lemaire
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 

Recently uploaded (20)

Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 

Running PHP on Nginx

  • 1. 1 Tips  and  tricks  for  high  performance  websites   Harald  Zeitlhofer   Boost  your  website  by  running   PHP  on  Nginx   @HZeitlhofer   harald.zeitlhofer@dynatrace.com    
  • 2. 2 • Technology  Strategist  at  Dynatrace   • Database  and  Web  Development   • PHP  for  more  than  15  years   • Love  to  discover  new  things   Harald  Zeitlhofer  
  • 3. 3 Tips and tricks for high performance websites
  • 5. 5
  • 6. 6 Modern  Web  Pages:  lots  of  staIc  content   434  Resources  in  total  on  that  page:   230  JPEGs,  75  PNGs,  50  GIFs,  …   more  than  20MB  page  size  
  • 7. 7 Fifa.com  during  Worldcup  2014   hXp://blog.dynatrace.com/2014/05/21/is-­‐the-­‐fifa-­‐world-­‐cup-­‐website-­‐ready-­‐for-­‐the-­‐tournament/   largest  item  on  page:   favicon.ico  with  370  KB!!!   but  also  some  heavyweight   CSS  and  JS  files  with  up  to  288  KB!!!  
  • 8. 8 cached  content   can  sIll  create  roundtrips     to  the  network!  
  • 11. 11 PHP   run  modes   Apache  Module   –  tradiIonal  approach   –  used  for  most  PHP  environments   PHP-­‐FPM   –  fast  process  manager   –  run  mulIple  PHP  worker  processes  to   serve  FastCGI  requests   HHVM   –  Virtual  machine  for  HipHop   –  fast  PHP  engine   –  can  serve  FastCGI  requests      
  • 12. 12 PHP-­‐FPM   FastCGI  Process  Manager       Available  since  5.3.3   Stable  since  5.4.1      
  • 13. 13 • InstallaIon     • Pool  configuraIon   /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
  • 14. 14 HHVM   HipHop  Virtual  Machine   Facebook's  PHP  engine   JIT  compiler   supports  PHP  and  Hack    
  • 15. 15 • InstallaIon     • start  server   cd /your/root/folder hhvm --mode server -vServer.Type=fastcgi -vServer.Port=9000 echo deb http://dl.hhvm.com/ubuntu trusty main | sudo tee /etc/apt/sources.list.d/hhvm.list sudo apt-get update sudo apt-get install hhvm hhvm --mode server -vServer.Type=fastcgi –vServer.FileSocket=/var/run/hhvm.sock
  • 16. 16 Nginx   Lightweight  HTTP  server   Event  based  request  handling   Open  Source  project  (BSD)     Development  started  in  2002  by  Igor  Sysoev  to  solve  the  c10k  problem   Commercial  version  NGINX  Plus  
  • 17. 17 Leading  among     top  100.000  websites  
  • 18. 18 /etc/nginx/nginx.conf       # max_clients = worker_processes * worker_connections worker_processes 8; # number of CPUs pcre_jit on; # enable JIT for regex events { worker_connections 1024; multi_accept on; }
  • 19. 19 •  StaIc  content  served  by  Nginx   •  Dynamic  requests  sent  to  PHP   IntegraIon   server { listen 80; server_name www.yourdomain.com; root /var/www/test; index index.php index.html index.htm; location ~* .(html|js|css|gif|jpg|jpe|jpeg|png|bmp|tif|pdf|ico)$ { try_files $uri =404; } location / { try_files $uri $uri/ =404; } location ~* .php$ { fastcgi_index index.php; fastcgi_pass 127.0.0.1; include fastcgi_params; } }
  • 20. 20 CommunicaIon  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;
  • 22. 22 hXp://www.mysite.com/news/browse/2014   è  to  be  processed  by  index.php   URL  rewrite   ... RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.+)$ index.php ...
  • 23. 23 hXp://www.mysite.com/news/browse/2014   è  to  be  processed  by  index.php   URL  rewrite   upstream php { server unix:/var/run/php5-fpm.sock; } server { listen 80; root /var/www; index index.php index.html index.htm; server_name www.mysite.com; location / { try_files $uri $uri/ @missing; } location @missing { rewrite (.*) /index.php; } location ~ .php$ { fastcgi_index index.php; include fastcgi_params; fastcgi_pass php; } }
  • 24. 24 using  Nginx/PHP-­‐FPM  there’s  a  beXer  way:   URL  rewrite   upstream php { server unix:/var/run/php5-fpm.sock; } server { listen 80; root /var/www; index index.php index.html index.htm; server_name www.mysite.com; location /images { try_files $uri =404; } location /scripts { try_files $uri =404; } location / { fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME /var/www/index.php; fastcgi_pass php; } } <?php $params = explode('/', $_SERVER["DOCUMENT_URI"]); ...
  • 25. 25 • Easy  way  to  deploy  your  applicaIon   • All  source  files  packed  into  one  *.phar  file   PHAR  –  PHP  Archives   location ~* .(php|phar)$ { fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root/myapp.phar; }
  • 26. 26 PHAR  example   root@hzvm01:/var/www/app/src# ls -lrtR .: total 8 drwxrwxr-x 2 root root 4096 Oct 10 16:27 lib -rw-r--r-- 1 root root 27 Oct 10 16:27 index.php ./lib: total 4 -rw-r--r-- 1 root root 87 Oct 10 16:27 App.php root@hzvm01:/var/www/app/src# cat index.php <?php $app = new App(); ?> root@hzvm01:/var/www/app/src# cat lib/App.php <?php class App { function __construct() { echo "Starting Applicationn"; } }
  • 27. 27 PHAR  example   location /myapp { fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root/myapp.phar; } root@hzvm01:/var/www/app# phar pack -f myapp.phar src lib/App.php index.php root@hzvm01:/var/www/app# l myapp.phar -rw-r--r-- 1 root root 6923 Oct 10 19:51 myapp.phar
  • 30. 30 • Part  of  Nginx'  FastCGI  module   Nginx  FastCGI  cache   fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=APPKEY:100m inactive=60m; fastcgi_cache_key "$scheme$request_method$host$request_uri"; 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_cache APPKEY; fastcgi_cache_valid 200 60m; }
  • 32. 32 FastCGI  cache  in  acIon   <?php echo time()."n"; ?>
  • 33. 33 FastCGI  cache  in  acIon   <?php echo time()."n"; ?>
  • 34. 34 Full  page  cache  with  Memcached   <?php ... function __construct () { $this->c = new Memcached(); $this->c->addServer('localhost',11211); } function setCache ($key, $content) { $this->c->set($key, $content); } ... // get HTML content $html = $this->renderPage(); $this->setCache($_SERVER['REQUEST_URI'], $html); ... ?>
  • 35. 35 Data  cache  with  Memcached   <?php ... function __construct () { $this->c = new Memcached(); $this->c->addServer('localhost',11211); } function setCache ($key, $content) { $this->c->set($key, $content); } ... // get data structure $newslist = $this->getNewsList(); $this->setCache('/data/news/getlist', json_encode($newslist)); ... ?>
  • 36. 36 • ngx_hXp_memcached_module   Full  page  /  data  cache  with  Nginx  and  Memcached   upstream php { server unix:/var/run/php5-fpm.sock; } server { location / { set $memcached_key "$uri"; memcached_pass localhost:11211; error_page 404 502 504 = @notincache; } location @notincache { fastcgi_pass php; } }
  • 37. 37 PHP,  5k  requests,  concurrency  100   0   1   2   3   4   5   6   7   8   Apache+PHP   Nginx+PHP   Nginx+Memcached   <?php echo "Hello World"; ?> 7,28   4,6   3,05   total  Ime  in  sec  
  • 38. 38 • set  HTTP  response  expires  header   Caching  StaIc  Content   location ~ .(html|js|css|gif|jpg|jpe|jpeg|png|bmp|tif|pdf|ico)$ { expires 365d; access_log off; error_log off; log_not_found off; add_header Cache-Control "public"; try_files $uri =404; }
  • 39. 39 • keep  handlers  for  requested  staIc  files  open   Filehandle  Caching   open_file_cache max=1000 inactive=5m; open_file_cache_valid 60s; open_file_cache_min_uses 5; open_file_cache_errors off;
  • 40. 40 Load  balancing   upstream php { ip_hash; server unix:/var/run/php5-fpm.sock weight=5; server 192.168.56.12:9000 weight=2; server 192.168.56.13:9000; server 192.168.56.14:9000 backup; } server { listen 80; root /home/www/test; server_name test.hzvm01; location / { try_files $uri =405; } location ~ .php$ { fastcgi_pass php; fastcgi_index index.php; include fastcgi_params; } }
  • 41. 41 • Methods   •  round-­‐robin   •  least-­‐connected  (least_conn)   •  session  persistence  (ip_hash)   • Weighted  load  balancing   • Health  checks   •  max_fails   •  fail_Imeout   Load  balancing  
  • 42. 42 more  performance  tuning   location ~ .(html|js|css|gif|jpg|jpe|jpeg|png|bmp|tif|pdf|ico)$ { expires 365d; access_log off; error_log off; log_not_found off; add_header Cache-Control "public"; try_files $uri =404; }
  • 43. 43 news  from  the  NGINX  front  
  • 44. 44 • remove  the  spdy  parameter  from  all  listen  direcIves  !!!   HTTP/2   server { listen 443 ssl http2 default_server; ssl_certificate server.crt; ssl_certificate_key server.key; ... }
  • 45. 45 • first  preview  announced  at  nginx.conf  2015   • custom  JS  engine     • one  VM  for  each  request   • JS  snippets  embedded  in  NGINX  configuraIon   • evaluated  at  runIme   nginScript  
  • 46. 46 nginScript  –  js_set   http { js_set $hello_world " var str = 'Hello World!'; // JavaScript str; "; server { ... location /{ return 200 $hello_world; } } }
  • 47. 47 nginScript  –  js_run   location / { js_run " var res; res = $r.response; res.status = 200; res.send('Hello World!'); res.finish(); "; }
  • 48. 48 nginScript  –  request  parameters   http { js_set $summary " var a, s, h; s += 'Method: ' + $r.method + 'n' + 'HTTP version: ' + $r.httpVersion + 'n'; s += 'Host: ' + $r.headers.host + 'n' + 'Remote Address: ' + $r.remoteAddress + 'n'; s += 'URI: ' + $r.uri + 'n'; s += 'Headers:n'; for (h in $r.headers) { s += ' header "' + h + '" is "' + $r.headers[h] + '"n'; } s += 'Args:n'; for (a in $r.args) { s += ' arg "' + a + '" is "' + $r.args[a] + '"n'; } s; "; server { listen 8000; location /summary { return 200 $summary; } }
  • 49. 49 • easy  distribuIon  of  3rd  party  modules  to  end  users   • migraIon  of  exisIng  modules  (rebuild)   • only  cerIfied  modules  loadable  in  NGINX  Plus   Dynamic  modules  
  • 50. 50 • Nginx  running  with  default  sepngs   • Apache   •  AllowOverride  None   •  MulI-­‐process  (prefork)  mode     to  allow  usage  of  mod_php   Benchmarking  Nginx  vs  Apache  
  • 51. 51 StaIc  HTML,  10k  requests   0   1   2   3   4   5   6   7   8   9   100   500   1000   2000   Apache/2.4.9   nginx/1.1.19   concurrency   Total  response  Ime  [sec]  
  • 53. 53 • Load  Generator     (Apache  Benchmark,  Selenium,  JMeter)   • Firebug,  Google  Developer  Tools   Dynatrace  Ajax  EdiIon   • Dynatrace  Free  Trial   •  Free  trial  license  for  30  days   •  Free  personal  license  for  developers   My  favorite  performance  tools   hXp://bit.ly/dXrial  
  • 54. 54 www.dynatrace.com   Thank you !!! Harald  Zeitlhofer   Performance  Advocate   #HZeitlhofer   harald.zeitlhofer@dynatrace.com   blog.dynatrace.com   Dynatrace  Free  Trial   Free  Personal  License   hDp://bit.ly/dDrial