Install LightTPD on Mac OSX

Local installation of web development environment on Mac OSX.

Add your user to admin group

Homebrew uses the admin group when setting the ownership of files and folders, so you need to put your user in this group:

$ sudo dseditgroup -o edit -a usernametoadd -t user admin

Install Homebrew

To install Homebre on your Mac, run the command below in a Terminal window:

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

The script explains what it will do and then pauses before it does it.

Once the installation ends, you should check it with the command:

$ brew doctor

Install software with brew

This is the list of packages you need to install with brew:

Repeat the following command for each package in the list, paying attention to the last lines displayed during the installation because they show you the location of configuration files and some other important info:

$ brew install PACKAGE

The installation is made under /usr/local folder.

NOTE: The default brew installation of LightTPD listens on port 8080 to be able to start without root prvileges.

Configure PHP (Optional)

You can skip this step if you don't use any specific PHP configuration.

Edit your php.ini file, located in /usr/local/etc/php/5.5/php.ini and change the following options:

disable_functions = pcntl_alarm, pcntl_fork, pcntl_waitpid, pcntl_wait, pcntl_wifexited, pcntl_wifstopped, pcntl_wifsignaled, pcntl_wexitstatus, pcntl_wtermsig, pcntl_wstopsig, pcntl_signal, pcntl_signal_dispatch, pcntl_get_last_error, pcntl_strerror, pcntl_sigprocmask, pcntl_sigwaitinfo, pcntl_sigtimedwait, pcntl_exec, pcntl_getpriority, pcntl_setpriority, system, shell_exec, phpinfo, show_source, popen, proc_open, fopen_with_path, dbmopen, dbase_open, putenv, rmdir, chmod, rename, filepro, filepro_rowcount, filepro_retrieve, posix_mkfifo

error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED

error_log = /usr/local/var/log/php_errors.log

default_charset = "UTF-8"

date.timezone = America/Mexico_City

Configure MySQL

Edit your my.cnf file located in /usr/local/opt/mysql/. Set default charset to UTF-8. At the end of the mysqld section add the following lines:

character-set-server=utf8
collation-server=utf8_general_ci

Configure LightTPD

IMPORTANT: When installing any additional software with Homebrew, all the installation goes under /usr/local and you need to make sure all configuration paths point to this folder.

First, you need to create a folder for your virtual hosts and at least one folder for the default virtual host:

$ cd
$ mkdir vhosts
$ mkdir vhosts/default
$ mkdir vhosts/default/htdocs

Configuration files

All configuration files are located in /usr/local/etc/lighttpd. This is the content of the main configuration file lighttpd.conf:

var.log_root    = "/usr/local/var/log/lighttpd/"
var.server_root = "/usr/local/var/www/"
var.state_dir   = "/usr/local/var/lighttpd/"
var.home_dir    = "/usr/local/var/lighttpd/"
var.conf_dir    = "/usr/local/etc/lighttpd/"
var.vhosts_dir  = "/Users/your_user/vhosts"
var.cache_dir   = "/usr/local/var/cache/lighttpd"
var.socket_dir  = home_dir + "/sockets"

include "modules.conf"

server.port = 8080
server.use-ipv6 = "disable"
server.username  = "YOUR_USERNAME"
server.groupname = "YOUR_USERGROUP"
server.document-root = server_root
server.pid-file = state_dir + "/lighttpd.pid"
server.errorlog             = log_root + "/error.log"

include "conf.d/access_log.conf"
include "conf.d/debug.conf"

server.event-handler = "select"
server.network-backend = "writev"
server.max-fds = 2048
server.stat-cache-engine = "simple"
server.max-connections = 256

index-file.names += (
  "index.xhtml", "index.html", "index.htm", "default.htm", "index.php"
)
url.access-deny             = ( "~", ".inc" )
$HTTP["url"] =~ "\.pdf$" {
  server.range-requests = "disable"
}
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi", ".scgi" )

include "conf.d/mime.conf"
include "conf.d/dirlisting.conf"

server.follow-symlink = "enable"
server.upload-dirs = ( "/usr/local/var/tmp" )

