+=====================================================+
         |                                                     |
         |    Step by Step Nginx Installation From Source      |
         |                    by ditatompel                    |
         |             http://crayonindonesia.com              |
         |               http://devilzc0de.org                 |
         |                  english version                    |
         |                                                     |
         +=====================================================+
         | Video : https://www.youtube.com/watch?v=AtJ5OBOj1gE |
         +=====================================================+


http://blog.ditatompel.crayoncreative.net/how-to/install-nginx-latest-stable-version-from-source-with-spawn-fcgi/
http://devilzc0de.org/forum/thread-9865.html


uname -roms : Linux 2.6.39.4 i686 GNU/Linux
There are some required libraries and tools for installing the web server, some
parameters that you will have to decide upon when compiling the binaries, and
some extra configuration to do on your system.

As you can see, this time we choose to download the source code application and
install it manually instead of using the package manager. There are several
reasons why people choose to install it manually:
1. the package may not be available in the repositories of our Linux
distribution
2. We would know more about how this f**king awesome web server system that we
use work.
in addition, the rare repositories that offer to download and install latest
stable Nginx version automatically using package manager ( Yum | Pacman |
Aptitude | Yast); mostly contain outdated versions.

First, let's download our web server from
http://nginx.org/download/nginx-1.0.5.tar.gz
(When I write this article the latest stable version is 1.0.5)
wget http://nginx.org/download/nginx-1.0.5.tar.gz

then copy source to /usr/local/src and extract it
tar -xvzf /usr/local/src/nginx-1.0.5.tar.gz

Note :
1. before we start the installation process, it always better to check whether
port 80 is being used or not. I've use BackTrack distribution and by default
apache uses port 80. /etc/init.d/apache2 stop or killall apache2
2. Nginx is a program written in C, so you will first need to install a compiler
tool such as the GNU Compiler Collection (GCC) on your system. GCC usually comes
with most distributions.
to make sure GCC is already installed on your system, simply run "gcc" ( without
quote ). If you get the following output gcc: no input files GCC is correctly
installed on your system. Otherwise, you need to install it.

go to the folder nginx-1.0.5 under /usr/local/src  directory and start
compiling.
cd /usr/local/src/nginx-1.0.5
/configure

Info :
By default, installing nginx will automatically install HTTP rewrite module
too. This module requires PCRE (Perl Compatible Regular Expression) library for
HTTP Rewrite and Core modules. Nginx uses PCRE for their regular expressions
syntax.
Now depending on your choice, if you:
1. requires a rewrite module, you should install PCRE:
apt-get install libpcre3 libpcre3-dev
2. otherwise, if you do not need it. Configure nginx without HTTP rewrite module
/configure --without-http_rewrite_module

My site require those rewrite module. So i've decide to install PCRE to enable
the Nginx HTTP rewrite module. So after installing PCRE, we should reconfigure
Nginx again.
/configure

See image below. if you get following output you've configure nginx correctly.

[images]

Continue the installation process
make && make install

The default installation process like I did will place the nginx workspace in
the /usr/local/nginx directory

create an init script for nginx

create file named "nginx" under /etc/init.d directory
nano /etc/init.d/nginx

then copy and paste the shell script below and save

#! /bin/sh
### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/nginx/sbin/nginx
NAME=nginx
DESC="nginx daemon"

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
       . /etc/default/nginx
fi

set -e

case "$1" in
       start)
               echo -n "Starting $DESC: "
               start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/nginx.pid \
                       --exec $DAEMON -- $DAEMON_OPTS
               echo "$NAME."
               ;;
       stop)
               echo -n "Stopping $DESC: "
               start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/nginx.pid \
                       --exec $DAEMON
               echo "$NAME."
               ;;
       restart|force-reload)
               echo -n "Restarting $DESC: "
               start-stop-daemon --stop --quiet --pidfile \
                       /usr/local/nginx/logs/nginx.pid --exec $DAEMON
               sleep 1
               start-stop-daemon --start --quiet --pidfile \
                       /usr/local/nginx/logs/nginx.pid --exec $DAEMON -- $DAEMON_OPTS
               echo "$NAME."
               ;;
       reload)
               echo -n "Reloading $DESC configuration: "
               start-stop-daemon --stop --signal HUP --quiet --pidfile
