User Tools

Site Tools


tutorials:common:applications:lighttpd_and_php-fpm

This is an old revision of the document!


Build a WEB server with Lighttpd and PHP-fpm on Cubieboard

About this Article

Abstract

In this article, we focus on building a WEB server with Lighttpd and PHP support which is suitable for most of middle-scale or small-scale applications. We mainly talk about Lighttpd + PHP-fastcgi and Lighttpd + PHP-fpm here, which are two different schemes, and you may choose one of them.

Lighttpd + PHP-fastcgi

It's quite easy and will be descriped briefly. Install the Lighttpd first.

$ sudo apt-get install lighttpd

Then enable fastcgi and fastcgi-php support

$ sudo lighttpd-enable-mod fastcgi fastcgi-php 

:!: fastcgi & fastcgi-php should be enabled together, otherwise you will get 403 Forbidden sort of error

Now, you can restart Lighttpd and see what happens.

$ sudo /etc/init.d/lighttpd force-reload

You may test your web site with your browser now.

Lighttpd + PHP-fpm

PHP-fpm is well known as an efficient backend for handling php requests, more efficient than fastcgi-php, people say. The installing and configuring procedure is a bit more tricky, but far from complicated, you can follow these steps.

Configurations

The key problem is how Lighttpd and PHP-fpm communicating with each other. If they work in the exact same system and the exact same machine, commucating through native Unix domain sockets is apparently efficient than TCP sockets, while communicating through TCP sockets could physically seperate Lighttpd and PHP-fpm processes, which is good for distributed systems. We discuss these two kinds of configurations below.

First, we create a script to support fastcgi and PHP-fpm.

$ sudo touch /etc/lighttpd/conf-available/10-php-fpm.conf

Then, we talk about details of different configurations.

Native Unix domain socket commucating configuration

Edit /etc/lighttpd/conf-available/10-php-fpm.conf as follows

server.modules+=("mod_fastcgi")
fastcgi.server = ( ".php" =>
    ( "localhost" =>
        (
         "socket" => "/var/run/php5-fpm.sock"
        )
    )
)

TCP socket communicating configuration

Edit /etc/lighttpd/conf-available/10-php-fpm.conf as follows

server.modules+=("mod_fastcgi")
fastcgi.server = ( ".php" =>
    ((
         "host" => "127.0.0.1",
         "port" => "9000"
    ))
)

Meanwhile, we need modify /etc/php5/fpm/pool.d/www.conf , locate this line

listen = /var/run/php5-fpm.sock

change it to

listen=127.0.0.1:9000

Restart the service

If we tend to use PHP-fpm, we must disable fastcgi-php (but remain the fastcgi), and enable our script for PHP-fpm.

$ sudo lighttpd-disable-mod fastcgi-php
$ sudo lighttpd-enable-mod php-fpm

Restart PHP-fpm and Lighttpd now

$ sudo /etc/init.d/php5-fpm restart
$ sudo /etc/init.d/lighttpd restart

Test

Lighttpd's default www folder is in /var/www/, we create a new test file /var/www/index.php, modify it as follows

<?php
  phpinfo();
?>

Test this web site with your browser now. If the following message is displayed, you make it.

Other PHP modules

We can install more modules to make our server a more powerful one.

$ sudo apt-get install  php5-memcache php5-curl php5-gd php5-mysql php5-mcrypt php5-sqlite

Network tuning

tutorials/common/applications/lighttpd_and_php-fpm.1383890120.txt.gz · Last modified: 2013/12/23 14:50 (external edit)