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…

Facing a Git error with the 'vi' editor? Discover how to easily fix it by checking your Vim path and configuring Git to use...
Sysadmin.ID
10 sec read

Apache :: MP4 Streaming

Learn how to enable MP4 streaming in Apache with the installation of the mod_h264 streaming module, making your video content accessible and efficient. Follow...
Sysadmin.ID
18 sec read

Apache :: Flash / FLV…

Learn how to effortlessly configure Apache for FLV streaming by installing the mod_flvx module and updating your httpd.conf file. Discover the simple steps to...
Sysadmin.ID
15 sec read

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

  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 *