/usr/local/nginx/logs/nginx.pid \
                       --exec $DAEMON
               echo "$NAME."
               ;;
       *)
       N=/etc/init.d/$NAME
       echo "Usage: $N {start|stop|restart|force-reload}" >&2
       exit 1
       ;;
esac
exit 0

chmod + x so the scripts can be executed
chmod +x /etc/init.d/nginx

After this, you can start, stop, restart or reload nginx daemon through the
script.
Let's try run nginx server
/etc/init.d/nginx start

then you should get a gretting message "welcome to nginx!" when accessing
localhost from your brwoser.


Installing and configuring PHP FAST CGI (spawn-fcgi) with nginx
apt-get install php5-cgi spawn-fcgi

after the installing with the package manager finished, create file named
php-fastcgi under /etc/init.d directory
nano /etc/init.d/php-fastcgi

Copy and paste the shell script below.
#!/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

Do not forget to chmod + x so the script can be executed

then before run the php-fastcgi, build our website structure. (I chose the
/var/www/nginx directory)
mkdir /var/www/nginx; cd nginx

create phpinfo file named info.php to make sure php is working with nginx
echo "<? php phpinfo ();?>"> info.php

then edit nginx configuration to fit with our website structure.
nano /usr/local/nginx/conf/nginx.conf
with the following configuration (Note that our root public html was in
/var/www/nginx)
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#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;

   server {
       listen       80;
       server_name  localhost;

       #charset koi8-r;

       #access_log  logs/host.access.log  main;

       location / {
           root   /var/www/nginx;
           index  index.html index.htm;
       }

       #error_page  404              /404.html;

       # redirect server error pages to the static page /50x.html
       #
       error_page   500 502 503 504  /50x.html;
       location = /50x.html {
           root   html;
       }

       # proxy the PHP scripts to Apache listening on 127.0.0.1:80
       #
       #location ~ \.php$ {
       #    proxy_pass   http://127.0.0.1;
       #}

       # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
       #
       location ~ \.php$ {
           root           html;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  /var/www/nginx$fastcgi_script_name;
           fastcgi_param  PATH_INFO  $fastcgi_script_name;
           include        fastcgi_params;
       }

       # deny access to .htaccess files, if Apache's document root
       # concurs with nginx's one
       #
       #location ~ /\.ht {
       #    deny  all;
       #}
   }


   # another virtual host using mix of IP-, name-, and port-based configuration
   #
   #server {
   #    listen       8000;
   #    listen       somename:8080;
   #    server_name  somename  alias  another.alias;

   #    location / {
   #        root   html;
   #        index  index.html index.htm;
   #    }
   #}


   # HTTPS server
   #
   #server {
   #    listen       443;
   #    server_name  localhost;

   #    ssl                  on;
   #    ssl_certificate      cert.pem;
   #    ssl_certificate_key  cert.key;

   #    ssl_session_timeout  5m;

   #    ssl_protocols  SSLv2 SSLv3 TLSv1;
   #    ssl_ciphers  HIGH:!aNULL:!MD5;
   #    ssl_prefer_server_ciphers   on;

   #    location / {
   #        root   html;
   #        index  index.html index.htm;
   #    }
   #}

}



to make sure out configuration are correct, we can run the command as follows:
/usr/local/nginx/sbin/nginx -t

if the syntax are correct, we can reload nginx daemon nginx using the init file
we've made before.
/etc/init.d/nginx restart

Test accessing info.php through your browser.
http://localhost/test.php
From there we can determine if nginx was was successfuly run with php or not.