include_shell "cat vhosts.d/*.conf"

In this file, you must specify the path for your virtual hosts directory in the option var.vhosts_dir.

The next configuration file is modules.conf:

server.modules = (
 "mod_access",
 "mod_auth",
 "mod_evasive",
 "mod_redirect",
 "mod_rewrite",
)
include "conf.d/compress.conf"
include "conf.d/fastcgi.conf"
include "conf.d/simple_vhost.conf"

The next configuration file is for the virtual hosts (conf.d/simple_vhost.conf):

server.modules += ( "mod_simple_vhost" )
simple-vhost.server-root   = vhosts_dir + "/"
simple-vhost.default-host  = "default"
simple-vhost.document-root = "htdocs"

Now, the most important configuration file: conf.d/fastcgi.conf

server.modules += ( "mod_fastcgi" )
fastcgi.server += ( ".php" => 
    ((
        "bin-path" => "/usr/local/bin/php-cgi",
        "socket" => "/usr/local/var/lighttpd/php.socket",
        "max-procs" => 1,
        "bin-environment" => ( 
            "PHP_FCGI_CHILDREN" => "9",
            "PHP_FCGI_MAX_REQUESTS" => "500"
        ),
        "bin-copy-environment" => (
            "PATH", "SHELL", "USER"
        ),
        "broken-scriptfilename" => "enable"
    ))
)

The virtual hosts configuration file: conf.d/simple_vhost.conf

server.modules += ( "mod_simple_vhost" )
simple-vhost.server-root   = vhosts_dir + "/"
simple-vhost.default-host  = "default"
simple-vhost.document-root = "htdocs"

Every virtual host is in a directory below a base directory (~/vhosts) in a path that is the same as the name of the vhost. Below this vhost path must be an extra directory which is the document root of the vhost (htdocs).

The document root for each vhost is built from three values:

The complete document root is constructed either by

server-root + hostname + document-root

or if this path does not exist by

server-root + default-host + document-root

For further information, you can check the official documetation page.

Sample vhost

This is a sample configuration file for a virtual host with document root in /Users/your_user/vhosts/example.local/htdocs:

  $HTTP["host"] == "example.local" {
    # REWRITE
    # if requested file is php, redirect
    $HTTP["url"] =~ "^/[^?]+\.php" {
      url.rewrite-once = ( "^/(.*)" => "/dyna/index.php/$1" )
    }
    # if not php file, redirect ONLY if requested file does not exist
    $HTTP["url"] !~ "^/[^?]+\.php" {
      url.rewrite-if-not-file = ( "^/(.*)" => "/dyna/index.php/$1" )
    }
  }

Also, you need to edit your local hosts file. Add the following line to your /etc/hosts file:

127.0.0.1   example.local

Service control

Now, you need a script to start/stop the service. This sample script is based on the control script from Ubuntu:

#!/bin/bash
DAEMON=/usr/local/bin/lighttpd
NAME=lighttpd
DESC="web server"
PIDFILE=/usr/local/var/lighttpd/lighttpd.pid
SCRIPTNAME=$HOME/bin/lightctl

DAEMON_OPTS="-f /usr/local/etc/lighttpd/lighttpd.conf"

test -x $DAEMON || exit 0

case "$1" in
    start)
      $DAEMON $DAEMON_OPTS
      ;;
    stop)
      killall lighttpd
      ;;
    restart)
      $0 stop
      $0 start
      ;;
    *)
      echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
      exit 1
      ;;
esac

exit 0

You can place this file wherever you want to control the service. An option is to put it in a bin folder under your own home directory. Then, you could call something like:

$ ~/bin/lighttpd.sh start

An alternative to a control script is the usage of aliases on the command line. You can add the following lines to your .bashrc file:

alias lighttpd_start="/usr/local/bin/lighttpd -f /usr/local/etc/lighttpd/lighttpd.conf"
alias lighttpd_stop="killall lighttpd"

Then, you can just run the commands like:

$ lighttpd_start
$ lighttpd_stop

Before starting the service, you need to make sure the following paths exist and belong to you:

References