\ Dexter's Laboratories » nginx and PHP
Home > Internet, Red Hat, Software > nginx and PHP

nginx and PHP

After installing (I will maybe post all steps later). Some basic settings and it is runing. cannot run scripts directly, for this reason you need to use fastcgi.

What do you need for this? 1st the php-cgi installed. To check where it is use

server ~/ : which php-cgi
/usr/bin/php-cgi


There are couple of different ways to spawn fcgi. This is the method I use for my desktop where I do my devs.

server ~/ $ cat /etc/init.d/php-fastcgi
$!/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

Some usefull fastcgi parameters that you need to know

server ~/ $ cat /etc/nginx/fastcgi_params
 
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
 
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
 
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
 
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

you can add more if you need and will be in the $_ superglobal in PHP.

After running the php-fastcgi init script let’s review some nginx configuration.

server ~/ $ cat /etc/nginx/nginx.conf
user  nginx;
worker_processes  1;
 
error_log  logs/error.log;
 
pid        logs/nginx.pid;
 
events {
    worker_connections  1024;
}
 
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  logs/access.log  main;
 
    sendfile        on;
    $tcp_nopush     on;
 
    $keepalive_timeout  0;
    keepalive_timeout  65;
 
    gzip  on;
    gzip_disable  msie6;
 
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*.conf;
 
}

Here I just define some basics and the main http behaviour of nginx. This is the default virtualhost of nginx that include the a location section for PHP.

server ~/ $ cat /etc/nginx/sites-available/default.conf
server {
        listen   80 default;
        server_name  _;
 
        access_log  /var/log/nginx/localhost.access.log;
 
        server_name_in_redirect  off;
 
        root   /var/www/;
        index  index.php;
 
	location ~ \.php$ {
		include /etc/nginx/fastcgi_params;
		fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
		fastcgi_pass  127.0.0.1:9000;
	}
 
}

Now you are done with the configuration. start nginx. And in the root of the default server here /var/www create a file index.php with the following:

<?php phpinfo(); ?>

with your favorite browser go to http://localhost if you did all correctly PHP info should show up.

Good luck!

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...
3,128 views