<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Carlos André Ferrari &#187; nginx</title>
	<atom:link href="http://ferrari.eti.br/tag/nginx/feed/" rel="self" type="application/rss+xml" />
	<link>http://ferrari.eti.br</link>
	<description>php, python, xhtml, html5, css, javascript, jquery, webstandards, linux, nginx</description>
	<lastBuildDate>Thu, 21 Jul 2011 12:42:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>PHP 5.3, PHP-FPM, XCache, Nginx no Debian Lenny &#8211; Performance extrema</title>
		<link>http://ferrari.eti.br/php-5-3-php-fpm-xcache-nginx-no-debian-lenny-performance-extrema/</link>
		<comments>http://ferrari.eti.br/php-5-3-php-fpm-xcache-nginx-no-debian-lenny-performance-extrema/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 14:36:08 +0000</pubDate>
		<dc:creator>caferrari</dc:creator>
				<category><![CDATA[git]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[webserver]]></category>
		<category><![CDATA[debian]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[php-fpm]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[xcache]]></category>

		<guid isPermaLink="false">http://ferrari.eti.br/?p=112</guid>
		<description><![CDATA[Neste post vou exemplificar os passos para configurar um Webserver que consome poucos recursos e é extremamente rápido no Debian Lenny. PHP-FPM: é um gerenciador de processos CGI do php, similar ao Phpcgi, porém com várias vantagens como: Gerenciador de Processos muito eficiente, com capacidade de parar e iniciar seus deamons caso necessário Possibilidade de [...]]]></description>
			<content:encoded><![CDATA[<p>Neste post vou exemplificar os passos para configurar um Webserver que consome poucos recursos e é extremamente rápido no <a href="http://www.debian.org/">Debian Lenny</a>.</p>
<p><a href="http://php-fpm.org/">PHP-FPM</a>: é um gerenciador de processos CGI do php, similar ao Phpcgi, porém com várias vantagens como:</p>
<ul>
<li>Gerenciador de Processos muito eficiente, com capacidade de parar e iniciar seus <a href="http://en.wikipedia.org/wiki/Daemon">deamons</a> caso necessário</li>
<li>Possibilidade de Iniciar processos com usuários diferentes e com arquivos php.ini diferentes</li>
<li>Uploads otimizados</li>
<li>Capacidade de reinicializar processos caso ocorra algum problema</li>
<li>Escalabilidade de processos, ele pode criar mais processos de acordo com a demanda</li>
<li>e muito mais.</li>
</ul>
<p><a href="http://xcache.lighttpd.net/">XCache</a>: é um gerenciador de cache, que armazena os scripts php em <a href="http://en.wikipedia.org/wiki/Opcode">opcode</a> para uso futuro, é extremamente rápido e muito usado no mundo em servidores com alta demanda.</p>
<p><a href="http://nginx.org">Nginx</a>: Conheci este webserver lendo o <a href="http://www.boo-box.com/blog/br/2009/nova-infraestrutura-de-servidores-web-do-sistema-boo-box/">blog da boo-box</a>, ele é um Servidor web e proxy reverso extremamente veloz e que vem ganhando mercado nos últimos meses devido a sua facilidade de uso e capacidade de carga.</p>
<p style="text-align: center;"><a href="http://royal.pingdom.com/2010/01/22/internet-2009-in-numbers/"><img class="aligncenter" title="Market Share de navegadores no final de 2009" src="http://farm5.static.flickr.com/4022/4295546152_b8e094e8cc_o.png" alt="Market Share de navegadores no final de 2009" width="580" height="300" /></a></p>
<p>Para agilizar a configuração, vamos usar alguns pacotes (php5.3, php-fpm) pré compilados e mantidos <a href="http://www.dotdeb.org/">www.dotdeb.org.</a> os testes que eu realzei usando estes pacotes tiveram um ganho de performance enorme.</p>
<h3>Instalação:</h3>
<p>Vamos começar adicionado os repositórios do dotdeb no /etc/apt/sources.list:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">deb http:<span style="color: #000000; font-weight: bold;">//</span>php53.dotdeb.org stable all
deb-src http:<span style="color: #000000; font-weight: bold;">//</span>php53.dotdeb.org stable all</pre></div></div>

<p>Vamos instalar os pacotes e algumas dependências para o nginx:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">aptitude</span> update
<span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> php5-fpm php5-xcache php5-gd php5-curl \
php5-mysql php5-pgsql libssl-dev libpcre3-dev build-essential <span style="color: #660033;">-y</span></pre></div></div>

<p>Agora temos o php instalado, vamos compilar o nginx:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span>
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> src
<span style="color: #7a0874; font-weight: bold;">cd</span> src
<span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>nginx.org<span style="color: #000000; font-weight: bold;">/</span>download<span style="color: #000000; font-weight: bold;">/</span>nginx-0.7.64.tar.gz
<span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-zxvf</span> nginx-0.7.64.tar.gz
<span style="color: #7a0874; font-weight: bold;">cd</span> nginx-0.7.64
.<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--prefix</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>nginx <span style="color: #660033;">--user</span>=www-data <span style="color: #660033;">--group</span>=www-data \
    --without-mail_pop3_module --without-mail_imap_module \
    --without-mail_smtp_module --with-http_stub_status_module \
    --with-http_ssl_module <span style="color: #660033;">--http-log-path</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>access.log \
    <span style="color: #660033;">--conf-path</span>=<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>nginx.conf  <span style="color: #660033;">--pid-path</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span>nginx.pid
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>Se tudo ocorreu bem, você já tem o nginx instalado, vamos apenas coloca-lo para inicializar automáticamente, crie o arquivo /etc/init.d/nginx e coloque o conteúdo:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/sh</span>
&nbsp;
<span style="color: #666666; font-style: italic;">### BEGIN INIT INFO</span>
<span style="color: #666666; font-style: italic;"># Provides:          nginx</span>
<span style="color: #666666; font-style: italic;"># Required-Start:    $all</span>
<span style="color: #666666; font-style: italic;"># Required-Stop:     $all</span>
<span style="color: #666666; font-style: italic;"># Default-Start:     2 3 4 5</span>
<span style="color: #666666; font-style: italic;"># Default-Stop:      0 1 6</span>
<span style="color: #666666; font-style: italic;"># Short-Description: starts the nginx web server</span>
<span style="color: #666666; font-style: italic;"># Description:       starts nginx using start-stop-daemon</span>
<span style="color: #666666; font-style: italic;">### END INIT INFO</span>
&nbsp;
<span style="color: #007800;">PATH</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>sbin:<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin:<span style="color: #000000; font-weight: bold;">/</span>sbin:<span style="color: #000000; font-weight: bold;">/</span>bin:<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin:<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin:<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">local</span>
<span style="color: #007800;">DAEMON</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>nginx
<span style="color: #007800;">NAME</span>=nginx
<span style="color: #007800;">DESC</span>=nginx
&nbsp;
<span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">$DAEMON</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Include nginx defaults if available</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>default<span style="color: #000000; font-weight: bold;">/</span>nginx <span style="color: #7a0874; font-weight: bold;">&#93;</span> ; <span style="color: #000000; font-weight: bold;">then</span>
        . <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>default<span style="color: #000000; font-weight: bold;">/</span>nginx
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">set</span> <span style="color: #660033;">-e</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #000000; font-weight: bold;">in</span>
  start<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Starting <span style="color: #007800;">$DESC</span>: &quot;</span>
        start-stop-daemon <span style="color: #660033;">--start</span> <span style="color: #660033;">--quiet</span> <span style="color: #660033;">--pidfile</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$NAME</span>.pid \
                <span style="color: #660033;">--exec</span> <span style="color: #007800;">$DAEMON</span> <span style="color: #660033;">--</span> <span style="color: #007800;">$DAEMON_OPTS</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #c20cb9; font-weight: bold;">true</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$NAME</span>.&quot;</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
  stop<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Stopping <span style="color: #007800;">$DESC</span>: &quot;</span>
        start-stop-daemon <span style="color: #660033;">--stop</span> <span style="color: #660033;">--quiet</span> <span style="color: #660033;">--pidfile</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$NAME</span>.pid \
                <span style="color: #660033;">--exec</span> <span style="color: #007800;">$DAEMON</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #c20cb9; font-weight: bold;">true</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$NAME</span>.&quot;</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
  restart<span style="color: #000000; font-weight: bold;">|</span>force-reload<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Restarting <span style="color: #007800;">$DESC</span>: &quot;</span>
        start-stop-daemon <span style="color: #660033;">--stop</span> <span style="color: #660033;">--quiet</span> <span style="color: #660033;">--pidfile</span> \
                <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$NAME</span>.pid <span style="color: #660033;">--exec</span> <span style="color: #007800;">$DAEMON</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #c20cb9; font-weight: bold;">true</span>
        <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span>
        start-stop-daemon <span style="color: #660033;">--start</span> <span style="color: #660033;">--quiet</span> <span style="color: #660033;">--pidfile</span> \
                <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$NAME</span>.pid <span style="color: #660033;">--exec</span> <span style="color: #007800;">$DAEMON</span> <span style="color: #660033;">--</span> <span style="color: #007800;">$DAEMON_OPTS</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #c20cb9; font-weight: bold;">true</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$NAME</span>.&quot;</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
  reload<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Reloading <span style="color: #007800;">$DESC</span> configuration: &quot;</span>
        start-stop-daemon <span style="color: #660033;">--stop</span> <span style="color: #660033;">--signal</span> HUP <span style="color: #660033;">--quiet</span> <span style="color: #660033;">--pidfile</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$NAME</span>.pid \
            <span style="color: #660033;">--exec</span> <span style="color: #007800;">$DAEMON</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #c20cb9; font-weight: bold;">true</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$NAME</span>.&quot;</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
  configtest<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Testing <span style="color: #007800;">$DESC</span> configuration: &quot;</span>
        <span style="color: #000000; font-weight: bold;">if</span> nginx <span style="color: #660033;">-t</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span>
        <span style="color: #000000; font-weight: bold;">then</span>
          <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$NAME</span>.&quot;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
          <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #007800;">$?</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
  <span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Usage: <span style="color: #007800;">$NAME</span> {start|stop|restart|reload|force-reload|configtest}&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span>
        <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
<span style="color: #000000; font-weight: bold;">esac</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span></pre></div></div>

<p>depois vamos torna-lo executável e atualizar o rc.d:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chmod</span> +x <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>nginx
update-rc.d nginx defaults</pre></div></div>

<p>Com isso o processo de instalação está concluído, vamos configurar!</p>
<h3>Configuração do XCache</h3>
<p>Coloque a seguinte configuração no arquivo /etc/php5/fpm/conf.d/xcache.ini:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">; configuration for php xcache module</span>
<span style="color: #000099;">extension</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">xcache.so</span>
&nbsp;
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>xcache.admin<span style="">&#93;</span></span>
xcache.admin.user <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;secom&quot;</span>
<span style="color: #666666; font-style: italic;">; xcache.admin.pass = md5($your_password)</span>
xcache.admin.pass <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;&quot;</span>
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>xcache<span style="">&#93;</span></span>
<span style="color: #666666; font-style: italic;">; ini only settings, all the values here is default unless explained</span>
<span style="color: #666666; font-style: italic;">; select low level shm/allocator scheme implemenation</span>
xcache.shm_scheme <span style="color: #000066; font-weight:bold;">=</span>        <span style="color: #933;">&quot;mmap&quot;</span>
<span style="color: #666666; font-style: italic;">; to disable: xcache.size=0</span>
<span style="color: #666666; font-style: italic;">; to enable : xcache.size=64M etc (any size &gt; 0) and your system mmap allows</span>
xcache.size  <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">                64M</span>
<span style="color: #666666; font-style: italic;">; set to cpu count (cat /proc/cpuinfo |grep -c processor)</span>
xcache.count <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">                 1</span>
<span style="color: #666666; font-style: italic;">; just a hash hints, you can always store count(items) &gt; slots</span>
xcache.slots <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">                8K</span>
<span style="color: #666666; font-style: italic;">; ttl of the cache item, 0=forever</span>
xcache.ttl   <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">                 0</span>
<span style="color: #666666; font-style: italic;">; interval of gc scanning expired items, 0=no scan, other values is in seconds</span>
xcache.gc_interval <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">           0</span>
<span style="color: #666666; font-style: italic;">; same as aboves but for variable cache</span>
xcache.var_size  <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">            64M</span>
xcache.var_count <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">             1</span>
xcache.var_slots <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">            8K</span>
<span style="color: #666666; font-style: italic;">; default ttl</span>
xcache.var_ttl   <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">             0</span>
xcache.var_maxttl   <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">          0</span>
xcache.var_gc_interval <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">     300</span>
xcache.test <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">                Off</span>
<span style="color: #666666; font-style: italic;">; N/A for /dev/zero</span>
xcache.readonly_protection <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> Off</span>
<span style="color: #666666; font-style: italic;">; for *nix, xcache.mmap_path is a file path, not directory.</span>
<span style="color: #666666; font-style: italic;">; Use something like &quot;/tmp/xcache&quot; if you want to turn on ReadonlyProtection</span>
<span style="color: #666666; font-style: italic;">; 2 group of php won't share the same /tmp/xcache</span>
<span style="color: #666666; font-style: italic;">; for win32, xcache.mmap_path=anonymous map name, not file path</span>
xcache.mmap_path <span style="color: #000066; font-weight:bold;">=</span>    <span style="color: #933;">&quot;/dev/zero&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">; leave it blank(disabled) or &quot;/tmp/phpcore/&quot;</span>
<span style="color: #666666; font-style: italic;">; make sure it's writable by php (without checking open_basedir)</span>
xcache.coredump_directory <span style="color: #000066; font-weight:bold;">=</span>   <span style="color: #933;">&quot;&quot;</span>
<span style="color: #666666; font-style: italic;">; per request settings</span>
xcache.cacher <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">               On</span>
xcache.stat   <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">               On</span>
xcache.optimizer <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">            On</span>
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>xcache.coverager<span style="">&#93;</span></span>
<span style="color: #666666; font-style: italic;">; per request settings</span>
<span style="color: #666666; font-style: italic;">; enable coverage data collecting for xcache.coveragedump_directory and xcache_coverager_start/stop/get/clean() functions (will hurt executing performance)</span>
xcache.coverager <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">          Off</span>
<span style="color: #666666; font-style: italic;">; ini only settings</span>
<span style="color: #666666; font-style: italic;">; make sure it's readable (care open_basedir) by coverage viewer script</span>
<span style="color: #666666; font-style: italic;">; requires xcache.coverager=On</span>
xcache.coveragedump_directory <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;&quot;</span></pre></div></div>

<h3>Configuração do Nginx</h3>
<p>Edite o arquivo em /etc/nginx/nginx.conf:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">user  www-data;
&nbsp;
<span style="color: #666666; font-style: italic;">#Coloque quantos processos simultaneos, o ideal é um para cada nucleo do sistema:</span>
worker_processes  <span style="color: #000000;">4</span>;
&nbsp;
error_log  <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>error.log;
&nbsp;
events <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #666666; font-style: italic;"># numero de conexões simultaneas por worker</span>
    worker_connections  <span style="color: #000000;">1024</span>;
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
http <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    include       mime.types;
    default_type  application<span style="color: #000000; font-weight: bold;">/</span>octet-stream;
&nbsp;
    access_log  <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>access.log;
    sendfile        on;
    <span style="color: #666666; font-style: italic;"># isso é interessante, dá um ganho enorme na navegação, e o nginx aguenta tranquilamente</span>
    keepalive_timeout  <span style="color: #000000;">60</span>;
    tcp_nodelay        on;
&nbsp;
    <span style="color: #666666; font-style: italic;">## Compressão</span>
    <span style="color: #c20cb9; font-weight: bold;">gzip</span> on;
    gzip_http_version <span style="color: #000000;">1.0</span>;
    gzip_comp_level <span style="color: #000000;">2</span>;
    gzip_proxied any;
    gzip_min_length  <span style="color: #000000;">1100</span>;
    gzip_buffers <span style="color: #000000;">16</span> 8k;
    <span style="color: #666666; font-style: italic;"># mime types para zipar e adicionar header de cache</span>
    gzip_types text<span style="color: #000000; font-weight: bold;">/</span>plain text<span style="color: #000000; font-weight: bold;">/</span>css application<span style="color: #000000; font-weight: bold;">/</span>x-javascript text<span style="color: #000000; font-weight: bold;">/</span>xml application<span style="color: #000000; font-weight: bold;">/</span>xml application<span style="color: #000000; font-weight: bold;">/</span>xml+rss text<span style="color: #000000; font-weight: bold;">/</span>javascript image<span style="color: #000000; font-weight: bold;">/</span>gif image<span style="color: #000000; font-weight: bold;">/</span>jpeg image<span style="color: #000000; font-weight: bold;">/</span>png;
    <span style="color: #666666; font-style: italic;"># Desabilita compressão em algumas versões do IE6</span>
    gzip_disable <span style="color: #ff0000;">&quot;MSIE [1-6].(?!.*SV1)&quot;</span>;
    <span style="color: #666666; font-style: italic;"># Impede que servidores proxy mandem versões em gzip do arquivo para os IE6</span>
    gzip_vary on;
&nbsp;
    server <span style="color: #7a0874; font-weight: bold;">&#123;</span>
	root  <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>;
	server_name     localhost;
	listen          <span style="color: #000000;">80</span>;
	location <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
		index  index.php index.html index.htm;
	<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">#error_page  404  /404.html;</span>
	<span style="color: #666666; font-style: italic;"># redirect server error pages to the static page /50x.html</span>
	error_page   <span style="color: #000000;">500</span> <span style="color: #000000;">502</span> <span style="color: #000000;">503</span> <span style="color: #000000;">504</span>  <span style="color: #000000; font-weight: bold;">/</span>50x.html;
	location = <span style="color: #000000; font-weight: bold;">/</span>50x.html <span style="color: #7a0874; font-weight: bold;">&#123;</span>
		root   <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>nginx-default;
	<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;"># Joga requisições para arquivos .php para o php-fpm</span>
	location ~ \.php$ <span style="color: #7a0874; font-weight: bold;">&#123;</span>
		fastcgi_pass   127.0.0.1:<span style="color: #000000;">9000</span>;
		fastcgi_index  index.php;
		include fastcgi_params;
        	fastcgi_param SCRIPT_FILENAME <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$fastcgi_script_name</span>;
        	fastcgi_param SERVER_NAME <span style="color: #007800;">$http_host</span>;
        	fastcgi_ignore_client_abort on;
	<span style="color: #7a0874; font-weight: bold;">&#125;</span>
    <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<h3>Configuração do php-fpm</h3>
<p>Edite o arquivo: /etc/php5/fpm/php5-fpm.conf e faça os ajustes que você achar interessante, aqui eu só aumento os max_childrens (Numero de clientes CGI) de 5 para 50, esta configuração depende do seu hardware.</p>
<h3>Configuração do Logrotate</h3>
<p>Vamos configurar agora o logrotate para não enchermos os discos com logs do nginx, edite o arquivo /etc/logrotate.d/nginx:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/*</span>.log <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        daily
        missingok
        rotate <span style="color: #000000;">7</span>
        compress
        delaycompress
        notifempty
        create <span style="color: #000000;">640</span> root adm
        sharedscripts
        postrotate
                <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span>nginx.pid <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #660033;">-USR1</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span>nginx.p$
        endscript
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<h3>Finalizando</h3>
<p>Agora que está tudo configurado, vamos reinicalizar os serviços:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>php5-fpm restart
<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>nginx restart</pre></div></div>

<p>crie um arquivo /var/www/index.php e vamos testar o php:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
e acesse<span style="color: #339933;">:</span> http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//ip_do_servidor/</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ferrari.eti.br/php-5-3-php-fpm-xcache-nginx-no-debian-lenny-performance-extrema/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>nginx &#8211; webserver, proxy reverso e perfeito!</title>
		<link>http://ferrari.eti.br/nginx-webserver-proxy-reverso-e-perfeito/</link>
		<comments>http://ferrari.eti.br/nginx-webserver-proxy-reverso-e-perfeito/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 13:00:25 +0000</pubDate>
		<dc:creator>caferrari</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[proxy]]></category>
		<category><![CDATA[webserver]]></category>

		<guid isPermaLink="false">http://ferrari.eti.br/?p=85</guid>
		<description><![CDATA[Nosso departamento é responsável por manter a identidade visual dos sites do Governo do Estado e ainda fazemos os sites dos orgãos que não possuem um. No trabalho sempre tivemos um constante problema para hospedar nossos sites: hardware!. Contamos hoje com dois incríveis servidores IBM Pentium4 3Ghz com 2gb de memória e HDs scsi, um [...]]]></description>
			<content:encoded><![CDATA[<p>Nosso <a href="http://web.secom.to.gov.br">departamento</a> é responsável por manter a identidade visual dos sites do <a href="http://to.gov.br">Governo do Estado</a> e ainda fazemos os sites dos orgãos que não possuem um.</p>
<p>No trabalho sempre tivemos um constante problema para hospedar nossos sites: hardware!. Contamos hoje com dois incríveis servidores IBM Pentium4 3Ghz com 2gb de memória e HDs scsi, um para hospedar mais de 40 sites e o outro onde fica postgres com os bancos de dados desses sites.</p>
<p>Ultimamente nosso problema tem crescido bastante, pois, a demanda por sites aumentou e ao mesmo tempo o número de visitas veio aumentando e, como não conseguimos de forma alguma (até agora) investimento em equipamento novo estavamos chegando em uma situação crítica, nosso servidor começava a <del datetime="2009-10-15T12:28:06+00:00">sentar</del> morrer com 200 conexões simultaneas!</p>
<p>Lembrei-me de um <a href="http://www.boo-box.com/blog/br/2009/a-infraestrutura-de-servidores-web-do-sistema-boo-box/">post do boo-box</a> onde vi um servidor web chamado <a href="http://nginx.net">nginx</a> (lê-se engineX) e decidi pesquisar mais e fazer testes em casa onde rodando um servidor e servindo uma página php com um usleep de meio segundo consegui mais de 2000 conexões simultaneas! uau!..</p>
<p style="text-align: center;"><img class="aligncenter" title="nginx_performance" src="../wp-content/uploads/2009/10/nginx_performance.jpg" alt="nginx_performance" width="500" height="331" /></p>
<p>Na segunda feira seguinte, sem pensar muito, fui para o nosso servidor de produção e apliquei a seguinte política de funcionamento:</p>
<ul>
<li>nginx recebe todas as conexões pela porta 80</li>
<li>nginx faz proxy para o apache (que roda em outra porta) se a solicitação for para uma página php</li>
<li>nginx resolve sozinho usando php-cgi caso a requisição seja para:
<ul>
<li>uma fotografia</li>
<li>um arquivo</li>
<li>uma requisição para o webservice</li>
<li>um download do diário oficial</li>
<li>um banner do Governo</li>
<li>a barra padrão do Estado</li>
<li>ou um arquivo na nossa lib</li>
</ul>
</li>
</ul>
<p><del datetime="2009-11-15T10:54:02+00:00">Como resultado da experiência, nossos sites continuam morrendo, porém, aguentando 700 conexões simultaneas e esse valor só é alcançado quando tem muita pesquisa no banco de dados (Dias que sai algo sobre algum concurso público ou muita demissão no Diário Oficial) logo, fica claro, que o problema não é mais do webserver e sim do nosso servidor de Banco. Bom, o nosso webserver atualmente mal chega a 5% de uso, mesmo com as 700 conexões. Só espero conseguir uma maquina nova e mais eficiente para os nossos Bancos de Dados logo!.</del></p>
<p>Update:<br />
Fomos atendidos, estamos com um Xeon Quad Core com 4Gb de memória para o Banco de dados, e, associado com uma otimização do cache do webservice alcançamos mais de 1000 conexões simultâneas sem nenhuma degradação na performance. Detalhe que o webserver ainda é um P4 3ghz com 1gb de memória em que a utilização de processamento não passa de 5%.</p>
]]></content:encoded>
			<wfw:commentRss>http://ferrari.eti.br/nginx-webserver-proxy-reverso-e-perfeito/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

