running php on nginx / php wgtn

47
Tips and tricks for high performance websites Harald Zeitlhofer April 2015 Boost your website by running PHP on Nginx @HZeitlhofer [email protected]

Upload: harald-zeitlhofer

Post on 30-Jul-2015

147 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Running PHP on Nginx / PHP wgtn

1

Tips%and%tricks%for%high%performance%websites%

Harald%Zeitlhofer%April%2015%

%

Boost%your%website%by%running%PHP%on%Nginx%

@HZeitlhofer%[email protected]%

%

Page 2: Running PHP on Nginx / PHP wgtn

2

• Technology%Strategist%at%Dynatrace%• Database%and%Web%Development%

• PHP%for%more%than%15%years%

• Love%to%discover%new%things%

Harald%Zeitlhofer%

Page 3: Running PHP on Nginx / PHP wgtn

3

I’m%from%Austria%

Page 4: Running PHP on Nginx / PHP wgtn

4

Also%from%Austria%

Page 5: Running PHP on Nginx / PHP wgtn

5

Page 6: Running PHP on Nginx / PHP wgtn

6

But%%

probably%

you%%

already%

know%%

that%...%

Page 7: Running PHP on Nginx / PHP wgtn

7

Web%ApplicaMons%

Page 8: Running PHP on Nginx / PHP wgtn

8

Page 9: Running PHP on Nginx / PHP wgtn

9

Modern%Web%Pages:%lots%of%staMc%content%

434#Resources#in%total%on%that%page:%230%JPEGs,%75%PNGs,%50%GIFs,%…%

Page 10: Running PHP on Nginx / PHP wgtn

10

cached%content%

can%sMll%create%roundtrips%%to%the%network!%

Page 11: Running PHP on Nginx / PHP wgtn

11

Web%Request%handling%

Page 12: Running PHP on Nginx / PHP wgtn

12

Web%Request%handling%

Page 13: Running PHP on Nginx / PHP wgtn

13

Tips and tricks for high performance websites

Page 14: Running PHP on Nginx / PHP wgtn

14

Web%Request%handling%

Page 15: Running PHP on Nginx / PHP wgtn

15

Leading%among%%top%10.000%websites%

Page 16: Running PHP on Nginx / PHP wgtn

16

PHP%run%modes%

Apache%Module%

–  tradiMonal%approach%–  used%for%most%PHP%environments%

PHP[FPM%

–  PHP%applicaMon%server%–  run%mulMple%PHP%worker%processes%to%

serve%FastCGI%requests%

HHVM%

–  Virtual%machine%for%HipHop%

–  fast%PHP%engine%–  can%serve%FastCGI%requests%%

%

Page 17: Running PHP on Nginx / PHP wgtn

17

PHP[FPM%FastCGI%Process%Manager%%%

Available%since%5.3.3%

Stable%since%5.4.1%

%%%

Page 18: Running PHP on Nginx / PHP wgtn

18

•  InstallaMon%%

• Pool%configuraMon%/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

Page 19: Running PHP on Nginx / PHP wgtn

19

HHVM%HipHop%Virtual%Machine%

Facebook's%PHP%engine%

JIT%compiler%

supports%PHP%and%Hack%

%

Page 20: Running PHP on Nginx / PHP wgtn

20

•  InstallaMon%

%

•  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

Page 21: Running PHP on Nginx / PHP wgtn

21

Nginx%

Lightweight%HTTP%server%

Event%based%request%handling%

Fast%especially%at%high%load%

Open%Source%project%(BSD)%%

Commercial%version%Nginx+%

Page 22: Running PHP on Nginx / PHP wgtn

22

/etc/nginx/nginx.conf%

%

%# max_clients = worker_processes * worker_connections worker_processes 8; # number of CPUs events { worker_connections 1024; multi_accept on; }

Page 23: Running PHP on Nginx / PHP wgtn

23

•  StaMc%content%to%be%served%by%Nginx%

•  Dynamic%requests%to%be%sent%to%PHP%

IntegraMon%

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 php; include fastcgi_params; }

}

