Lighttpd 1.4.32 + PHP 5.4.14(cgi-fcgi) + Mysql 5.5.30 howto

============================================================
============================================================Compile/install PHP

since the firmware php is just a apache2 module, this php installation will be the default php of the system.


----download source code to /DataVolume/shares/Public/php-5.4.14.tar.gz
mkdir -p /DataVolume/shares/dev/php
mybook:/DataVolume/shares/dev/php# mv …/…/Public/php-5.4.14.tar.gz ./
mybook:/DataVolume/shares/dev/php# tar -xzf php-5.4.14.tar.gz

----compile tools needed. just install and confirm one by one.
autoconf: 2.13+ (for PHP < 5.4.0), 2.59+ (for PHP >= 5.4.0)
automake: 1.4+
libtool: 1.4.x+ (except 1.4.2)
re2c: Version 0.13.4 or newer
flex: Version 2.5.4 (for PHP <= 5.2)
bison: Version 1.28 (preferred), 1.35, or 1.75

aptitude install autoconf


----dev components needed by php.
mybook:/DataVolume/shares/dev/php/php-5.4.14# aptitude install libssl-dev
mybook:/DataVolume/shares/dev/php/php-5.4.14# aptitude install lbzip2 bzip2 libzip-dev
aptitude install libdb5.1-dev
aptitude install libpng+±dev

----1. config para I am using
./configure --enable-debug --without-pear --enable-fpm --enable-sysvsem --enable-sysvshm --with-mhash --with-pcre-regex --enable-calendar --enable-dba --with-db4 --disable-pdo --without-pdo_sqlite --without-sqlite3 --with-mysql=/opt/mysql-5.5.30 --with-gettext --enable-mbstring --enable-mbregex --with-openssl --enable-ftp --enable-zip --with-zlib --with-gd=shared --with-png-dir=/usr

----2. make install
mybook:/DataVolume/shares/dev/php/php-5.4.14# make
mybook:/DataVolume/shares/dev/php/php-5.4.14# make install
Installing shared extensions: /usr/local/lib/php/extensions/debug-non-zts-20100525/
Installing PHP CLI binary: /usr/local/bin/
Installing PHP CLI man page: /usr/local/php/man/man1/
Installing PHP FPM binary: /usr/local/sbin/
Installing PHP FPM config: /usr/local/etc/
Installing PHP FPM man page: /usr/local/php/man/man8/
Installing PHP FPM status page: /usr/local/php/fpm/
Installing PHP CGI binary: /usr/local/bin/
Installing build environment: /usr/local/lib/php/build/
Installing header files: /usr/local/include/php/
Installing helper programs: /usr/local/bin/
program: phpize
program: php-config
Installing man pages: /usr/local/php/man/man1/
page: phpize.1
page: php-config.1
/DataVolume/shares/dev/php/php-5.4.14/build/shtool install -c ext/phar/phar.phar /usr/local/bin
ln -s -f /usr/local/bin/phar.phar /usr/local/bin/phar

----3. test to get php info.
mybook:/DataVolume/shares/dev/php# php -r “phpinfo();” | more
----something like following
PHP Version => 5.4.14

System => Linux mybook 2.6.32.11-svn70860 #1 Thu May 17 13:32:51 PDT 2012 ppc
Configure Command => ‘./configure’ ‘–enable-debug’ ‘–without-pear’ ‘–enable-fpm’ ‘–enable-sysvsem’ ‘–enable-sysvshm’ ‘–with-mhash’ ‘–with-pcre-regex’ ‘–enable -calendar’ ‘–enable-dba’ ‘–with-db4’ ‘–disable-pdo’ ‘–without-pdo_sqlite’ ‘–without-sqlite3’ ‘–with-mysql=/opt/mysql-5.5.30’ ‘–with-gettext’ ‘–enable-mbstring’ ‘–enable-mbregex’ ‘–with-openssl’ ‘–enable-ftp’ ‘–enable-zip’ ‘–with-zlib’ ‘–with-gd=shared’ ‘–with-png-dir=/usr’
Server API => Command Line Interface
Virtual Directory Support => disabled

Configuration File (php.ini) Path => /usr/local/lib

Registered PHP Streams => https, ftps, compress.zlib, php, file, glob, data, http, ftp, phar, zip
Registered Stream Socket Transports => tcp, udp, unix, udg, ssl, sslv3, tls

extension_dir => /usr/local/lib/php/extensions/debug-non-zts-20100525
Default timezone => America/Toronto

DBA support => enabled
Supported handlers => cdb cdb_make db4 inifile flatfile

DOM/XML => enabled
DOM/XML API Version => 20031129
libxml Version => 2.8.0
HTML Support => enabled
XPath Support => enabled
XPointer Support => enabled
Schema Support => enabled
RelaxNG Support => enabled
Simplexml support => enabled

