Lighttpd is a lightweight (lighter than Apache) http server. It has been gaining more an more press lately (The Ruby crowd uses it often for Rails).
You can run PHP on lighttpd as a CGI (Fast CGI prefferably). You can even keep your existing Apache/PHP install and run Lighttpd/PHP side by side (on another port)
Step 1: Install lighttpd.
By far the easiest way to do this on a OSX is with darwin ports (easy to install, easy to upgrade). In a terminal just issue:
$ sudo port install lighttpd
It will most likley install a few prerecusits and then lighttpd. If you are not a dp fan, compiling and installing lighttpd is not dificult either, see their docs for that.
Step 2: Install a Fast CGI enabled PHP5.
Download the latest PHP5 tarball (at the time of this writing that was 5.2.0)
cd to the unpacked php-5.2.0 directory and run your configure command. In my case I used…
./configure --prefix=/usr/local/php-fcgi
--enable-fastcgi
--with-mysqli=/usr/local/mysql/bin/mysql_config
--with-mysql=/usr/local/mysql
--with-curl=/usr/include
--with-xsl
--with-gd
--with-png-dir=/opt/local
--with-jpeg-dir=/opt/local
--enable-sockets
--with-gettext=/opt/local
--with-zlib-dir=/opt/local
--with-config-file-path=/etc
--with-xml-rpc
--enable-mbstring
--with-iconv
--enable-memory-limit
Two things are important here.
1. –prefix=/usr/local/php-fcgi : This needs to be different than your existing php install. If not you will overwrite/corrupt that existing install.
2. –enable-fastcgi : Self explanitory
As for the other config flag, just use what you need, this example is just what I use.
When configure completes run make then sudo make install
You will end up with a php install at what was used for ‘–prefix=…’ (/usr/local/php-fcgi in my case)
Take PHP’s example php.ini file ({php-source-dir}/php.ini-recommended) and move it to /etc/php-fast-cgi.ini (in my case since I used –with-config-file-path=/etc). Naming the file php-fast-cgi.ini rather than php.ini caused this cgi version of PHP to use that ini file rather than say another php.ini file there that Apache is using. This lets you run Apache mod PHP and cgi PHP on the same box and keep the ini files centaly located.
Step 3: Configure Lighttpd.
Lighttpd has aconfig file (similar to httpd.conf) and after a darwin ports install it can be found at /opt/local/etc/lighttpd/lighttpd.conf.default. First make a copy of that file
$ sudo cp /opt/local/etc/lighttpd/lighttpd.conf.default /opt/local/etc/lighttpd/lighttpd.conf
Now with you favorite editor, make these changes to lighttpd.conf.
within server.modules, uncomment (delete the ‘#’) for “mod_fastcgi”,
set the document-root to something appropriate, I used…
server.document-root = "/Users/sam/Sites/lighttpd/root/"
set the erorlog to something appropriate, I used…
server.errorlog = "/Users/sam/Sites/lighttpd/logs/lighttpd.error.log"
for OS X uncomment server.event-handler…
## set the event-handler (read the performance section in the manual)
server.event-handler = "freebsd-kqueue" # needed on OS X
Set the accesslog to something appropriate…
accesslog.filename = "/Users/sam/Sites/lighttpd/logs/access.log"
Set the server.port (I used 8181, you can use any available port)…
server.port = 8181
Set up the fast-cgi section. This is the minimum needed, see the lighttpd docs for more info.
fastcgi.server = ( ".php" => ((
"bin-path" => "/usr/local/php-fcgi/bin/php",
"socket" => "/tmp/phpfcgi.socket"
)))
Set up the user and group for lighttpd to run as. I chose www (should exist on OSX, you can use any ‘unprivileged’ user)
server.username = "www"
server.groupname = "www"
Okay, save that file and quickly create the directories we refered to in the config file (if they do not exist) and change there ownership to the server.username:server.groupname you chose above (www:www in my case)
$ mkdir -p /Users/sam/Sites/lighttpd/root /Users/sam/Sites/lighttpd/logs
$ sudo chgown -R www:www /Users/sam/Sites/lighttpd/logs
Now we are ready to test our config file with this command (-t signifies test and -f tells lighttpd which config file to use)
$ /opt/local/sbin/lighttpd -t -f /opt/local/etc/lighttpd/lighttpd.conf
If all is well, you should see “Syntax OK”. If not, google your error or look at the docs for lighttpd
Step 4: Start lighttpd.
From the terminal issue
$ sudo /opt/local/sbin/lighttpd -f /opt/local/etc/lighttpd/lighttpd.conf
Now place a test php file in your lighttpd web-root (/Users/sam/Sites/lighttpd/root/index.php in my case) and put these lines in it.
Save the file and then open abrowser and go to http://localhost:8181/index.php and you should see something like
php-fcgi.png
Ths important thing here is that you go to localhost:8181 (not localhost or localhost:80). The nice thing is, if you already had PHP/Apache installed, you can still use that on whatever local URL you had setup (i.e. http://localhost)
So welcome to lighttpd. There is a vast amount on lighttpd that was not covered here, so be sure to check out the lighttpd site for more details.
Friday, February 16, 2007
Subscribe to:
Post Comments (Atom)

0 comments:
Post a Comment