Symfony flying with Lighttpd on Mac OS X 10.6

This guide aims to Mac OS X 10.6 users who intend to run their Symfony 1.2 projects on Lighttpd with PHP 5.3 and MySQL5. Steps described in this article refer to a development machine setup and do not consider any security related configurations necessary for a production setup.

Why?

Why would I want to use Symfony with Lighttpd (Lighty) instead of Apache?

  1. It is light – as the name tells already.

  2. It is fast – believe it or not. Mark Round has an interesting benchmark on the actual performance differences here:

Who?

This guide assumes that you have basic knowledge of the bash, Web Server configuration, *NIX Filesystem Permissions, MySQL, PHP, who sudo is, etc.
If more than one of the above does not apply to you, stop here.

Preparation

Backup

Of course your system is fully backed up and you are in the position to take full responsibility for the following steps.
Ideally you had less than 2 beers – etc.

Time

Depending on your machine this procedure takes something between 2-3 hours.
It is not only the configuration that takes time – but as we will be using the MacPorts System to get the software necessary,
compile time is a major factor for this.

Tools

A Mac with OS X 10.6.1 based computer
(It might apply to other OS X Versions which I have not tested so far.)

A working – ideally fast Internet connection

Your favorite Editor

I will be using TextMate “mate” in the following.
I recommend it to everyone who’s old love is about to, or if you are honest to yourself – has already expired.
While we talk about it, there is a TextMate bundle for lighttpd which can be obtained here (http://svn.textmate.org/trunk/Bundles/)
(Yes, he’s a fanboy)
In case you prefer VIM, EMACS or whatever editor you shall be easily able to replace all the mate commands with that.

Apple Developer Tools

The current Apple XCode Developer Tools installed (Version 3.2 at the time of writing).
You can obtain them here: http://developer.apple.com/

A snack & cup of coffee or tee

Assumptions

You have clean OS X 10.6 installation with no Web Server active.
To ensure this please go to System Preferences -> Sharing on your Mac and make sure that Web Sharing is NOT ticked.
(This appears to be enabled by default on a fresh 10.6 Installation!)

You are NOT running your shell as a super user. If you ARE running the shell as a super user – even better!
You will save yourself loads of sudo statements at the beginning of each line in the following.

Setting the Foundation

As said we will be using MacPorts to get the required software.
The one or the other might be raising there hand saying that not all packages we will be using are at latest
in the MacPorts tree. Yes, you are right. Still it makes life simple – so I will stick to it.
What versions are we dealing with? @16th of September 2009

  • MacPorts 1.8.0
  • Lighttpd 1.4.2.2
  • PHP 5.3
  • MySQL 5.0.85
  • Symfony 1.2.8

Open you favorite Web Browser and grab lets grab the latest version of MacPorts from http://www.macports.org/
Follow the instructions on screen in order to complete the MacPorts installation.

Installing MySQL

Bring up the Terminal and make sure that your macports installation is up-to-date:

sudo port selfupgrade

Grab MySQL from ports

sudo port install mysql5-server

Configure MySQL and tell it to use the mysql user.

sudo mysql_install_db5 -u mysql

Start mysql for the first time:

sudo /opt/local/lib/mysql5/bin/mysqld_safe &

Do a little basic security for your MySQL install:

sudo /opt/local/lib/mysql5/bin/mysql_secure_installation

Add MySQL to the OS X autostart.

sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist

Installing Lighttpd

sudo port install lighttpd

lets copy the default configuration, as it fits our needs with regard to amendments we need to make later on:

sudo /opt/local/etc/lighttpd/lighttpd.conf.default /opt/local/etc/lighttpd/lighttpd.conf

Lighty will require a few files to be accessible. The default configuration file tells Lighty to use www user and group.
(Those are the default OS X Web Server Accounts – you do not need to create those.)

Let’s make sure that this is the case.

Logfile Settings

sudo mkdir /opt/local/var/log/lighttpd/
sudo touch /opt/local/var/log/lighttpd/access.log
sudo touch /opt/local/var/log/lighttpd/error.log
sudo chmod 755 /opt/local/var/log/lighttpd/*.log
sudo chown -R www:www /opt/local/var/log/lighttpd/

Process Stuff

sudo mkdir /opt/local/var/run/lighttpd/
sudo touch /opt/local/var/run/lighttpd.pid
sudo chown www:www /opt/local/var/run/lighttpd.pid
sudo chown -R www:www /opt/local/var/run/lighttpd/

Let’s place a dummy index page into the document root:
sudo mate /opt/local/www/htdocs/index.html

…and put something like this into it:

<html>
<body>
this is lighty on os x
</body>
</html>

The default document root of your is something we want to have accessible to Lighty:

sudo chown -R www:www /opt/local/www/htdocs/

Check if Lighty is working:

sudo /opt/local/etc/LaunchDaemons/org.macports.lighttpd/lighttpd.wrapper start

(You might want to define an alias in your ~/.profile so you do not need to type the whole path all the time.)

It starts! Good. Send your Web Browser to localhost, you should see our dummy page.
It does not? That is the point in time where you might want to have look into the error logfile:

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

Afterwards ensure to add Lighty to the autostart with this command:

sudo launchtl load -w /Library/LaunchDaemons/org.macports.lighttpd.plist

Installing PHP

Lighty does not have “native” PHP support. It has something even better,
it supports FastCGI http://www.fastcgi.com/ which lets you do a lot of great things – which I tend to explain. (3.2.1. NOT!)

So let’s tell MacPorts to grab PHP5 for us.
(You might want to add additional PHP5 packages here. For details please refer to the MacPorts directory http://www.macports.org/ports.php)

sudo port install php5 +fastcgi +php5-mysql +php5-xsl +pear +php5-gd +php5-mbstring

This is the point where you would go for your snack… zzZZzzZz

So everything munged and your shell prompt is expecting you to throw crazy commands at it?
Great!

Let’s get hold of a php.ini first

 sudo cp /opt/local/etc/php5/php.ini-development /opt/local/etc/php5/php.ini

Make sure to define the timezone to prevent annoying messages later on:
Somewhere around line 995, there is a lonely

;date.timezone = 

Turn it into something like this. (according to your local timezone, of course)

date.timezone = Europe/Berlin 

Also have look into line 846 and uncomment the following parameter:

cgi.fix_pathinfo=1

(Knowing this is the default setting – I want you to be able to say yes in case someone asks…)

Configuring Lighty for PHP

At this point we should take some time to go through our lighttpd.conf.

Enable FastCGI by uncommenting the following line below server.modules by removing the #

        "mod_fascgi", 

Next thing is to make Lighty aware of these .php files.
Scroll down to something around 230. There is a big block commented out. Modify it, so it looks like this one:

fastcgi.server             = ( “.php” =>
( “localhost” =>
(
“socket” => “/opt/local/var/run/lighttpd/php-fastcgi.socket”,
“bin-path” => “/opt/local/bin/php-cgi”
)
)
)

Tell Lighty to restart:

sudo /opt/local/etc/LaunchDaemons/org.macports.lighttpd/lighttpd.wrapper restart

No error messages on the shell? Good – time to proceed.
Let’s see if Lighty is able to run some PHP.

sudo mate /opt/local/www/htdocs/info.php
<?php
phpinfo()
?>

Point your Web Browser to http://localhost/info.php
You should see the PHP Info page.

Symfony specifics

There are a few things we need to take into consideration now…

Rewriting & Virtual Hosts

As Symfony makes use of Apache’s mod_rewrite – we need to use the Lighttpd equivalent.
I did not find something like the “definitiv guide” – which btw was the reason to document my procedure.
Much more you can find a few different snippets within the first 3 results when you googling.
Also I am confident that the Virtual Server question mark might has passed you head already?!

So let’s have another hand on our Lighty config and enable some more modules.

sudo mate /opt/local/etc/lighttpd.conf

server.modules              = (
“mod_rewrite”,
“mod_alias”,

#define a new host which is reachable at the symfony.localhost domain

$HTTP [“host”] == “symfony.localhost” {

#point this domain to the symfony web directory
server.document-root = “/opt/local/www/htdocs/sftest/web/”

#define the sf folder alias – as I pull Symfony from pear it is here:
alias.url = (
“/sf/” => “/opt/local/lib/php/data/symfony/web/sf/”
)

#rewrite rules for symfony
url.rewrite-once = (
“^/([a-z_]).php(/[^?]+)?(?.*)?” => “/$1.php$2$3”,
“^/(.*..+(?!html))$” => “$0”,
“^/(.*).(.*)”        => “$0”,
“^/([^.]+)$”          => “/index.php/$1”,
“^/$”        => “/index.php”
)

}

Tell Lighty to restart:

sudo /opt/local/etc/LaunchDaemons/org.macports.lighttpd/lighttpd.wrapper restart

Project Setup

Which Symfony?

Depending on your development practice you would pull Symfony out of your software configuration management (e.g. subversion)
In this example I will pull Symfony through PEAR (http://pear.php.net/)

Pull Symfony through PEAR

sudo pear channel-discover pear.symfony-project.com
sudo pear install symfony/symfony-1.2.8

Note: At the time of writing  – the latest stable version of Symfony is 1.2.8.

For some reason symfony/symfony still points to 1.0.2.1 – so please adapt as necessary.
Question to the readers: Do I need to pull Phing separately?

Hostfile configuration
remember to add the virtual server in your hosts file

sudo mate /etc/hosts
127.0.0.1    localhost
127.0.0.1    symfony.localhost    symfony

Ready for some Error Messages?

Basically we are done and you should now be able to generate your symfony project an obtain some error messages as you have not configured your database so far. Once you managed that – point your browser to symfony.localhost

One Reply to “Symfony flying with Lighttpd on Mac OS X 10.6”

Comments are closed.