Tuesday, January 13, 2009

Instalarea nginx cu suport PHP

Nginx este un server web mic, dar foarte eficient si performant.
Poate fi utilizat ca web server, suportand incarcari foarte mari in ceea ce priveste servirea paginilor statice, reverse proxy pentru a reduce incarcarea pe serverul backend.
Datorita faptului ca nu exista destula documentatie in engleza, nu este atat de popular.
Voi discuta despre instalarea sa cu suport PHP ca FastCGI .

Ultima versiune la momentul scrierii acestui document - 0.3.46 poate fi descarcata de pe http://sysoev.ru/nginx/nginx-0.3.46.tar.gz.

Pe FreeBSD - instalare din porturi


# cd /usr/ports/www/nginx
# make install clean


optiuni disponibile:
WITH_OPENSSL_MODULE - va compila nginx cu suport OpenSSL


# echo "nginx_enable=\"YES\"" >> /etc/rc.conf

Linux sau orice alt Unix based OS, din surse (pe un sistem Linux rpm-based ar trebui sa aveti instalate pcre-devel si gzip-devel pentru a profita
din plin de capacitatile nginx , plus adaugati optiunea --with-cc-opt=" -I /usr/include/pcre" la compilare ) :

# ./configure --prefix=/usr --error-log-path=/var/log/nginx/error_log --http-log-path=/var/log/nginx/access_log --user=nobody --group=nobody --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx.pid --http-client-body-temp-path=/var/spool/nginx/client_body_temp --http-proxy-temp-path=/var/spool/nginx/proxy_temp --http-fastcgi-temp-path=/var/spool/nginx/fastcgi_temp --with-cc-opt="-I /usr/include/pcre"


# make && make install
# mkdir -p /var/spool/nginx/
# chown nobody:nobody /var/spool/nginx/



Urmeaza editarea fisierului de configurare /usr/local/etc/nginx/nginx.conf ( FreeBSD ) , /etc/nginx/nginx.conf (Linux) .
Fisierul implicit e suficient pentru a rula nginx .


events {
worker_connections 1024; #
use epoll; # Linux
use kqueue; # BSD
}


http {
include /etc/nginx/mime.types; # Linux
include /usr/local/etc/nginx/mime.types # FreeBSD
default_type application/octet-stream;

sendfile on; #
gzip on; #

server {

# presupunem ca docroot este /var/www/html

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


# suportul PHP
location ~ .php$ {
# PHP va rula ca FastCGI pe portul 9000
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
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;

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;

# PHP only, required if PHP was built
# with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;

}


}

}





Deoarece nu se poate rula PHP ca mod_php, e nevoie de a rula PHP ca FastCGI, ceea ce e posibil in doua moduri :

# folosind un FastCGI wrapper, ca de exemplu cel din lighttpd (spawn-fcgi + spawn-php.sh), vor fi posibile conexiuni prin socket, cat si TCP .
# pornind PHP implicit cu optiunea -b IP:PORT , in cazul dat vor fi acceptate conexiuni doar pe TCP .


Am ales prima varianta.
Instalati PHP prin metoda preferata, cu conditia ca la compilare sa fie indicate urmatoarele optiuni :

'--enable-discard-path' '--enable-force-cgi-redirect' '--enable-fastcgi'

Din documentatia PHP :

--enable-force-cgi-redirect
Enable the security check for internal server redirects. You should use this if you are running the CGI version with Apache.

--enable-discard-path
If this is enabled, the PHP CGI binary can safely be placed outside of the web tree and people will not be able to circumvent .htaccess security.

--enable-fastcgi
If this is enabled, the cgi module will be built with support for fastcgi also.


Am redenumit executabilul php in php-cgi din motiv ca rulez o PHP compilat ca fastcgi nu permite rularea scripturilor din shell, cron .


Instalarea wrapper-ului propriu-zis :

Linux

Descarcati lighttpd de pe pagina sa


# ./configure --prefix=/usr
# make

# cp doc/spawn-php.sh /usr/bin/spawn-php.sh
# cp src/spawn-fcgi /usr/sbin/spawn-fcgi





FreeBSD


# cd /usr/ports/www/lighttpd/; make<
# cp work/lighttpd-1.4.11/src/spawn-fcgi /usr/local/sbin/spawn-fcgi
# cp work/lighttpd-1.4.11/doc/spawn-php.sh /usr/local/bin/spawn-php.sh





Urmeaza editarea fisierului spawn-php.sh si setarea urmatorilor parametri :


## ABSOLUTE path to the spawn-fcgi binary
SPAWNFCGI="/usr/sbin/spawn-fcgi"

## ABSOLUTE path to the PHP binary
FCGIPROGRAM="/usr/local/bin/php-cgi"

## TCP port to which to bind on localhost
FCGIPORT="9000"

## number of PHP children to spawn
PHP_FCGI_CHILDREN=10
## maximum number of requests a single PHP process can serve before
## it is restarted
PHP_FCGI_MAX_REQUESTS=1000

## IP addresses from which PHP should access server connections
FCGI_WEB_SERVER_ADDRS="127.0.0.1"

# allowed environment variables, separated by spaces
ALLOWED_ENV="ORACLE_HOME PATH USER"

## if this script is run as root, switch to the following user
USERID=nobody
GROUPID=nobody





Pentru a manipula mai usor nginx pe un Linux RedHat based, am creat un init script simplu , care poate fi descarcat de
aici >>>

# wget http://www.unixro.net/art/nginx.init.txt -O /etc/init.d/nginx
# chmod 750 /etc/init.d/nginx

Pe FreeBSD se va folosi /usr/local/etc/rc.d/nginx

Nu uitati sa rulati si spawn-php.sh si sa-l adaugati in init-scripturi pentru a starta la bootare.

# /usr/bin/spawn-php.sh
spawn-fcgi.c.190: child spawned successfully: PID: 21547

In listingul proceselor va aparea ca

nobody 21562 0.0 0.1 4440 1316 ? Ss 19:23 0:00 /usr/local/bin/php-cgi
nobody 21563 0.0 0.1 4440 1324 ? S 19:23 0:00 /usr/local/bin/php-cgi

Daca totul a decurs fara probleme, un phpinfo() va returna :

Server API CGI/FastCGI

###


Informatie suplimentara la :

http://nginx.net/
http://blog.kovyrin.net/2006/04/04/nginx-small-powerful-web-server/
http://blog.kovyrin.net/2006/04/17/typical-nginx-configurations/
http://www.lighttpd.net/download/spawn-fcgi.txt
http://www.fastcgi.com/

No comments:

Post a Comment