Regex Library => Bundled library enabled
fileinfo support => enabled
version => 1.0.5

FTP support => enabled
GetText Support => enabled
Hashing Engines => md2 md4 md5 sha1 sha224 sha256 sha384 sha512 ripemd128 ripemd160 ripemd256 ripemd320 whirlpool tiger128,3 tiger160,3 tiger192,3 tiger128,4 tiger160,4 tiger192,4 snefru snefru256 gost adler32 crc32 crc32b fnv132 fnv164 joaat haval128,3 haval160,3 haval192,3 haval224,3 haval256,3 haval128,4 haval160,4 haval192,4 haval 224,4 haval256,4 haval128,5 haval160,5 haval192,5 haval224,5 haval256,5

iconv implementation => glibc
json support => enabled
json version => 1.2.1
libXML Compiled Version => 2.8.0

Multibyte string engine => libmbfl

MySQL Support => enabled
Client API version => 5.5.30
MYSQL_MODULE_TYPE => external
MYSQL_SOCKET => /tmp/mysql.sock
MYSQL_INCLUDE => -I/opt/mysql-5.5.30/include
MYSQL_LIBS => -L/opt/mysql-5.5.30/lib -lmysqlclient

OpenSSL support => enabled
OpenSSL Library Version => OpenSSL 1.0.1e 11 Feb 2013

PCRE (Perl Compatible Regular Expressions) Support => enabled
PCRE Library Version => 8.32 2012-11-30

Phar-based phar archives => enabled
Tar-based phar archives => enabled
ZIP-based phar archives => enabled
gzip compression => enabled
bzip2 compression => disabled (install pecl/bz2)
Native OpenSSL support => enabled

Tokenizer Support => enabled
libxml2 Version => 2.8.0
XMLReader => enabled
XMLWriter => enabled

Zip version => 1.11.0

--------now PHP compiled and installed.

============================================================test php connect to mysql.
mybook:/DataVolume/shares/dev/php# more ./testmysql.php

