📜 ⬆️ ⬇️

Setting up php-cgi in conjunction with sockets-based nginx

Hello.

Below the hood is an example of how you can configure php-cgi based on unix: sockets.
The example is designed for Ubuntu and Debian distributions.


Introduction


Personally, I really like this method, so I decided to share it with the public, which I don’t know.
')
Well, firstly, because it is very simple.
Secondly, flexible thanks to sockets
Third, fast.
Well, and other resulting opportunities, including those with rights)

Installation


Next is nothing special, standard actions:
nginx

sudo apt-get update
sudo apt-get install nginx

php

sudo apt-get install php5-cgi

We connect php and nginx


For binding, it is enough to use the script of one good man Till Klampaeckel.
And so, in the /etc/init.d/ directory we create the file php-fcgid
write the following into it:
 #! / bin / sh
 #
 # Author: Till Klampaeckel <till@php.net>
 # Credits
 # * original script: http://unix.derkeiler.com/Mailing-Lists/FreeBSD/questions/2007-09/msg00468.html
 # * improved: http://till.klampaeckel.de/blog/archives/30-PHP-performance-III-Running-nginx.html
 # linux start script all inspired by CouchDB's start script (by Noah Slater)

 SCRIPT_OK = 0
 SCRIPT_ERROR = 1

 DESCRIPTION = "php-fcgi super-duper-control thing"
 NAME = php-fgcid
 SCRIPT_NAME = $ (basename $ 0)

 WWW_GROUP = www-data
 PHP_CGI = / usr / bin / php-cgi

 phpfcgid_users = "user1 user2"
 phpfcgid_children = "2"
 phpfcgid_tmpdir = "/ tmp"
 phpfcgid_requests = "100"

 log_daemon_msg () {
     echo $ @
 }

 log_end_msg () {
     # Dummy function to be replaced by LSB library.

     if test "$ 1"! = "0";  then
         echo "Error with $ DESCRIPTION: $ NAME"
     fi
     return $ 1
 }

 phpfcgid_start () {
     echo "Starting $ NAME with $ phpfcgid_children children (req: $ phpfcgid_requests)."

     export PHP_FCGI_CHILDREN = $ phpfcgid_children
     export PHP_FCGI_MAX_REQUESTS = $ phpfcgid_requests

     for user in $ {phpfcgid_users};  do
         socketdir = "$ {phpfcgid_tmpdir} /. fastcgi. $ {user}"
         mkdir -p $ {socketdir}
         chown $ {user}: $ {WWW_GROUP} $ {socketdir}
         chmod 0750 $ {socketdir}
         su -m $ {user} -c "$ {PHP_CGI} -b $ {socketdir} / socket &"
     done
 }

 phpfcgid_stop () {
     echo "Stopping $ NAME."
     pids = `pgrep php-cgi`
     pkill php-cgi
 }

 phpfcgid_status () {
     log_daemon_msg "To be implemented: status"
     log_end_msg $ SCRIPT_ERROR
 }


 parse_script_option_list () {

     case "$ 1" in
         start)
             log_daemon_msg "Starting $ DESCRIPTION" $ NAME
             if phpfcgid_start;  then
                 log_end_msg $ SCRIPT_OK
             else
                 log_end_msg $ SCRIPT_ERROR
             fi
             ;;
         stop)
             log_daemon_msg "Stopping $ DESCRIPTION" $ NAME
             if phpfcgid_stop;  then
                 log_end_msg $ SCRIPT_OK
             else
                 log_end_msg $ SCRIPT_ERROR
             fi
             ;;
         restart | force-reload)
             log_daemon_msg "Restarting $ DESCRIPTION" $ NAME
             if phpfcgid_stop;  then
                 if phpfcgid_start;  then
                     log_end_msg $ SCRIPT_OK
                 else
                     log_end_msg $ SCRIPT_ERROR
                 fi
             else
                 log_end_msg $ SCRIPT_ERROR
             fi
             ;;
         status)
             phpfcgid_status
             ;;
         *)
             cat << EOF> & 2
 Usage: $ SCRIPT_NAME {start | stop | restart | force-reload | status}
 EOF
             exit $ SCRIPT_ERROR
             ;;
     esac
 }

 parse_script_option_list $ @


Customize


In the nginx settings, we configure user matching with fastcgi_pass.
Config example:
 server {
	 listen 80;
	 server_name myhost.com;

	 access_log /var/log/nginx/myhost.com.access.log;

	 location / {
		 autoindex on;
		 root /var/www/myhost.com;
		 index index.php index.html index.htm;
	 }

	 location ~ .php {
		 include fastcgi_params;

		 fastcgi_pass unix: /tmp/.fastcgi.www-data/socket;
		 fastcgi_index index.php;

		 fastcgi_param SCRIPT_FILENAME /var/www/myhost.com/$fastcgi_script_name;
	 }
 }


First run of the script:
sudo bash /etc/init.d/php-fcgid

Restarting nginx, after the changes, I think zanane how.

Actually that's all.
Enjoy!

Source: https://habr.com/ru/post/84587/


All Articles