miércoles, 8 de enero de 2014

Make NGINX send PHP scripts through UNIX socket

I proved this working on both Fedora 19 and 20 as that of today.

After following this great tutorial by falko I got my web server with MySQL (MariaDB) and PHP support up and running in just a few minutes, then I remembered that making PHP listen on a UNIX socket rather than the traditional TCP port could hel you save a few bytes of overhead, which is specially helpful on very busy sites, and speeds things a bit up on very low resource computers.

However, they don't specify how to do so on the above mentioned tutorial, which is why I googled a little and found another tutorial (for CentOS this time, again by falko) which mentions how to do so, and its rather simple:

1.- After having everything installed, open the /etc/php-fpm.d/www.conf file (with root privileges) and look for the listen option.

2.- Change (or comment it) and set it to the unix socket you want to use, for example: /var/run/php5-fpm.sock, you can specify any location you want, but the /var/run/ directory is a pretty common location.


3.- Save it and then open the /etc/nginx/nginx.conf file.

4.- Locate your ~ \.php$ stanza and find the fastcgi_pass option.



5.- Again, change or comment it and set it to the path of the unix socket you specified before (/var/run/php5-fpm.sock in my case), preceded by the unix: protocol specifier.

6- Now just restart both nginx.service and php-fpm.service by issuing:
systemctl restart php-fpm.service
then
systemctl restart nginx.service
with root privileges of course.

7.- Done! Now PHP listens for scripts through a UNIX socket, avoiding all the TCP overhead.

I know the performance diference is way too little to even be noticeable, but I look at it this way: TCP ports are limited (up to 65535, minus 1024 which are reserved) and UNIX sockets (which are like any other regular files) are just limited by the filesystem you create them in, about 4,000,000,000 or maybe even more on newer systems.

Have a great day ;) and let me now if this helped you someway.

No hay comentarios.:

Publicar un comentario