Ubuntu :: PHP FastCGI + NGINX

1 min read

:: How to setup PHP FastCGI for NGINX in Ubuntu
+ Installing NGINX and php-cgi

apt-get update
apt-get install nginx php5-cli php5-cgi spawn-fcgi

+ Make php-fcgi script in init.d

vi /etc/init.d/php-fcgi

add these line:

#!/bin/bash

BIND=127.0.0.1:9000
USER=www-data
PHP_FCGI_CHILDREN=15
PHP_FCGI_MAX_REQUESTS=1000

PHP_CGI=/usr/bin/php-cgi
PHP_CGI_NAME=`basename $PHP_CGI`
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
RETVAL=0

start() {
      echo -n "Starting PHP FastCGI: "
      start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}
stop() {
      echo -n "Stopping PHP FastCGI: "
      killall -q -w -u $USER $PHP_CGI
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}

case "$1" in
    start)
      start
  ;;
    stop)
      stop
  ;;
    restart)
      stop
      start
  ;;
    *)
      echo "Usage: php-fastcgi {start|stop|restart}"
      exit 1
  ;;
esac
exit $RETVAL

+ change permission anda run the script when booting

chmod +x /etc/init.d/php-fcgi
update-rc.d php-fcgi defaults 

+ setup PHP FastCGI in NGINX

vi /etc/nginx/sites-enabled/blackonsole.org

add line in server {} like these:

location ~ .php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME /var/www/blackonsole.org$fastcgi_script_name;
                keepalive_timeout 0;
        }

so the configuration like:

server {
        listen   80;

	error_log /var/log/nginx/blackonsole.org-error.log
	access_log /var/log/nginx/blackonsole.org-access.log

        root /var/www/blackonsole.org;
        index index.php index.html index.htm;

        # Make site accessible from //localhost/
        server_name blackonsole.org;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to index.html
                try_files $uri $uri/ /index.html;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules

                if (!-e $request_filename) {
                        rewrite ^(.*)$ /index.php?q=$1 last;
                        break;
                }
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ .php$ {
        #       fastcgi_split_path_info ^(.+.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
                fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
        #       fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME /var/www/blackonsole.org$fastcgi_script_name;
                keepalive_timeout 0;
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /.ht {
                deny all;
        }
}

+ restarting NGINX and php-fcgi

/etc/init.d/nginx restart
/etc/init.d/php-fcgi restart

+ checking the daemon

netstat -tlupn

make sure you see nginx and php-fcgi service

 
:: Links
+ Google
+ Tomasz

How to fix problem with the editor ‘vi’

:: git error with vi error: There was a problem with the editor 'vi'   :: how to fix the problem 0. check path...
Sysadmin.ID
10 sec read

Apache :: MP4 Streaming

:: How to Enable mp4 Streaming in Apache + Installing mod h624 streaming wget //h264.code-shop.com/download/apache_mod_h264_streaming-2.2.7.tar.gz tar -xzf apache_mod_h264_streaming-2.2.7.tar.gz cd mod_h264_streaming-2.2.7/ ./configure --with-apxs=`which apxs2` make...
Sysadmin.ID
18 sec read

Apache :: Flash / FLV Streaming

:: How to Configure Apache for FLV Streaming + Installing mod_flvx wget //people.apache.org/~pquerna/modules/mod_flvx.c which apxs /usr/bin/apxs /usr/bin/apxs -cia mod_flvx.c + setup mod_flvx in http.conf...
Sysadmin.ID
15 sec read

One Reply to “Ubuntu :: PHP FastCGI + NGINX”

  1. thanks guys. it’s work. apparently nginx in ubuntu require spawn-fcgi for access php script. big thanks

Leave a Reply

Your email address will not be published. Required fields are marked *