Lighttpd: uma alternativa mais leve do que o Apache

Muitas vezes o Apache apresenta problemas de performance, principalmente consumo excessivo de memória. Há uma opção interessante, o lighttpd. Ele permite ganhar disponibilidade e economizar recursos do servidor. Mas para servir o Drupal, o lighttpd precisa de algumas configurações especiais. Segue um passo-a-passo para uma instalação funcional do lighttpd, pronto para rodar o Drupal. Instalando o básico Em primeiro lugar, é necessário instalar o lighttpd, o PHP 5 com suporte a CGI, o mod_magnet para lighttpd. O mod_magnet usa a linguagem de script lua para permitir urls limpas no drupal, e os gerenciadores de pacotes costumam instalar esta dependência automaticamente. A nomenclatura de pacotes mais comum é lighttpd, lighttpd-mod-magnet e php5-cgi Habilitando os módulos necessários: No lighttpd, precisamos ativar os módulos fastcgi e magnet. Habilitando as URLs amigáveis: Crie o seguinte arquivo: /etc/lighttpd/drupal.lua Com o seguinte conteúdo:
-- little helper function
function file_exists(path)
  local attr = lighty.stat(path)
  if (attr) then
      return true
  else
      return false
  end
end
function removePrefix(str, prefix)
  return str:sub(1,#prefix+1) == prefix.."/" and str:sub(#prefix+2)
end

-- prefix without the trailing slash
local prefix = ''

-- the magic ;)
if (not file_exists(lighty.env["physical.path"])) then
    -- file still missing. pass it to the fastcgi backend
    request_uri = removePrefix(lighty.env["uri.path"], prefix)
    if request_uri then
      lighty.env["uri.path"]          = prefix .."/index.php"
      local uriquery = lighty.env["uri.query"] or ""
      lighty.env["uri.query"] = uriquery .. (uriquery ~= ""and "&" or "") .. "q=" .. request_uri
      lighty.env["physical.rel-path"] =lighty.env["uri.path"]
      lighty.env["request.orig-uri"]  =lighty.env["request.uri"]
      lighty.env["physical.path"]     =lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"]
    end
end
-- fallthrough will put it back into the lighty request loop
-- that means we get the 304 handling for free. ;)
Adicione a seguinte linha ao arquivo /etc/lighttpd/lighttpd.conf :
# for clean urls
magnet.attract-physical-path-to = ( "/etc/lighttpd/drupal.lua" )
Permita o php alocar mais memória para script quando necessário, editando o arquivo php.ini, que costuma estar na pasta /etc/php5/cgi. Mude a diretiva:
memory_limit = 16M
para:
memory_limit = 50M
Ajustes finais Agora, só falta adaptar o /etc/lighttpd/lighttpd.conf às suas necessidades. Este arquivo costuma ser bem comentado e auto-explicativo Por exemplo, para definir a raiz de um determinado domínio,use:
$HTTP["host"] == "www.exemplo.com.br" { server.document-root = "/var/www/www.exemplo.com.br" }

Comments

Ótimo post, vou testar lá

Ótimo post, vou testar lá em casa ;)

VirtualHost?

O lighttpd suporta virtual host?

Sim!

Sim! Conheço dois jeitos de criar Virtual Hosts no lighty, um com a configuração:
$HTTP['domain'] == "www.example.com" {
   server.document-root = "/sites/www.example.com"
}
$HTTP['domain'] == "mail.example.com" {
   server.document-root = "/usr/share/squirrelmail"
}
$HTTP['domain'] == "mysql.example.com" {
   server.document-root = "/usr/share/phpmyadmin"
}
Desta forma, você pode definir uma série de recursos especificamente para alguns hosts. Aceita os operadores: == , que compara a igualdade de duas strings =~ , que valida uma expressão regular Além disso, pode-se usar também o módulo Simple Vhost, mais info em: http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModSimpleVhost -- Lourenzo Ferreira

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters (without spaces) shown in the image.