Page 24: Running PHP on Nginx / PHP wgtn

24

CommunicaMon%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;

Page 25: Running PHP on Nginx / PHP wgtn

25

TransacMon%flow%

Page 26: Running PHP on Nginx / PHP wgtn

26

Nginx%and%Caching%

Page 27: Running PHP on Nginx / PHP wgtn

27

• 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; }

Page 28: Running PHP on Nginx / PHP wgtn

28

TesMng%FastCGI%cache%

Page 29: Running PHP on Nginx / PHP wgtn

29

TesMng%FastCGI%cache%

<?php echo time()."\n"; ?>

Page 30: Running PHP on Nginx / PHP wgtn

30

TesMng%FastCGI%cache%

<?php echo time()."\n"; ?>

Page 31: Running PHP on Nginx / PHP wgtn

31

Full%page%/%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 HTML content $this->setCache($_SERVER['REQUEST_URI'], $this->renderPage()); ... // get JSON structure $this->setCache('/data/news/getlist', $this->getNewsList()); ... ?>

Page 32: Running PHP on Nginx / PHP wgtn

32

• ngx_hjp_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; } }

Page 33: Running PHP on Nginx / PHP wgtn

33

PHP,%5k%requests,%concurrency%100%

0%

1%

2%

3%

4%

5%

6%

7%

8%

Apache+PHP% Nginx+PHP% Nginx+Memcached%

<?php echo "Hello World"; ?>

Page 34: Running PHP on Nginx / PHP wgtn

34

•  set%HTTP%response%expires%header%

Client%Side%Caching%

location ~ \.(html|js|css|gif|jpg|jpe|jpeg|png|bmp|tif|pdf|ico)$ { expires 90d; access_log off; error_log off; try_files $uri =404; }

Page 35: Running PHP on Nginx / PHP wgtn

35

• keep%handlers%for%requested%staMc%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;

Page 36: Running PHP on Nginx / PHP wgtn

36

Load%balancing%PHP%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; }

}

Page 37: Running PHP on Nginx / PHP wgtn

37

• Nginx%running%with%default%semngs%

• Apache%•  AllowOverride%None%

•  MulM[process%(prefork)%mode%%to%allow%usage%of%mod_php%

Benchmarking%Nginx%vs%Apache%

Page 38: Running PHP on Nginx / PHP wgtn

38

StaMc%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%respo

nse%Mme%[sec]%

Page 39: Running PHP on Nginx / PHP wgtn

39

Performance%Monitoring%

Page 40: Running PHP on Nginx / PHP wgtn

40

Performance%Tools%

Page 41: Running PHP on Nginx / PHP wgtn

41

monitor'all'transac,ons'in'all'channels'

Page 42: Running PHP on Nginx / PHP wgtn

42

monitor'your'infrastructure'

Page 43: Running PHP on Nginx / PHP wgtn

43

process'monitoring'

Page 44: Running PHP on Nginx / PHP wgtn

44

iden,fy'response',me'hotspots'in'the'backend'

Page 45: Running PHP on Nginx / PHP wgtn

45 drill'down'to'find'the'root'cause'

Page 46: Running PHP on Nginx / PHP wgtn

46

• Load%Generator%%(Apache%Benchmark,%Selenium,%JMeter)%

• Firebug,%Google%Developer%Tools%Dynatrace%Ajax%EdiMon%

• Google%PageSpeed%• Dynatrace%Free%Trial%

•  Free%trial%license%for%30%days%

•  Free%personal%license%for%developers%

My%favorites%

hjp://bit.ly/djrial%

Page 47: Running PHP on Nginx / PHP wgtn

47

www.dynatrace.com%

Thank%you%!!!%

Harald'Zeitlhofer'Senior%Technology%Strategist%

#HZeitlhofer%[email protected]%

hjp://blog.dyntrace.com%