Difference between revisions of "Lighttpd"

From ArchWiki
Jump to: navigation, search
(Redirect HTTP requests to HTTPS)
(PHP)
Line 90: Line 90:
 
         "bin-path" => "/usr/bin/php-cgi",
 
         "bin-path" => "/usr/bin/php-cgi",
 
         "socket" => "/var/run/lighttpd/php-fastcgi.sock",
 
         "socket" => "/var/run/lighttpd/php-fastcgi.sock",
 +
        "max-procs" => 4, # default value
 +
        "bin-environment" => (
 +
          "PHP_FCGI_CHILDREN" => "1", # default value
 +
        },
 
         "broken-scriptfilename" => "enable"
 
         "broken-scriptfilename" => "enable"
 
       ))
 
       ))
Line 100: Line 104:
  
 
There is no adaptive spawning anymore in recent lighttpd releases. For dynamic management of PHP processes, you can use {{Package Official|php-fpm}}.
 
There is no adaptive spawning anymore in recent lighttpd releases. For dynamic management of PHP processes, you can use {{Package Official|php-fpm}}.
 
 
  # pacman -S php-fpm
 
  # pacman -S php-fpm
 
  # rc.d start php-fpm
 
  # rc.d start php-fpm

Revision as of 11:49, 16 September 2011

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

Merge-arrows-2.pngThis article or section is a candidate for merging with Lighttpd For Both SSL And Non-SSL.Merge-arrows-2.png

