Thursday, October 13, 2011

Notes on Installing Lighty (lightTPD) + PHP on Mac OS X

Although, in general, a relatively easy process when done as described here, it still wasn't w/o issues for me. The two main articles I based the install off of are as follows:

1. Install PHP5, lighttpd and Imagick on Mac OS X Leopard
2. Symfony flying with Lighttpd on Mac OS X 10.6

In my case I was using Snow Leopard (10.6) and didn't care about Imagick or Symfony so simply ignored those parts. My system is also already a running MAMP (Mac+Apache+Mysql+Php) so in the end I configure Lighty to run on port 81 and, for the sake of this blog, I'm not bothered with installing MySQL because I already have it. If you need to install MySQL, however, there's now a self-installing package for Mac OS X so it should be a breeze.

I found the following useful for if things go wrong:

- Check if lighttpd was loaded AND enabled using the terminal command "sudo launchctl list".
- Open up the ever-handy Console utility (Applications>Utilities>Console) and view the Console Message or system.log. If Lighty is trying to start but failing it should show a message about every 10 seconds explaining why it failed.

INSTALLING LIGHTY

In my case, the main reason the default installation didn't work is because of the following default Lighty configuration:

server.network-backend = "linux-sendfile"

Which results in the following error: "server.network-backend has a unknown value: linux-sendfile"

Not sure what the 'best' value should be on a Mac, but simply changing this to:

server.network-backend = "writev"

worked for me.

I also tripped on the following, so watch out! The modules.conf file says "at least mod_access and mod_accesslog should be loaded" and then the server.modules variable shows only "mod_access" by default. So I added "mod_accesslog" but this only stopped Lighty from loading. So don't do this! Later realized that the accesslog module is already included in the master lighttpd.conf file.

Also, you do NOT need to add modules to the server.modules variable if there's already a predefined .conf file in the conf.d directory. Simply un-comment the line in modules.conf that includes the appropriate file. This is because the include file does this for you.

INSTALLING PHP

Again, mostly just followed the instructions. Below are things that varied for my install or selections I made when instructions differed.

1. The install command in the Imagick instructions is out-dated and will not install MySQL. The command in the 'Symfony' instructions look more up-to-date but still didn't work for me. I had to instal several extensions separately. The commands I used were

"sudo port install php5 +fastcgi +pear"
"sudo port install php5-mysql"
"sudo port install php5-xsl"
"sudo port install php5-gd"
"sudo port install php5-mbstring"
"sudo port install php5-iconv"
"sudo port install php5-curl"
"sudo port install php5-openssl"
"sudo port install php5-soap"

Installation of php5-mysql output the message "To use mysqlnd with a local MySQL server, edit /opt/local/etc/php5/php.ini and set mysql.default_socket, mysqli.default_socket and pdo_mysql.default_socket to /opt/local/var/run/mysql5/mysqld.sock" but I just ignored this (tried doing it, as well as making the appropriate directory with group permission for mysql user, but then Lighty connections to MySQL started failing).

2. Config files were located at /opt/local/etc/php5 and I copied from the dev default file since that's what I'll be using Lighty for.

3. Didn't bother with changing the extension_dir variable.

4. My install came with a hierarchical .conf file structure so enabling PHP involved multiple .conf files as opposed to a single lighttpd.conf file. This involved...
- un-comment the line "include "conf.d/fastcgi.conf" in modules.conf
- adding the following to the end of the fastcgi.conf file:

fastcgi.server = ( ".php" =>
( "localhost" =>
(
"socket" => socket_dir + "/php-fastcgi.socket",
"bin-path" => "/opt/local/bin/php-cgi"
)
)
)

- Note that the socket directory uses a pre-defined variable, unlike the fixed
paths given in the referenced instructions.

5. Setup ownerships and permission mostly according to the 'Symfony' instructions, but my paths were based on variables defined in lighttpd.conf. For example, the pid file path was
server.pid-file = state_dir + "/lighttpd.pid",
which was equivalent to
"/opt/local/var/run/lighttpd/lighttpd.pid".
As a result, the folders and location of files I needed to give www ownership to varied from those in the 'Symfony' instructions.

6. Note that my install had no lighttpd.wrapper file, so they start/restart command were not applicable. I had to start/restart using load/unload with the launchctl command. For example:
"sudo launchctl load /opt/local/etc/LaunchDaemons/org.macports.lighttpd/org.macports.lighttpd.plist"

ADDING VHOSTS
For this I used the simple_vhost module.

1. Un-comment the following line in modules.conf

include "conf.d/simple_vhost.conf"

2. Edit simple_vhosts.conf accordingly. For example,

simple-vhost.server-root = vhosts_dir + "/"
simple-vhost.default-host = "lighty.anotherlocalhost"
simple-vhost.document-root = "/anotherlocalhost/"

In this example above, the path

vhosts_dir + "/lighty.anotherlocalhost/anotherlocalhost/"

MUST exist. That is, the path to the doc-root includes a directory named after the default-host variable.

3. Add an entry for lighty.anotherlocalhost into you hosts file (/etc/hosts) and clear the cache for this (terminal command: "dscacheutil -flushcache").

No comments:

Post a Comment