<?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; tutorial</title>
	<atom:link href="http://ferrari.eti.br/tag/tutorial/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, 29 Jul 2010 10:05:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Pegando a temperatura de qualquer cidade</title>
		<link>http://ferrari.eti.br/pegando-a-temperatura-de-qualquer-cidade/</link>
		<comments>http://ferrari.eti.br/pegando-a-temperatura-de-qualquer-cidade/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 09:36:38 +0000</pubDate>
		<dc:creator>caferrari</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[codigo]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[temperatura]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://ferrari.eti.br/?p=126</guid>
		<description><![CDATA[Me surgiu um trabalho aqui pelo qual preciso exibir a temperatura atual da cidade, e como estava com insônia decidi brincar um pouco e criar uma solução para isso, fui para o google e pesquisei por &#8220;Previsão do Tempo&#8220;, me surgiram alguns resultados, porém, gostei mais do UOL.
Depois disso foi correr para a criação do [...]]]></description>
			<content:encoded><![CDATA[<p>Me surgiu um trabalho aqui pelo qual preciso exibir a <strong>temperatura</strong> atual da cidade, e como estava com insônia decidi brincar um pouco e criar uma solução para isso, fui para o google e pesquisei por &#8220;<strong>Previsão do Tempo</strong>&#8220;, me surgiram alguns resultados, porém, gostei mais do <a href="http://tempoagora.uol.com.br/previsaodotempo.html/brasil/Palmas-TO/">UOL</a>.</p>
<p>Depois disso foi correr para a criação do script para ler o HTML e retornar um objeto contendo as informações da temperatura no momento, segue o código escrito em <strong>php</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> loadTemp<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cidade</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'Palmas-TO'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// variavel para dados de retorno</span>
	<span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'url'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;http://tempoagora.uol.com.br/previsaodotempo.html/brasil/<span style="color: #006699; font-weight: bold;">$cidade</span>/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// carrega o html do tempoagora</span>
	<span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ret</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Pegamos apenas a parte que interessa do html</span>
	<span style="color: #000088;">$iniflag</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;!-- Insere dados Atuais de AeroPortos ou Est. Meteorologica --&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$fimflag</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;!-- FIM Insere dados Atuais de AeroPortos ou Est. Meteorologica --&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$ini</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$html</span><span style="color: #339933;">,</span> <span style="color: #000088;">$iniflag</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iniflag</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$fim</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$html</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fimflag</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ini</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$html</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ini</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fim</span><span style="color: #339933;">-</span><span style="color: #000088;">$ini</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// pegamos a url do icone</span>
	<span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@&lt;img src=&quot;/(imagens/icones/[a-zA-Z\-]+.gif)&quot;@'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$html</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mat</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$ret</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'icone'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://tempoagora.uol.com.br/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$mat</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Limpa o código html</span>
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strip_tags</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&amp;nbsp;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// quebra o que sobrou em um vetor</span>
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// limpa espaços e linhas em branco</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$k</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$v</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #339933;">===</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// pega horário da verificação</span>
	<span style="color: #000088;">$lin</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@[0-9]{1,2}:[0-9]{1,2}@'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lin</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mat</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$ret</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'verificado'</span><span style="color: #009900;">&#93;</span>  <span style="color: #339933;">=</span> <span style="color: #000088;">$mat</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// pega insidência de nuvens</span>
	<span style="color: #000088;">$ret</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nuvens'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// define nomes melhores para as próximas propriedades</span>
	<span style="color: #000088;">$keys</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'temperatura'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sensacao'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'vento'</span><span style="color: #339933;">,</span> 
			<span style="color: #0000ff;">'pressao'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'umidade'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'visibilidade'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// reseta as chaves do vetor</span>
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_unique</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// percorre chaves impares e aplica as propriedades definidas acima</span>
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">&lt;=</span><span style="color: #cc66cc;">11</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #000088;">$x</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$ret</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$keys</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// retorna os dados como um objeto</span>
	<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span><span style="color: #000088;">$ret</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Analisando o site deles, notei que a <strong>temperatura</strong> é atualizada de hora em hora, então a melhor forma de utilizar essa solução é utilizando o <strong>crontab</strong>, que executa a cada hora, eu escolhi executar no quinto minuto de cada hora cheia (5:05, 6:05, etc).</p>
<p>segue o código <strong>php</strong> que o <strong>crontab</strong> executa:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'loadTemp.php'</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// define nome do arquivo de cache</span>
<span style="color: #000088;">$cache_file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sys_get_temp_dir</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/temp'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// carrega a temperatura</span>
<span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> loadTemp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// salva em cache</span>
<span style="color: #990000;">file_put_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_file</span><span style="color: #339933;">,</span> <span style="color: #990000;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$temp</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>E para exibir no sistema eu fiz uma funçãozinha:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> temp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$cache_file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sys_get_temp_dir</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/temp'</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// se o cache existe, retorna o objeto</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">unserialize</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// senão cria o cache e retorna =)</span>
	<span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> loadTemp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">file_put_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache_file</span><span style="color: #339933;">,</span> <span style="color: #990000;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$temp</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$temp</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>depois, no local específico, ficou algo mais ou menos assim:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> temp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;img src=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$temp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">icone</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; alt=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$temp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nuvens</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; /&gt;
&lt;span&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$temp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">temperatura</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> (&lt;a rel=&quot;external&quot; href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$temp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;saiba mais&lt;/a&gt;)&lt;/span&gt;</pre></div></div>

<p>Minha forma de pagar por esse uso é manter um link para o <a href="http://tempoagora.uol.com.br/">Tempoagora</a>, o que é bastante interessante, pois meu visitante pode querer obter mais informações</p>
<p>É isso ai, abraço a todos</p>
]]></content:encoded>
			<wfw:commentRss>http://ferrari.eti.br/pegando-a-temperatura-de-qualquer-cidade/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<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 Iniciar processos com [...]]]></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>27</slash:comments>
		</item>
		<item>
		<title>Trabalhando em Grupo em um mesmo branch no git</title>
		<link>http://ferrari.eti.br/trabalhando-em-grupo-em-um-mesmo-branch-no-gi/</link>
		<comments>http://ferrari.eti.br/trabalhando-em-grupo-em-um-mesmo-branch-no-gi/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 19:58:29 +0000</pubDate>
		<dc:creator>caferrari</dc:creator>
				<category><![CDATA[git]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://ferrari.eti.br/?p=96</guid>
		<description><![CDATA[Problema: Trabalhar em grupo em um branch que não seja o master usando git.
Solução:
Considerando que o repositório já está ok, e você já está conectado no remote:
Aqui estão os passos para criar o branch que a equipe vai trabalhar

# criar o branch
git checkout -b meu_branch
# enviar o branch para a origem
git push origin meu_branch
# vamos [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Problema:</strong> Trabalhar em grupo em um branch que não seja o master usando git.</p>
<p><strong>Solução:</strong><br />
Considerando que o repositório já está ok, e você já está conectado no remote:</p>
<p>Aqui estão os passos para criar o branch que a equipe vai trabalhar</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># criar o branch</span>
git checkout <span style="color: #660033;">-b</span> meu_branch
<span style="color: #666666; font-style: italic;"># enviar o branch para a origem</span>
git push origin meu_branch
<span style="color: #666666; font-style: italic;"># vamos apagar esse branch local agora</span>
git checkout master
git branch <span style="color: #660033;">-D</span> meu_branch</pre></div></div>

<p>Agora cada um que vai trabalhar com este branch pode adicioná-lo assim:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git checkout <span style="color: #660033;">--track</span> <span style="color: #660033;">-b</span> meu_branch origin<span style="color: #000000; font-weight: bold;">/</span>meu_branch</pre></div></div>

<p>Agora todo mundo da equipe pode trabalhar no &#8220;meu_branch&#8221; como se estivesse no master.</p>
<p>e para excluir o branch da origem faça:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git push origin :meu_branch</pre></div></div>

<p>Até a proxima</p>
]]></content:encoded>
			<wfw:commentRss>http://ferrari.eti.br/trabalhando-em-grupo-em-um-mesmo-branch-no-gi/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Backup automático do postgres + rsync para outra máquina</title>
		<link>http://ferrari.eti.br/backup-automatico-do-postgres-usando-rsync/</link>
		<comments>http://ferrari.eti.br/backup-automatico-do-postgres-usando-rsync/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 19:16:33 +0000</pubDate>
		<dc:creator>caferrari</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[rsync]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://ferrari.eti.br/?p=91</guid>
		<description><![CDATA[Aqui vou exemplificar a solução que usamos para fazer backup diário dos nossos bancos de dados, este backup é armazenado em um servidor fora do país. para fazer a sincronia, usamos o rsync.
1) Configurando login automático via chaves digitais no nosso servidor de armazenamento
Primeiro você tem que gerar as chaves no seu servidor de banco, [...]]]></description>
			<content:encoded><![CDATA[<p>Aqui vou exemplificar a solução que usamos para fazer backup diário dos nossos bancos de dados, este backup é armazenado em um servidor fora do país. para fazer a sincronia, usamos o <a href="http://samba.anu.edu.au/rsync/">rsync</a>.</p>
<h2>1) Configurando login automático via chaves digitais no nosso servidor de armazenamento</h2>
<p>Primeiro você tem que gerar as chaves no seu servidor de banco, com o usuário do banco de dados, no meu caso é o usuário &#8220;postgres&#8221;.</p>
<p>Você será perguntado por uma senha, deixe em branco e confirme:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">su</span> postgres
$ <span style="color: #c20cb9; font-weight: bold;">ssh-keygen</span> <span style="color: #660033;">-t</span> dsa</pre></div></div>

<p>Agora você terá sua chave, copie-a para seu servidor que irá armazenar o backup</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">scp</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>id_dsa.pub user<span style="color: #000000; font-weight: bold;">@</span>server_bkp:~<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>Agora acesse seu servidor via ssh e adicione a chave pública na lista de chaves autorizadas do ssh</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ssh</span> user<span style="color: #000000; font-weight: bold;">@</span>server_bkp
$ <span style="color: #c20cb9; font-weight: bold;">cat</span> ~<span style="color: #000000; font-weight: bold;">/</span>id_dsa.pub <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh<span style="color: #000000; font-weight: bold;">/</span>authorized_keys</pre></div></div>

<p>Volte para o seu servidor de banco no usuario do postgres e teste:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ssh</span> user<span style="color: #000000; font-weight: bold;">@</span>server_bkp</pre></div></div>

<p>Se você conseguiu acessar o server de backup sem digitar senha, continue no passo 2, senão, de uma revisada. qualquer dúvida comente!.</p>
<h2>2) Instalando o rsync e script de backup</h2>
<p>Para instalar o rsync no debian é bem simples:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> rsync</pre></div></div>

