Wednesday, May 9, 2012

Setup Nginx for GPRS

1/ Install nginx

  • sudo aptitude install php5-cgi
  • sudo aptitude install nginx
2/ Create PHP 5 FastCGI start-up script:
    • sudo vim /etc/init.d/php-fastcgi
      • Inside, put:
        • #!/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
    • Make start-up script executable:
      • sudo chmod +x /etc/init.d/php-fastcgi
    • Launch PHP:
      • sudo /etc/init.d/php-fastcgi restart
    • Launch at start-up:
      • sudo update-rc.d php-fastcgi defaults
    • That's it. All installed and ready to go.

3/ Test:

    • In your server config, add the following:
      • location ~ \.php$ {
            fastcgi_pass    127.0.0.1:9000;
            fastcgi_index   index.php;
            fastcgi_param   SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
            include         fastcgi_params;
        }
    • Restart nginx:
      • sudo /etc/init.d/nginx restart
    • Create /var/www/test.php: phpinfo();

4/ Restart php-fpm
sudo /etc/init.d/php-fpm restart.

5/ Setup virtual host

server {
        listen  80;
        server_name     154.154.154.11 default;
        autoindex on;
#format log
        log_format main '$remote_addr - $server_addr - $remote_user [$time_local]  '
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent"';
        access_log      /var/log/nginx/154.154.154.11.access.log main;
        error_log      /var/log/nginx/154.154.154.11.error.log;
        rewrite_log on;
        index           index.php;

       root /home/web/sites/154.154.154.11;


        location /gprs.mytable.com.au {
                rewrite /gprs.mytable.com.au/(?!(scripts|images|styles)) /gprs.mytable.com.au/index.php last;
        }

#        location ~ /gprs.mytable.com.au/(?!order-request).*\.php$ {


        location  /gprs {
            alias  /home/web/data/mytable.com.au/uploads/order-request/;
            gzip off;
            #include /etc/nginx/mime.types;
            #default_type text/html;
            add_header Content-Type "text/html";

        }



#       rewrite  ^/gprs.mytable.com.au/order-callback\.php /gprs.mytable.com.au/orders/callback/ permanent;
        rewrite  ^/gprs.mytable.com.au/order-callback\.php /gprs.mytable.com.au/orders/callback/ last;
        location = /gprs.mytable.com.au/order-request.php {

             if ($args ~ "a=(.+)&u=(.+)&p=(.+)"){

                  set $res $1;
                  set $user $2;
                  set $pass $3;
                  rewrite ^/gprs.mytable.com.au/order-request\.php "/gprs/$res-$user-$pass.html?" last;
             }
        }

        location ~ /gprs.mytable.com.au/(?!(order-request|order-callback)).*\.php$ {
                #include /etc/nginx/fastcgi_params;
                #fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                fastcgi_param   SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
                fastcgi_param APPLICATION_ENV live;
                fastcgi_index  index.php;
                fastcgi_pass  127.0.0.1:9000;     //Please check port
                include         fastcgi_params;
        }

        location ~ \.php$ {
                 fastcgi_pass    127.0.0.1:9000;  //Please check port
                 fastcgi_index   index.php;
                 fastcgi_param   SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
                 include         fastcgi_params;
        }
}

No comments:

Post a Comment