Boost your website by running
PHP on Nginx
Tips and tricks for high performance websites
Harald Zeitlhofer @HZeitlhofer
harald.zeitlhofer@dynatrace.com
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
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 heavyweight
CSS and JS files with up to 288 KB!!!
PHP-FPM
• Installation
• Pool configuration
/etc/php/7.0/fpm/pool.d/www.conf
[www]
user = www-data
group = www-data
listen = 127.0.0.1:9000# for Unix socket: unix:/var/run/php7.0-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/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
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
/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;
}
Integration
• Static content served by Nginx
• Dynamic requests sent to PHP
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:9000;
include fastcgi_params;
}
}
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;