<p>No nosso banco de dados os backups são feitos diariamente as 19 horas, e usamos a pasta /backup para armazena-los:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>backup
$ <span style="color: #c20cb9; font-weight: bold;">touch</span> <span style="color: #000000; font-weight: bold;">/</span>backup<span style="color: #000000; font-weight: bold;">/</span>backup.sh
$ <span style="color: #c20cb9; font-weight: bold;">chmod</span> +x <span style="color: #000000; font-weight: bold;">/</span>backup<span style="color: #000000; font-weight: bold;">/</span>backup.sh
$ <span style="color: #c20cb9; font-weight: bold;">chown</span> postgres:postgres <span style="color: #000000; font-weight: bold;">/</span>backup <span style="color: #660033;">-R</span></pre></div></div>

<p>Agora, edite o arquivo backup.sh 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/bash</span>
&nbsp;
<span style="color: #007800;">PASTA</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">DATA</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">DIA</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>backup<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$PASTA</span>
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>backup<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$PASTA</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$DIA</span>
&nbsp;
<span style="color: #007800;">LIST</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span>psql <span style="color: #660033;">-l</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{ print $1}'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-vE</span> <span style="color: #ff0000;">'^-|^\(|^List|^N[o|a]me|template[0|1]'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">for</span> d <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$LIST</span>
<span style="color: #000000; font-weight: bold;">do</span>
        pg_dump <span style="color: #660033;">-i</span> <span style="color: #660033;">-h</span> 127.0.0.1 <span style="color: #660033;">-p</span> <span style="color: #000000;">5432</span> <span style="color: #660033;">-U</span> postgres <span style="color: #660033;">-F</span> t <span style="color: #660033;">-b</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;/backup/<span style="color: #007800;">$DATA</span>-<span style="color: #007800;">$d</span>.backup&quot;</span> <span style="color: #007800;">$d</span>
        <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-zcf</span> <span style="color: #000000; font-weight: bold;">/</span>backup<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$PASTA</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$DIA</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$DATA</span>-<span style="color: #007800;">$d</span>.tar.gz <span style="color: #000000; font-weight: bold;">/</span>backup<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$DATA</span>-<span style="color: #007800;">$d</span>.backup
        <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #000000; font-weight: bold;">/</span>backup<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$DATA</span>-<span style="color: #007800;">$d</span>.backup
        <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>rsync <span style="color: #660033;">-avzh</span> <span style="color: #660033;">--delete</span> <span style="color: #660033;">--progress</span> <span style="color: #660033;">--update</span> <span style="color: #000000; font-weight: bold;">/</span>backup<span style="color: #000000; font-weight: bold;">/</span> user<span style="color: #000000; font-weight: bold;">@</span>server_bkp:~<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<p>faça um teste e rode o script com o user do postgres:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">su</span> postgres
$ <span style="color: #000000; font-weight: bold;">/</span>backup<span style="color: #000000; font-weight: bold;">/</span>backup.sh</pre></div></div>

<p>Se tudo ocorrer bem, é só adicionar no crontab para executar quando você quiser:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">su</span>
$ pico <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>crontab</pre></div></div>

<p>e adicionar:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000;">0</span> <span style="color: #000000;">19</span>    <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span>   postgres    <span style="color: #000000; font-weight: bold;">/</span>backup<span style="color: #000000; font-weight: bold;">/</span>backup.sh</pre></div></div>

<p>Reinicie o cron e o backup será feito todos os dias as 19 horas.</p>
]]></content:encoded>
			<wfw:commentRss>http://ferrari.eti.br/backup-automatico-do-postgres-usando-rsync/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Autenticação de Usuários no Vórtice PHP</title>
		<link>http://ferrari.eti.br/autenticacao-de-usuarios-no-vortice-php/</link>
		<comments>http://ferrari.eti.br/autenticacao-de-usuarios-no-vortice-php/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 19:02:57 +0000</pubDate>
		<dc:creator>caferrari</dc:creator>
				<category><![CDATA[vortice]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://ferrari.eti.br/?p=71</guid>
		<description><![CDATA[Nesse post vou explicar como adicionar autenticação de usuários no @vorticephp, como base para o sistema vamos utilizar o exemplo que já vem com o framework.
Script de BD:

CREATE TABLE IF NOT EXISTS orgaos &#40;
id int&#40;11&#41; NOT NULL AUTO_INCREMENT,
sigla varchar&#40;50&#41; collate utf8_unicode_ci NOT NULL,
nome varchar&#40;150&#41; collate utf8_unicode_ci NOT NULL,
PRIMARY KEY  &#40;id&#41;
&#41; ENGINE=MyISAM  DEFAULT CHARSET=utf8
&#160;
CREATE TABLE IF [...]]]></description>
			<content:encoded><![CDATA[<p>Nesse post vou explicar como adicionar autenticação de usuários no <a href="http://twitter.com/vorticephp">@vorticephp</a>, como base para o sistema vamos utilizar o exemplo que já vem com o framework.</p>
<p>Script de BD:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> orgaos <span style="color: #66cc66;">&#40;</span>
id int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
sigla varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> collate utf8_unicode_ci <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
nome varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">150</span><span style="color: #66cc66;">&#41;</span> collate utf8_unicode_ci <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM  <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> usuarios <span style="color: #66cc66;">&#40;</span>
id int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
nome varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
email varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
senha char<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> usuarios <span style="color: #66cc66;">&#40;</span>nome<span style="color: #66cc66;">,</span> email<span style="color: #66cc66;">,</span> senha<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Administrador'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'adm@ferrari.eti.br'</span><span style="color: #66cc66;">,</span> md5<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'123'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>eu criei essa tabela com esse usuario e senha no database que já vem configurado no framework para vocês testarem localmente. Você pode configurar a conexão com o seu próprio banco de dados no arquivo app/app.php</p>
<p>Para iniciar vamos criar o objeto do usuário em: app/model/Usuario.php e codificar:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> Usuario<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$nome</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$email</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$senha</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nome</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$nome</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">email</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$email</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">senha</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$senha</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>agora que temos o objeto, vamos criar um DAO para realizar operações, crie o arquivo app/model/UsuarioDAO.php e segue o código:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> UsuarioDAO<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getByEmail<span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM usuarios WHERE email='<span style="color: #006699; font-weight: bold;">$email</span>'&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> Database<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">queryOne</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>vamos para o controller de usuários, crie o arquivo app/controller/UsuarioController.php e segue o codigo inicial:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> UsuarioController <span style="color: #000000; font-weight: bold;">extends</span> Controller<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> login<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Dai a gente cria o nosso formulário de login, edite app/view/usuario/login.php:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;h2&gt;Login&lt;/h2&gt;
&lt;fieldset&gt;
	&lt;form method=&quot;post&quot;&gt;
		&lt;p&gt;&lt;label&gt;E-mail:&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;email&quot; /&gt;&lt;/p&gt;
		&lt;p&gt;&lt;label&gt;Senha:&lt;/label&gt;&lt;input type=&quot;password&quot; name=&quot;senha&quot; /&gt;&lt;/p&gt;
		&lt;p&gt;&lt;input type=&quot;submit&quot; value=&quot;Logar!&quot; /&gt;&lt;/p&gt;
	&lt;/form&gt;
&lt;/fieldset&gt;</pre></div></div>

<p>Agora com o form criado, vamos editar a action login do controller Usuario, volte e edite o arquivo app/controller/UsuarioController.php:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> UsuarioController <span style="color: #000000; font-weight: bold;">extends</span> Controller<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> login<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// se um formulario foi postado</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>post<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// pega as variáveis do post</span>
			<span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #990000;">addslashes</span><span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$senha</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'senha'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// seleciona o usuario, armazena na variavel $u</span>
			<span style="color: #666666; font-style: italic;">// e ainda verifica se o usuario existe</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$u</span> <span style="color: #339933;">=</span> UsuarioDAO<span style="color: #339933;">::</span><span style="color: #004000;">getByEmail</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">// verifica se a senha digitada combina com</span>
				<span style="color: #666666; font-style: italic;">// a que está no BD</span>
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$u</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">senha</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$senha</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #666666; font-style: italic;">// remove a senha do objeto</span>
					<span style="color: #000088;">$u</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">senha</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
					<span style="color: #666666; font-style: italic;">// grava a session</span>
					Session<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;u&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$u</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #666666; font-style: italic;">// Cria mensagem de sucesso e redireciona para o index</span>
					Post<span style="color: #339933;">::</span><span style="color: #004000;">setSucesso</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Olá <span style="color: #006699; font-weight: bold;">{$u-&gt;nome}</span>, você está logado&quot;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #990000;">Link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span>
					<span style="color: #666666; font-style: italic;">// Retorna erro</span>
					Post<span style="color: #339933;">::</span><span style="color: #004000;">setErros</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Senha incorreta!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span>
				<span style="color: #666666; font-style: italic;">// Retorna erro</span>
				Post<span style="color: #339933;">::</span><span style="color: #004000;">setErros</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;e-mail não encontrado!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Para ficar bonita a url vamos criar um route para esta action, edite o arquivo app/routes.php e adicione:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Route<span style="color: #339933;">::</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;^login$&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;usuario:login&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Agora o formulário de login já está funcionando, vamos fazer o sistema obrigar o usuario estar logado para fazer qualquer coisa no sistema, para isso edite o arquivo app/controller/MasterController.php, como queremos que para todo o sistema sejá necessário o login, vamos adicionar um construtor nessa classe com o seguinte código:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// se a session &quot;u&quot; não existir, criar uma com um usuario vazio</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Session<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;u&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> Session<span style="color: #339933;">::</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;u&quot;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">new</span> Usuario<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Se a session u tiver um objeto vazio..</span>
	<span style="color: #666666; font-style: italic;">// e o controller/action forem diferentes dos de login</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>Session<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;u&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">&amp;&amp;</span> controller <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'usuario'</span> <span style="color: #339933;">&amp;&amp;</span> action <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'login'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// redireciona para a tela de login</span>
		redirect<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #990000;">Link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;login&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Bom, com isso já deve estar tudo funcionando de forma eficiente e simples. Qualquer dúvida ou sugestão mande sem hexitar.</p>
]]></content:encoded>
			<wfw:commentRss>http://ferrari.eti.br/autenticacao-de-usuarios-no-vortice-php/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
