Nginx & php-fpm

Quelques notes en vrac à propos de l'installation d'un serveur web de production avec nginx et PHP en fast cgi sous debian Lenny.

Ajout des dépots doteb avec php 5.3 :

# fichier /etc/apt/sources.list
deb http://php53.dotdeb.org stable all 
deb-src http://php53.dotdeb.org stable all

Ajout de la clé GnuPG :

gpg --keyserver keys.gnupg.net --recv-key 89DF5277
gpg -a --export 89DF5277 | apt-key add -

Mettre à jour les dépots et la distro :

apt-get update
apt-get upgrade

Installer nginx :

apt-get install nginx

Installer php5, php-fpm, pear/pecl :

apt-get install php5-fpm php5-cli php5-common php5-dev php5-apc php-pear

Configurer nginx pour traiter les requêtes PHP via php-fpm, le gestionnaire de process fcgi :

# file : /etc/nginx/nginx.conf

user www-data;
worker_processes  2;

error_log /var/log/nginx/error.log; pid /var/run/nginx.pid;

events { worker_connections 1024; }

http { include /etc/nginx/mime.types; default_type application/octet-stream;

<span class="kn">access_log</span>  <span class="n">/var/log/nginx/access.log</span><span class="p">;</span>

<span class="kn">sendfile</span>        <span class="no">on</span><span class="p">;</span>
<span class="kn">tcp_nopush</span>     <span class="no">on</span><span class="p">;</span>

<span class="kn">keepalive_timeout</span>  <span class="mi">65</span><span class="p">;</span>
<span class="kn">tcp_nodelay</span>        <span class="no">on</span><span class="p">;</span>

<span class="c1"># Enable Gzip:

gzipon;gzip_http_version1.0;gzip_comp_level5;gzip_min_length512;gzip_buffers48k;gzip_proxiedany;gzip_types# text/html is always compressed by HttpGzipModule text/csstext/javascripttext/xmltext/plaintext/x-componentapplication/javascriptapplication/x-javascriptapplication/jsonapplication/xmlapplication/rss+xmlfont/truetypefont/opentypeapplication/vnd.ms-fontobjectimage/svg+xml;

<span class="c1"># gzip_static on;

gzip_disable"MSIE[1-6]\.";gzip_varyon;

<span class="kn">server</span> <span class="p">{</span>
    <span class="kn">listen</span> <span class="mi">80</span> <span class="s">default_server</span><span class="p">;</span>
    <span class="c1">#server_name www.example.com;

root/home/http/default;indexindex.htmlindex.htm;

    <span class="kn">charset</span> <span class="s">utf-8</span><span class="p">;</span>
    <span class="c1">#error_page 404 /404.html;

location~*^.+\.(css|js|jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|eot|mp4|ogg|ogv|webm)$ {expiresmax;access_logoff;}}

<span class="kn">include</span> <span class="n">/etc/nginx/conf.d/*.conf</span><span class="p">;</span>
<span class="kn">include</span> <span class="n">/etc/nginx/sites-enabled/*</span><span class="p">;</span>

}

# fichier : /etc/nginx/conf.d/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 PATH_INFO $fastcgi_script_name;

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;

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

# Custom settings fastcgi_connect_timeout 60; fastcgi_send_timeout 180; fastcgi_read_timeout 180; fastcgi_buffer_size 128k; fastcgi_buffers 4 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; fastcgi_intercept_errors on; fastcgi_ignore_client_abort on;

Exemple de vhost :

server {
    listen   80;
    server_name  www.monsite.tld;

<span class="kn">access_log</span>  <span class="n">/var/log/nginx/localhost.access.log</span><span class="p">;</span>

<span class="c1">#error_page  404  /404.html;

# redirect server error pages to the static page /50x.html # error_page500502503504/50x.html;location=/50x.html{root/home/http/html;}

<span class="c1"># 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${fastcgi_pass127.0.0.1:9000;# alternative via socket #fastcgi_pass unix:/dev/shm/php-fpm.sock;
fastcgi_indexindex.php;includefastcgi_params;}

<span class="c1"># deny access to .htaccess files, if Apache's document root

# concurs with nginx's one # location~/.ht{denyall;}}

Démarrage et test

/etc/init.d/nginx start
/etc/init.d/php5-fpm start

Enjoy !

blog comments powered by Disqus