<? $host="192.168.0.80"; $user="root"; $pass="xxx"; $db="yourdb"; $connect = mysql\_connect ( $host, $user, $pass ) ; if (! $connect ){ echo "ERROR connecting to $host
"; trigger\_error ( mysql\_error(), E\_USER\_ERROR ); return; } echo "connected.\r\n"; mysql\_select\_db($db) or die( "Unable to select database $db"); $sql="select \* from X"; $result = mysql\_query($sql); $num=mysql\_numrows($result); mysql\_close(); // echo "Got rows num=".$num."

\r\n"; $i=0; while ($i < $num) { $a=mysql\_result($result,$i,"a"); $b=mysql\_result($result,$i,"b"); echo " a=$a, b=$b
\r\n"; $i++; } echo "-------done."; ?>

mybook:/DataVolume/shares/dev/php# php ./testmysql.php
connected.
Got rows num=2


a=1, b=tt

a=2, b=bb

-------done.
mybook:/DataVolume/shares/dev/php#

============================================================Install lighttpd and use fastcgi/php
--------download lighttpd
mybook:/DataVolume/shares/dev# mkdir /DataVolume/shares/dev/lighttpd/lighttpd
mybook:/DataVolume/shares/dev# cd /DataVolume/shares/dev/lighttpd/lighttpd/
mybook:/DataVolume/shares/dev/lighttpd# wget http://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-1.4.32.tar.gz
mybook:/DataVolume/shares/dev/lighttpd# tar -xzf lighttpd-1.4.32.tar.gz
mybook:/DataVolume/shares/dev/lighttpd# cd lighttpd-1.4.32

----pcore
aptitude install libpcre3-dev
----bzip2
aptitude install libghc-bzlib-dev
aptitude install libbz2-ocaml-dev
----compile
mybook:/DataVolume/shares/dev/lighttpd/lighttpd-1.4.32# ./configure
mybook:/DataVolume/shares/dev/lighttpd/lighttpd-1.4.32# make
mybook:/DataVolume/shares/dev/lighttpd/lighttpd-1.4.32# make install
Libraries have been installed in:
/usr/local/lib
.libs/mod_fastcgi.so ==> /usr/local/lib/mod_fastcgi.so
lighttpd ==> /usr/local/sbin/lighttpd

----check version
mybook:/DataVolume/shares/dev/lighttpd/lighttpd-1.4.32# /usr/local/sbin/lighttpd -v
lighttpd/1.4.32 - a light and fast webserver

------------------------------------------------------------config
mybook:~# cd /DataVolume/shares/dev/lighttpd

----1. create start/stop script /etc/init.d/lighttpd
mybook:/DataVolume/shares/dev/lighttpd# cp lighttpd-1.4.32/doc/initscripts/rc.lighttpd.redhat /etc/init.d/lighttpd
mybook:/DataVolume/shares/dev/lighttpd# sudo chmod a+rx /etc/init.d/lighttpd

----2. create system config /etc/sysconfig/lighttpd
mybook:/DataVolume/shares/dev/lighttpd# mkdir /etc/lighttpd
mybook:/DataVolume/shares/dev/lighttpd# cp -p lighttpd-1.4.32/doc/initscripts/sysconfig.lighttpd /etc/lighttpd/lighttpd
mybook:/DataVolume/shares/dev/lighttpd# chown root:root /etc/lighttpd/lighttpd
mybook:/DataVolume/shares/dev/lighttpd# more /etc/lighttpd/lighttpd
LIGHTTPD_CONF_PATH=/etc/lighttpd/lighttpd.conf

----3. create real config folder and config files
mybook:/DataVolume/shares/dev/lighttpd# cp -R lighttpd-1.4.32/doc/config/conf.d/ lighttpd-1.4.32/doc/config/*.conf lighttpd-1.4.32/doc/config/vhosts.d/ /etc/lighttpd/

----4. enable at system start , /etc/init.d/lighttpd
! not yet, seems I don’t need

----5. runtime folders and web root
mybook:/DataVolume/shares/dev/lighttpd# mkdir /DataVolume/shares/lighttpdweb
mybook:/DataVolume/shares/dev/lighttpd# cd /DataVolume/shares/lighttpdweb
mybook:/DataVolume/shares/lighttpdweb# mkdir webroot webroot/htdocs home home/sockets cache tmp
root@mybook:~$ mkdir /var/log/lighttpd
root@mybook:~$ chown lighttpd:lighttpd /var/log/lighttpd

----index.html
mybook:/DataVolume/shares/lighttpdweb# vi webroot/htdocs/index.html

My First Heading

My first paragraph.

----6. modify config file to use folders just created.
REF:
/var/log/lighttpd/error.log 
/DataVolume/shares/lighttpdweb/webroot
/DataVolume/shares/lighttpdweb/webroot/htdocs
/var/run/lighttpd.pid 
/DataVolume/shares/lighttpdweb/home
/DataVolume/shares/lighttpdweb/home/sockets
/DataVolume/shares/lighttpdweb/cache
/DataVolume/shares/lighttpdweb/tmp

----6.1. /etc/lighttpd/lighttpd.conf
vi /etc/lighttpd/lighttpd.conf
server.port = 6666
server.use-ipv6 = “disable”
server.bind = “192.168.0.80” <— your ip

server.username = “lighttpd”
server.groupname = “lighttpd”

server.max-fds = 2048 <== max of system support – should be 4096 or 8192, 
server.max-connections = 256 <== max connections org=1024
static-file.exclude-extensions = ( “.php”, “.pl”, “.fcgi”, “.scgi”, “~”, “.inc” )

----6.2. /etc/init.d/lighttpd
vi /etc/init.d/lighttpd

config: /etc/lighttpd/lighttpd 
LIGHTTPD_CONF_PATH=“/etc/lighttpd/lighttpd.conf
pidfile: /var/run/lighttpd.pid
lighttpd=”/usr/local/sbin/lighttpd"

mybook:/etc/lighttpd# mkdir -p /var/lock/subsys
mybook:/etc/lighttpd# touch /var/lock/subsys/lighttpd

----7. create user/group
–add user
mybook:/etc/lighttpd# /usr/sbin/groupadd lighttpd
mybook:/etc/lighttpd# /usr/sbin/adduser --system --no-create-home --disabled-login --disabled-password lighttpd 
–add group, since webroot is on share folder and group
root@mybook:~$ usermod -G lighttpd,share lighttpd
----pid, no need?
mybook:/etc/lighttpd# touch /var/run/lighttpd.pid
mybook:/etc/lighttpd# chown lighttpd:lighttpd /var/run/lighttpd.pid

----8. test without php
mybook:/etc/lighttpd# /usr/local/sbin/lighttpd -t -f /etc/lighttpd/lighttpd.conf
Syntax OK
----start the server for testing
/usr/local/sbin/lighttpd -D -f /etc/lighttpd/lighttpd.conf
----check logs

tail -f /var/log/lighttpd/error.log

tail -f /var/log/lighttpd/access.log

visit OK.: http://192.168.0.80:6666/

============================================================script to start/stop
/etc/init.d/lighttpd [start|stop]

----------------I wraped up this script by myself, start/stop are tested to work fine, just modify and use it.
root@mybook:/DataVolume/shares/lighttpdweb/webroot/htdocs$ more /etc/init.d/lighttpd 
#!/bin/sh

lighttpd Startup script for the lighttpd server

description: Lightning fast webserver with light system requirements

processname: lighttpd

config: /etc/lighttpd/lighttpd.conf

config: /etc/lighttpd/lighttpd

pidfile: /var/run/lighttpd.pid

Note: pidfile is assumed to be created

by lighttpd (config: server.pid-file).

If not, uncomment ‘pidof’ line.

DESC=“Startup script for the lighttpd server”
NAME=“lighttpd”
DAEMON=/usr/local/sbin/lighttpd
CFGFILE=/etc/lighttpd/lighttpd.conf
DAEMON_ARGS=“-f $CFGFILE”
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

RETVAL=0

Source function library

#. /etc/rc.d/init.d/functions

if [-f /etc/lighttpd/lighttpd]; then
. /etc/lighttpd/lighttpd
fi
if [-z “$LIGHTTPD_CONF_PATH”]; then
LIGHTTPD_CONF_PATH=$CFGFILE
fi

Exit if the package is not installed

if [! -x “$DAEMON”]; then
echo “Main program not exists or no execute permission: $DAEMON”
exit 0
fi

Define LSB log_* functions.

Depend on lsb-base (>= 3.0-6) to ensure that this file is present.

. /lib/lsb/init-functions

Function that starts the daemon/service

do_start() {

Return

0 if daemon has been started OK

1 if daemon was already running

2 if daemon could not be started

echo -n $"Starting $NAME: "
#start-stop-daemon --start --exec /my/exec/prog --pidfile /my/pid/file --background
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null || return 1
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON – $DAEMON_ARGS || return 2
#daemon $lighttpd -f $LIGHTTPD_CONF_PATH
RETVAL=$?
[$RETVAL -eq 0] && touch /var/lock/subsys/$NAME
return $RETVAL
}

Function that stops the daemon/service

do_stop() {

Return

0 if daemon has been stopped

1 if daemon was already stopped

2 if daemon could not be stopped

other if a failure occurred

echo -n $“Stopping $NAME: "
#start-stop-daemon --stop --pidfile /my/pid/file
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
#killproc $lighttpd
RETVAL=$?
[”$RETVAL" = 2] && return 2

start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[“$?” = 2] && return 2

[$RETVAL -eq 0] && rm -f /var/lock/subsys/$NAME
rm -f $PIDFILE

return $RETVAL
}

do_reload() {
echo -n $"Reloading $prog: "
#killproc $lighttpd -HUP
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
RETVAL=$?
#echo
return $RETVAL
}

case “$1” in
start)
do_start
case “$?” in
0|1) [“$VERBOSE” != no] && log_end_msg 0 ;;
2) [“$VERBOSE” != no] && log_end_msg 1 ;;
esac
;;
stop)
do_stop
case “$?” in
0|1) [“$VERBOSE” != no] && log_end_msg 0 ;;
2) [“$VERBOSE” != no] && log_end_msg 1 ;;
esac
;;
restart)
do_stop
do_start
;;
condrestart)
if [-f /var/lock/subsys/$prog]; then
stop
start
fi
;;
reload)
reload
;;
status)
#status $DAEMON
echo “not implemented yet. - hanshan”
RETVAL=$?
;;
*)
echo $“Usage: $0 {start|stop|restart|condrestart|reload|status}”
RETVAL=1
esac

