How to install Lighttpd, PHP 5, and MySQL 5.1 on FreeBSD 7.2
Installing MySQL
Before we start, make sure your ports are updated.
portsnap fetch
portsnap update
Right. Now we're ready.
cd /usr/ports/databases/mysql51-server
make install clean
This might take a while. Do something productive.
When that finishes we'll install the database, and make sure user and group mysql is the owner of the database folder.
/usr/local/bin/mysql_install_db
chown -R mysql /var/db/mysql/
chgrp -R mysql /var/db/mysql/
Next, we'll set MySQL to start at FreeBSD boot.
echo 'mysql_enable="YES"' >> /etc/rc.conf
Finally, we'll start MySQL and change the root password to keep out the hackers.
/usr/local/etc/rc.d/mysql-server start
/usr/local/bin/mysqladmin -u root password newpassword
If you did everything right, MySQL should now be running.
Installing Lighttpd
Assuming our ports are still up to date after that MySQL compile, we'll start the Lighttpd install.
cd /usr/ports/www/lighttpd
make install clean
When that's finished, we'll create the document root folder.
mkdir -p /usr/local/www/data
Now, create the access and error log files. Those files are:
/var/log/lighttpd.access.log
/var/log/lighttpd.error.log
Next, we'll give Lighttpd ownership of it's log files.
chown www:www /var/log/lighttpd.access.log
chown www:www /var/log/lighttpd.error.log
Now it's time to open up the Lighttpd configuration file. Great.
vi /usr/local/etc/lighttpd.conf
Uncomment the line "mod_fastcgi", in "server.modules".
Append the following code in the fastcgi module section.
fastcgi.server = ( ".php" =>
( "localhost" =>
(
"socket" => "/tmp/php-fastcgi.socket",
"bin-path" => "/usr/local/bin/php-cgi"
)
)
)
Finally, we'll set Lighttpd to start with FreeBSD.
echo 'lighttpd_enable="YES"' >> /etc/rc.conf
Before we start Lighttpd, Lets install PHP.
Installing PHP
Installing PHP is very easy. Ready? Ok.
cd /usr/ports/lang/php5
make install clean
Now we'll install some PHP extensions. Make sure you check the mysql and or mysqli boxes to be able to use MySQL in PHP.
cd /usr/ports/lang/php5-extensions
make config install clean
Next, copy php.ini-recommended to php.ini.
cp /usr/local/etc/php.ini-recommended /usr/local/etc/php.ini
With PHP installed, we can now start Lighttpd.
/usr/local/etc/rc.d/lighttpd start
It should work now, unless I missed a few steps. Create a phpinfo script in /usr/local/www/data and load it in your browser to test!

July 2nd, 2009 - 09:27
Thanks, It’s the only tutorial which really worked for me with the Lighttpd server start. I tried three times before with other tutorials and Lighttpd would not start.
I have to figure out how to get .php files not to display “403 – Forbidden”, html work well though. Thanks again. I strongly encourage you to write more FreeBSD tutorials.
July 10th, 2009 - 08:38
Great tutorial! I followed it to the teeth and it all installed perfectly. I’m beginner who recently ‘turned to the light’ and is loving freeBSD; problem is I’ve never set up a web server. Could you perhaps write another tutorial, a continuation from this one, with configuring and setting up lighttpd to host a website? Thanks!