Boost your website by running PHP on Nginx
-
Upload
harald-zeitlhofer -
Category
Technology
-
view
1.095 -
download
0
Transcript of Boost your website by running PHP on Nginx
![Page 1: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/1.jpg)
Boost your website by running PHP on NginxTips and tricks for high performance websites
Harald Zeitlhofer @[email protected]
![Page 2: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/2.jpg)
Harald Zeitlhofer
• Technology Strategist at Dynatrace• Database and Web Development• Love to discover new things
![Page 3: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/3.jpg)
Tips and tricks for high performance websites
![Page 4: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/4.jpg)
Once upon a time...
performance matters !!!
![Page 5: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/5.jpg)
performance matters !!!
![Page 6: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/6.jpg)
performance matters !!!
![Page 7: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/7.jpg)
![Page 8: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/8.jpg)
overloaded pages
![Page 9: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/9.jpg)
Modern Web Pages: lots of static content
434 Resources in total on that page:230 JPEGs, 75 PNGs, 50 GIFs, …
more than 20MB page size
![Page 10: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/10.jpg)
Fifa.com during Worldcup 2014
http://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 heavyweightCSS and JS files with up to 288 KB!!!
![Page 11: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/11.jpg)
• Expires• Last-Modified• Etag• Cache-Control
![Page 12: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/12.jpg)
cached content
can still create roundtrips to the network!
![Page 13: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/13.jpg)
Web Request handling
![Page 14: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/14.jpg)
Web Request handling
![Page 15: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/15.jpg)
PHP-FPMFastCGI Process Manager
Available since 5.3.3
Stable since 5.4.1
![Page 16: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/16.jpg)
PHP-FPM• Installation
• Pool configuration/etc/php/7.0/fpm/pool.d/www.conf
[www]user = www-datagroup = www-datalisten = 127.0.0.1:9000 # for Unix socket: unix:/var/run/php7.0-fpm.sock;
root@hzvm01:/etc/nginx/sites-enabled# ps -ef | grep phproot 6435 1 0 14:39 ? 00:00:32 php-fpm: master process (/etc/php/7.0/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 php7.0-fpm
![Page 17: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/17.jpg)
NginxLightweight HTTP serverEvent based request handlingOpen Source project (BSD) Development started in 2002 by Igor Sysoev to solve the c10k problemCommercial version NGINX Plus
![Page 18: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/18.jpg)
Leading among top 100.000 websites
![Page 19: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/19.jpg)
/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;
}
![Page 20: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/20.jpg)
Integration• Static content served by Nginx• Dynamic requests sent to PHPserver {
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:9000; include fastcgi_params;
}}
![Page 21: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/21.jpg)
Communication 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/php7.0-fpm.sock;
![Page 22: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/22.jpg)
Transaction flow
![Page 23: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/23.jpg)
URL rewrite
...RewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-lRewriteRule ^(.+)$ index.php...
http://www.mysite.com/news/browse/2014 to be processed by index.php
![Page 24: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/24.jpg)
URL rewrite
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 unix:/var/run/php5-
fpm.sock;}
}
http://www.mysite.com/news/browse/2014 to be processed by index.php
![Page 25: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/25.jpg)
URL rewriteusing Nginx/PHP-FPM there’s a better way:
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 unix:/var/run/php5-fpm.sock;
}
}
<?php
$params = explode('/', $_SERVER["DOCUMENT_URI"]);...
![Page 26: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/26.jpg)
Finish Web Request
<?php
do_some_processing();
render_page();
fastcgi_finish_request();
do_slower_stuff(Array(‘convert_uploaded_videos’,
‘post_to_social_media_platforms’,‘backup_data’,‘much more’
));
?>
![Page 27: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/27.jpg)
Nginx and Caching
![Page 28: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/28.jpg)
Nginx FastCGI cache• Part of Nginx' FastCGI module
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 29: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/29.jpg)
FastCGI cache in action<?phpecho time()."\n";?>
![Page 30: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/30.jpg)
FastCGI cache in action<?phpecho time()."\n";?>
![Page 31: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/31.jpg)
FastCGI cache in action<?phpecho time()."\n";?>
![Page 32: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/32.jpg)
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);echo $html;...
?>
![Page 33: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/33.jpg)
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));...
?>
![Page 34: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/34.jpg)
Full page / data cache with Nginx and Memcached• ngx_http_memcached_module
server { location / {
set $memcached_key "$uri";memcached_pass localhost:11211;
error_page 404 502 504 = @notincache; }
location @notincache { fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;include fastcgi_params;
}}
![Page 35: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/35.jpg)
PHP, 5k requests, concurrency 100
Apache+PHP Nginx+PHP Nginx+Memcached0
1
2
3
4
5
6
7
8
<?php echo "Hello World";?>
7,28 4,6 3,05tota
l tim
e in
sec
![Page 36: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/36.jpg)
Caching Static Content• set HTTP response expires header
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;}
![Page 37: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/37.jpg)
Filehandle Caching• keep handlers for requested static files open
open_file_cache max=1000 inactive=5m;open_file_cache_valid 60s;open_file_cache_min_uses 5;open_file_cache_errors off;
![Page 38: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/38.jpg)
FastCGI request forwardingserver {
listen 80;root /var/www/mysite;server_name www.mysite.com;location / {
try_files $uri =405;}location ~ \.php$ {
fastcgi_pass 192.168.56.12:9000;fastcgi_index index.php;include fastcgi_params;
}}
![Page 39: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/39.jpg)
FastCGI request forwardingupstream php {
server 192.168.56.12:9000;}
server {listen 80;root /var/www/mysite;server_name www.mysite.com;location / {
try_files $uri =405;}location ~ \.php$ {
fastcgi_pass php;fastcgi_index index.php;include fastcgi_params;
}}
![Page 40: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/40.jpg)
Load balancingupstream 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 /var/www/mysite;server_name www.mysite.com;location / {
try_files $uri =405;}location ~ \.php$ {
fastcgi_pass php;fastcgi_index index.php;include fastcgi_params;
}}
![Page 41: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/41.jpg)
High scalability
![Page 42: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/42.jpg)
High availability
Performance
Maintainability
![Page 43: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/43.jpg)
![Page 44: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/44.jpg)
desktop traffic
response time
![Page 45: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/45.jpg)
slow response time
traffic using chrome
![Page 46: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/46.jpg)
![Page 47: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/47.jpg)
![Page 48: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/48.jpg)
![Page 49: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/49.jpg)
![Page 50: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/50.jpg)
![Page 51: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/51.jpg)
![Page 52: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/52.jpg)
My favorite performance tools
• Load Generator (Apache Benchmark, Selenium, JMeter)
• Firebug, Google Developer Tools
• Dynatrace Application Monitoring Free Trial• Free trial license for 30 days• Free personal license for developers
• Dynatrace Ruxit• 2016 free hours for monitoring
http://bit.ly/monitoring-2016http://bit.ly/dttrial
![Page 53: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/53.jpg)
Further information
http://apmblog.dynatrace.comhttp://apmblog.dynatrace.com/2014/08/19/easily-boost-web-application-using-nginx/http://apmblog.dynatrace.com/2014/10/30/proper-configuration-running-php-nginx/http://apmblog.dynatrace.com/2015/11/24/cool-new-stuff-on-the-nginx-front/https://www.nginx.com/resources/wiki/
![Page 54: Boost your website by running PHP on Nginx](https://reader033.fdocuments.us/reader033/viewer/2022061307/58e74c711a28abd63a8b5dbb/html5/thumbnails/54.jpg)
Thank you !!!Harald ZeitlhoferPerformance [email protected]@dynatrace.comhttp://blog.dynatrace.com
Dynatrace Free TrialFree Personal LicenseRuxit Free Trialhttp://bit.ly/[email protected]