exit $RETVAL

============================================================add php-cgi to fastcgi tested OK 

vi /etc/lighttpd/conf.d/fastcgi.conf

server.modules += ( “mod_fastcgi” )
fastcgi.server = ( “.php” =>
( “php-5-procs” =>
(
“socket” => socket_dir + “/php-fastcgi.socket”,
“bin-path” => “/usr/local/bin/php-cgi”,
“bin-environment” => (
“PHP_FCGI_CHILDREN” => “16”,
“PHP_FCGI_MAX_REQUESTS” => “10000”,
),
“max-procs” => 5,
“broken-scriptfilename” => “enable”,
)
),
)

----restart
/etc/init.d/lighttpd stop
/etc/init.d/lighttpd start

----test script
cd /DataVolume/shares/lighttpdweb/webroot/htdocs
root@mybook:/DataVolume/shares/lighttpdweb/webroot/htdocs$ cp /DataVolume/shares/dev/php/testmysql.php ./
root@mybook:/DataVolume/shares/lighttpdweb/webroot/htdocs$ ls
index.html testmysql.php

----TESTED OK
http://192.168.0.80:6666/testmysql.php

============================================================backup system
mybook:~# dd if=/dev/md1 of=/DataVolume/shares/Public/mysql_custom_rootfs.img
dd: opening `/dev/md1’: No such file or directory
mybook:~# dd if=/dev/md0 of=/DataVolume/shares/Public/mysql_custom_rootfs.img

============================================================Now all is done.