Notes: please use the second argument of the template to provide more detailed indications. (Discuss in Talk:Lighttpd#)

From the lighttpd website:

lighttpd is a secure, fast, compliant, and very flexible web-server that has been optimized for high-performance environments. It has a very low memory footprint compared to other webservers and takes care of cpu-load. Its advanced feature-set (FastCGI, CGI, Auth, Output-Compression, URL-Rewriting and many more) make lighttpd the perfect webserver-software for every server that suffers load problems.

Installation

Lighttpd is available in the extra repository so:

# pacman -S lighttpd

Configuration

Basic Setup

The lighttpd configuration file is: Template:Filename. By default it should produce a working test page.

To check your Template:Filename for bugs you can use this command - helps finding misconfigurations very fast:

$ lighttpd -t -f /etc/lighttpd/lighttpd.conf

The default configuration file specifies Template:Filename as the document directory served.

It may be necessary to add a user and group for http if you do not already have one. That user needs to have write permissions to Template:Filename as well, so we will make it the owner of the folder:

# groupadd http
# adduser http
# chown -R http /var/log/lighttpd

To test the install

# /etc/rc.d/lighttpd start
# touch /srv/http/index.html
# chmod 755 /srv/http/index.html
# echo 'TestMe!' >> /srv/http/index.html

Then point your browser to localhost, and you should see the test page.

You may want to add lighttpd to the daemons list in Template:Filename to start the server on boot.

Examples configuration files are in Template:Filename.

FastCGI

Install fcgi

# pacman -S fcgi

Now you have lighttpd with fcgi support. If it was that what you wanted you are all set. People that want Ruby on Rails and/or PHP should continue. Template:Box Note

The following needs adding to the config file, Template:Filename

server.modules += ( "mod_fastcgi" )

server.indexfiles += ( "dispatch.fcgi" ) #dispatch.fcgi if rails specified

server.error-handler-404   = "/dispatch.fcgi" #too
fastcgi.server = (
    ".fcgi" => (
      "localhost" => ( 
        "socket" => "/var/run/lighttpd/rails-fastcgi.sock",
        "bin-path" => "/path/to/rails/application/public/dispatch.fcgi"
      )
    )
)

Then in Template:Filename:

include "conf.d/fastcgi.conf"

For PHP or Ruby on Rails see the next sections.

PHP

Install php and required modules (see also PHP and LAMP):

# pacman -S php php-cgi

Check that php-cgi is working Template:Codeline

PHP 5.3.1 with Suhosin-Patch (cgi-fcgi) (built: Nov 23 2009 21:12:29)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies

If you get a similar output then php is installed correctly.

Note: Please keep in mind if you receive errors like No input file found after attempting to access your php files then make sure Template:Filename has the directives enabled:
cgi.fix_pathinfo=1
open_basedir = /srv/http/:/home/:/tmp/:/usr/share/pear/:/another/path:/second/path

And that the files are world readable,

# chmod -R 755

In Template:Filename add:

server.modules += ( "mod_fastcgi" )

server.indexfiles += ( "index.php" ) 

fastcgi.server = (
    ".php" => (
      "localhost" => ( 
        "bin-path" => "/usr/bin/php-cgi",
        "socket" => "/var/run/lighttpd/php-fastcgi.sock",
        "max-procs" => 4, # default value
        "bin-environment" => (
          "PHP_FCGI_CHILDREN" => "1", # default value
        },
        "broken-scriptfilename" => "enable"
      ))
)

Then in Template:Filename:

include "conf.d/fastcgi.conf"

Using php-fpm

There is no adaptive spawning anymore in recent lighttpd releases. For dynamic management of PHP processes, you can use Template:Package Official.

# pacman -S php-fpm
# rc.d start php-fpm
Note: You can configure the number of servers in the pool and tweak other configuration options by editing the file Template:Filename. More details on php-fpm can be found on the php-fpm website

In Template:Filename add:

server.modules += ( "mod_fastcgi" )

index-file.names += ( "index.php" ) 

fastcgi.server = (
    ".php" => (
      "localhost" => ( 
        "socket" => "/var/run/php-fpm/php-fpm.sock",
        "broken-scriptfilename" => "enable"
      ))
)

Ruby on Rails

Install and configure FastCGI. (See #FastCGI above.)

Install ruby from [extra] and Template:Package AUR from AUR.

Follow instructions on RubyOnRails.

Python FastCGI

Install flup

# pacman -S python-flup

Configure:

fastcgi.server = (
    ".py" =>
    (
        "python-fcgi" =>
        (
         "socket" => socket_dir + "fastcgi.python.socket",
         "bin-path" => "test.py",
         "check-local" => "disable",
         "max-procs" => 1,
        )
    ))

SSL

Generate an SSL Cert, e.g. like that:

# mkdir /etc/lighttpd/certs
# openssl req -x509 -nodes -days 7300 -newkey rsa:2048 -keyout /etc/lighttpd/certs/www.example.com.pem -out /etc/lighttpd/certs/www.example.com.pem
# chmod 600 /etc/lighttpd/certs/www.example.com.pem

Edit Template:Filename. To make lighttpd SSL-only (you probably need to set the server port to 443 as well)

ssl.engine = "enable" 
    ssl.pemfile = "/etc/lighttpd/certs/www.example.com.pemm"

To enable SSL in addition to normal HTTP

$SERVER["socket"] == ":443" {
    ssl.engine                  = "enable" 
    ssl.pemfile                 = "/etc/lighttpd/certs/www.example.com.pem" 
 }

If you want to serve different sites, you can change the document root inside the socket conditional:

$SERVER["socket"] == ":443" {
    server.document-root = "/srv/ssl" # use your ssl directory here
    ssl.engine                 = "enable"
    ssl.pemfile                = "/etc/lighttpd/certs/www.example.com.pem"  # use the path where you created your pem file
 }

or as alternative you can use the scheme conditional to distinguish between secure and normal requests.

$HTTP["scheme"] == "https" {
    server.document-root = "/srv/ssl" # use your ssl directory here
    ssl.engine                 = "enable"
    ssl.pemfile                = "/etc/lighttpd/certs/www.example.com.pem"  # use the path where you created your pem file
 }

Note that you cannot use the scheme conditional around ssl.engine above, since lighttpd needs to know on what port to enable SSL.

Redirect HTTP requests to HTTPS

You should add "mod_redirect" in server.modules array in Template:Filename:

server.modules              = (
                                     ...
                                "mod_redirect", 
                                     ...
}
$SERVER["socket"] == ":80" {
  $HTTP["host"] =~ "example.org" {
    url.redirect = ( "^/(.*)" => "https://example.org/$1" )
    server.name                 = "example.org" 
  }
}
$SERVER["socket"] == ":443" {
  ssl.engine = "enable" 
  ssl.pemfile = "/etc/lighttpd/ssl/server.pem" 
  server.document-root = "..." 
}

To redirect all hosts to their secure equivalents use the following in place of the socket 80 configuration above:

$SERVER["socket"] == ":80" {
  $HTTP["host"] =~ "(.*)" {
    url.redirect = ( "^/(.*)" => "https://%1/$1" )
  }
}

To redirect all hosts for part of the site (e.g. secure or phpmyadmin):

$SERVER["socket"] == ":80" {
  $HTTP["host"] =~ "(.*)" {
    url.redirect = ( "^/(secure($|/.*))" => "https://%1/$1" )
  }
}

Output Compression

In Template:Filename add

var.cache_dir           = "/var/cache/lighttpd"

Then create directory for a compressed files:

# mkdir /var/cache/lighttpd/compress
# chown http:http /var/cache/lighttpd/compress

Copy example configuration file:

# mkdir /etc/lighttpd/conf.d
# cp /usr/share/doc/lighttpd/config/conf.d/compress.conf /etc/lighttpd/conf.d/

Add following in Template:Filename:

include "conf.d/compress.conf"

Template:Box Note Now restart your server:

# rc.d restart lighttpd

Troubleshooting

Lighttpd downloads .php files

If lighttpd downloads Template:Filename files instead of "initializing" them you probably missed to add these lines to your Template:Filename.

server.modules = (
                   "mod_fastcgi",
                 )

fastcgi.server = ( ".php" => ((
                     "bin-path" => "/usr/bin/php-cgi", #depends where your php-cgi has been installed. Default here.
                     "socket" => "/tmp/php.socket",
                     "max-procs" => 2,
                     "bin-environment" => (
                       "PHP_FCGI_CHILDREN" => "16",
                       "PHP_FCGI_MAX_REQUESTS" => "10000"
                     ),
                     "bin-copy-environment" => (
                       "PATH", "SHELL", "USER"
                     ),
                     "broken-scriptfilename" => "enable"
                 )))

Styles (CSS) not working properly

The default lighttpd config does not include a mimetype definition for CSS so when standards compliant browsers get text/html instead of text/css they get confused and nothing displays properly. To fix this add an entry for CSS.

mimetype.assign	= (
  ".html" => "text/html",
  ".txt" => "text/plain",
  ".jpg" => "image/jpeg",
  ".png" => "image/png",
  ".css" => "text/css"
)

New lines are not needed and are only used here for readability.

See also