Migrating from Apache2 to Lighttpd: Part 1 - Installing FastCGI, PHP5, Zend, eaccelerator
Okay guys,
Before migrating to Lighttpd,
we need to recompile PHP to support FastCGI.
Folow the guide step by step.
It won’t take five minutes.
Promise 
In case you messed up somewhere.
DON’T WORRY, it’s just one email / comment away
Step 1)
Prepare location for installer
mkdir -p /root/INSTALL cd /root/INSTALL
Step 2)
Install FastCGI
wget http://www.fastcgi.com/dist/fcgi.tar.gz tar -zxvf fcgi-2.4.0.tar.gz cd fcgi-2.4.0 ./configure make make install cd ..
Step 3)
Rebuild PHP to support FastCGI
wget http://files.directadmin.com/services/customapache/php-5.2.9.tar.gz wget http://files.directadmin.com/services/customapache/php-5.2.9-mail-header.patch tar -zxvf php-5.2.9.tar.gz patch -p0 < php-5.2.9-mail-header.patch cd php-5.2.9 ./configure --enable-soap --with-libxml-dir=/usr/include/libxml2 \ --with-curl=/usr/local/lib --with-gd \ --enable-gd-native-ttf --with-ttf --with-gettext \ --with-jpeg-dir=/usr/local/lib \ --with-freetype-dir=/usr/local/lib --with-kerberos \ --with-openssl --with-mcrypt --with-mhash \ --with-mysql=/usr/bin/mysql --with-mysqli=/usr/bin/mysql_config \ --with-pdo-mysql=/usr/bin/mysql \ --with-pear --with-png-dir=/usr/local/lib \ --with-zlib --with-zlib-dir=/usr/local/lib --enable-zip --with-iconv=/usr/local \ --enable-bcmath --enable-calendar --enable-ftp --enable-magic-quotes --enable-sockets \ --enable-mbstring --with-curlwrappers --enable-shared --enable-static \ --enable-fastcgi --enable-force-cgi-redirect make make install cd ..
Step 4)
Install Zend Optimizer
wget http://files.directadmin.com/services/customapache/ZendOptimizer-3.3.3-linux-glibc23-i386.tar.gz tar -zxvf ZendOptimizer-3.3.3-linux-glibc23-i386.tar.gz cd ZendOptimizer-3.3.3-linux-glibc23-i386 ./install.sh ENTER ENTER ENTER ENTER ENTER ENTER ENTER ENTER ENTER ENTER ENTER ENTER ENTER ENTER cd ..
Step 5)
Install eaccelerator
wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2 tar -xjvf eaccelerator-0.9.5.3.tar.bz2 cd eaccelerator-0.9.5.3 make clean export PHP_PREFIX="/usr/local" $PHP_PREFIX/bin/phpize ./configure \ --enable-eaccelerator=shared \ --with-php-config=$PHP_PREFIX/bin/php-config make clean make make install cd ..
Remember the location….
| [root@server eaccelerator-0.9.5.3]# make install
Installing shared extensions: /usr/local/lib/php/extensions/no-debug-non-zts-20060613/ |
If you forgot the location of your eaccelerator,
find it using this command:
locate eaccelerator.so
Find the location of your php.ini
php -r "phpinfo();" | grep php.ini Configuration File (php.ini) Path => /usr/local/lib Loaded Configuration File => /usr/local/Zend/etc/php.ini
Now, edit php.ini
vi /usr/local/Zend/etc/php.ini
and add / modify this line…
| [Zend] zend_extension=”/usr/local/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so” zend_extension_manager.optimizer=/usr/local/Zend/lib/Optimizer-3.3.3 zend_extension_manager.optimizer_ts=/usr/local/Zend/lib/Optimizer_TS-3.3.3 zend_optimizer.version=3.3.3 zend_extension=/usr/local/Zend/lib/ZendExtensionManager.so zend_extension_ts=/usr/local/Zend/lib/ZendExtensionManager_TS.so eaccelerator.shm_size=”16″ eaccelerator.cache_dir=”/tmp/eaccelerator“ eaccelerator.enable=”1″ eaccelerator.optimizer=”1″ eaccelerator.check_mtime=”1″ eaccelerator.debug=”0″ eaccelerator.filter=”" eaccelerator.shm_max=”0″ eaccelerator.shm_ttl=”0″ eaccelerator.shm_prune_period=”0″ eaccelerator.shm_only=”0″ eaccelerator.compress=”1″ eaccelerator.compress_level=”9″ |
Create cache directory for eaccelerator
mkdir /tmp/eaccelerator chmod 0777 /tmp/eaccelerator
Step 6)
Check your php version
Make sure all work fine before we restart apache.
DO NOT restart apache before passing this step.
eaccelerator and Zend must be loaded properly…
| [root@server eaccelerator-0.9.5.3]# php -v PHP 5.2.9 (cli) (built: Mar 23 2009 12:19:57) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies with eAccelerator v0.9.5.3, Copyright (c) 2004-2006 eAccelerator, by eAccelerator with Zend Extension Manager v1.2.2, Copyright (c) 2003-2007, by Zend Technologies with Zend Optimizer v3.3.3, Copyright (c) 1998-2007, by Zend Technologies |
php-cgi was compiled as fastcgi —> cgi-fcgi
| [root@server eaccelerator-0.9.5.3]# php-cgi -v PHP 5.2.9 (cgi-fcgi) (built: Mar 23 2009 12:19:24) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies with eAccelerator v0.9.5.3, Copyright (c) 2004-2006 eAccelerator, by eAccelerator with Zend Extension Manager v1.2.2, Copyright (c) 2003-2007, by Zend Technologies with Zend Optimizer v3.3.3, Copyright (c) 1998-2007, by Zend Technologies |
Step 7)
Restart apache
service httpd restart
Step 8 9 10 ….
Troubleshooting 
and take a break.
Next … installing Lighttpd
Lighttpd vs Nginx (Apache Benchmark)
Continuing previous post Lighttp vs Apache2
Now, i’ve done another test Lighttpd vs Nginx using Apache Benchmark
Benchmark parameter
ab -c 300 -n 1000 …
.
.
.
.
.
Nginx + spawn-fcgi
14:31:34 up 9 days, 21:39, 2 users, load average: 18.47, 7.77, 5.68
Server Software: nginx/0.6.35
Document Path: /
Document Length: 22355 bytesConcurrency Level: 300
Time taken for tests: 45.095 seconds
Complete requests: 1000
Failed requests: 120
(Connect: 0, Receive: 0, Length: 120, Exceptions: 0)
Write errors: 0
Non-2xx responses: 119
Total transferred: 19941669 bytes
HTML transferred: 19740333 bytes
Requests per second: 22.18 [#/sec] (mean)
Time per request: 13528.485 [ms] (mean)
Time per request: 45.095 [ms] (mean, across all concurrent requests)
Transfer rate: 431.85 [Kbytes/sec] received
Lighttpd + spawn-fcgi
14:47:53 up 9 days, 21:55, 2 users, load average: 21.21, 6.73, 4.57
Server Software: lighttpd/1.4.22
Document Path: /
Document Length: 22388 bytesConcurrency Level: 300
Time taken for tests: 42.815 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 22599690 bytes
HTML transferred: 22388000 bytes
Requests per second: 23.36 [#/sec] (mean)
Time per request: 12844.583 [ms] (mean)
Time per request: 42.815 [ms] (mean, across all concurrent requests)
Transfer rate: 515.47 [Kbytes/sec] received
Conclusion: Lighttpd vs Nginx (Apache Benchmark)
Lighttpd gave me less error and slightly faster.
Nginx consume less resource, with slightly produce more error.
Setting low amount of worker thread will greatly reduce resource usage,
but couldn’t serve high concurrency.
Recommendation:
Use Nginx for Desktop based server,and Lighttpd for Normal server.- Throw away ApacheĀ :p
Edit:
I found the culprit that cause many errors on nginx was actually the spawn-fcgi parameters.
Nginx itself works smoothly beyond expectation.I remark the conclusion. Both are great Apache2 replacement.
But, so far…
I coudn’t find a good numbers of childs to spawn and fork.
With nginx, it either crash my server (too high) or many failed requests (too low).
end of file … Lighttpd vs Nginx (Apache Benchmark)
Lighttpd+FastCgi+Php-Cli vs Apache2+mod_php
I heard a lot of good things about lighttpd and fastcgi.
People keep claiming that Lighttpd are faster than Apache2.
So here i want to prove it by myself.
If you’ve been on internet for more than 10 years,
you’ll understand that there’s a bunch of bullshit lying around.
I’m going to believe what i heard, only after i see it.
Took almost 2 days to setup the environment.
On my server, the monstrous AMD Phenom 9650 Quad Core with 4GB RAM,
coated with the best operating system in the world CentOS 5.2
(Didn’t i say there’s a bunch of bullshit on internet? You just heard one)
Okay, all set.
I install both:
Apache2 + mod_php, and Lighttpd/1.4.22 + FastCgi + Php-FCLI
Let me know if you need tutorial to put up these, i’ll post a guide by request.
Say the first configuration of Apache/2, i will name him [K]
and the second one is [L] which stands for Lighttpd.
Both [K] and [L] are on the same machine,
served on different static IP,
which you can purchase for $0.5-$3 per IP …
if you have a dedicated server.
Okay, let’s cut the crap.
I use ApacheBench, Version 2.3 as the judge.
Isn’t [L] one brave challenger here.
The situation doesn’t favor him.
I wish i can have LighttpdBench to keep the fight fair.
Oh, well i said stop the crap. Let’s go….
First round!!!!!

Concurrency Level: 200
Complete requests: 1000
The result is..
Both has zero failed requests
[L] Requests per second: 67.22 [#/sec] (mean)
[K] Requests per second: 47.01 [#/sec] (mean)
WINNER: [L], FAST!
Clearly shown that [L] is 43% faster than [K]
Move to the second round!!!

Concurrency Level: 400
Complete requests: 2000
[L] Failed requests: 22
[K] Failed requests: 1268
[L] Requests per second: 64.62 [#/sec] (mean)
[K] Requests per second: 87.53 [#/sec] (mean)
WINNER: [L]. HARD, STRONG, AND ROBUST.
At high concurrency level [L] has 0.0017339218158890290037831021437579% less failure than than [K]
FINAL ROUND!!!

Concurrency Level: 100
Complete requests: 2000
[L]
Failed requests: 0
Requests per second: 65.47 [#/sec] (mean)
[K]
Failed requests: 7
Requests per second: 62.67 [#/sec] (mean)
WINNER: [L].
Given normal condition both perform almost equally,
but [L] is has less failure than [K]
Conclusion Lighttpd+FastCgi+Php-Cli vs Apache2+mod_php
[L] sitting in the corner with satisfied looks on his … her face.
Based on performance [L] beats [K] to dust
and send him fly thousand kilometer to the blue sky.
If performance is #1 int your life, then you must choose [L].

But remember, don’t overwhelmed with the benchmark result,
and these lovely pictures.
Benchmarking and tweaking is for poor people.
It took a lot of time just to improve the tiny bit of performance.
Take this into consideration.
Right now the majority are using Apache,
which means bunch of people swimming in this pool.
You might want to sacrifice performance,
spend less time on installation and tweaking,
and spend more money on hardware.
In the end, outcome is everything.
Means not only you have to cut hardware cost,
but also time for soft-optimization.
Later~
Use OpenDNS. Period!
Yesterday after changing the DNS for several domain, i experienced a huge delay.
It doesn’t change on my side for more than 24 hours.
I’m getting mad and called the hosting server.
The technician on the edge of their temper i believe :p,
explains to me that it’s been propagated perfectly.
I said no no no! There’s no change on my said.
Later i found out that the problem is on DNS Server of my ISP.
The records were cached there, and it took 2-3 days until refreshed.
I don’t like wasting every second just to wait
for my domain to work properly.
Quick search on this issue.
Browsed several pages of google result.
Got me landing on the OpenDNS Website.
Do a small task setting my DNS Server.

And refresh the cache from http://cache.opendns.com/
Works like a charm
First month result from SSEC … $108.91
Just a quick update.
Last month i build couple thousand of pages from around a hundred of affiliate.
So far only 4 sponsors convert which gave me … *tada*
a whooping $47.3 from 1st afiliate network and $61.61 from second one.
Still not enough to cover the cost
Trying a couple of new techniques now.
Wish me luck!
Simplified search engine cloaker, the journey begins
Despite of negative talks i read over BHW. I made up my mind to join SSEC, Simplified search engine cloaker. Actually fantomaster is my first choice, but my budget won’t allow. I thought it’s good to start with this one first.
I join without even knowing what cloaking is. I read the forum over and over.
First thing came up on my mind. This membership do over-price. I should’ve heard BHW ppl there.
Second opinion, this is nothing new to me. I’ve done it since years ago. I did it without even knowing what search engine cloaking is. My Gosh, i’m a genius! I just realized it.
Third opinion after hanging out for couple weeks. Brian, the owner, is pretty nice guy. I got impression though, people who start doing this since two three years ago … those who has hundreds or thousand posts on forum, is already on top of food pyramid. Che!
Fourth, the forum is pretty quiet. I can hear the echo. Maybe only around 50 active members over there. But wait!!! The forum is very active for a small community. Lot’s a cool guys there.
Fifth, I would keep my membership for couple months. Expect some updates from me. Simplified search engine cloaker, here we go!
Subdomain 301 Redirection using .htaccess
I'm planning to post a lot more script, snipplets, whatever you called it. Mainly covering minisites, scraped pages, cycled pages, etc. Used properly, each of my post will help you making money. No guarantee. Period
So i pick a better name for my blog, and figure out that i need to redirect it to the new one. Here's the code i save it as .htaccess and put it on public_html/php folder:
-
RewriteEngine on
-
RewriteCond %{HTTP_HOST} ^php.fallenray.com$ [OR]
-
RewriteCond %{HTTP_HOST} ^www.php.fallenray.com$ [NC]
-
RewriteRule ^(.*)$ http://rainbowhatseo.fallenray.com/$1 [R=301,L]
My hosting use cPanel. It has the function to do this, but i simply doesn't satisfied with the result.
Above code will permanently (301) redirect from php.fallenray.com to rainbowhatseo.fallenray.com
Kohana and Zend, Killer Combo! The best PHP5 framework and library in one package.
My current favorite php framework is kohana. After several weeks playing with it, i feel that KohanaPhp far exceed CakePHP and CodeIgniter. Now i want to buid a *secret* site, after taking a look around i found that Zend framework has a neat library providing many classes that i need. Take a look of this list, you'll get the idea:
- Zend_Acl
- Zend_Captcha
- Zend_Feed
- Zend_Gdata
- Zend_Http
- Zend_OpenId
- Zend_Pdf
- Zend_Rest
- Zend_Service_Akismet
- Zend_Service_Amazon
- Zend_Service_Audioscrobbler
- Zend_Service_Delicious
- Zend_Service_Flickr
- Zend_Service_Nirvanix
- Zend_Service_ReCaptcha
- Zend_Service_Simpy
- Zend_Service_SlideShare
- Zend_Service_StrikeIron
- Zend_Service_Technorati
- Zend_Service_Twitter
- Zend_Service_Yahoo
I really love it, but i don't like zend framework itself. So i search around and found this cool tutorial.
It gives cool trick but doesn't work well, so i modify a bit.
Put the 'Zend' folder (unpacked from the Zend Framework package, under 'Library/') in Kohana installation's 'application/vendors/Zend' folder
Copy code below and paste it to a new file called Zend.php in application/libraries/.
-
/**
-
* Zend Framework Loader
-
*
-
* Put the 'Zend' folder (unpacked from the Zend Framework package, under 'Library/')
-
* in Kohana installation's 'application/vendors/Zend' folder
-
* You can put it elsewhere but remember to alter the script accordingly
-
*
-
* Usage:
-
* Zend::instance('Zend/Package/Name');
-
* Zend::instance('Zend/Service/Yahoo');
-
*
-
* * the second usage is useful for autoloading the Zend Framework library
-
* * Zend/Package/Name does not need the '.php' at the end
-
*/
-
class Zend
-
{
-
/**
-
* Returns a singleton instance of URI.
-
*
-
* @return object
-
*/
-
{
-
static $instance;
-
-
// Create the singleton
-
if ($instance == NULL)
-
{
-
// Initialize the URI instance
-
$instance = new Zend($class);
-
} else
-
{
-
$instance->load($class);
-
}
-
-
return $instance;
-
}
-
-
/**
-
* Constructor
-
*
-
* @param string $class class name
-
*/
-
function __construct($class = NULL)
-
{
-
// include path for Zend Framework
-
// alter it accordingly if you have put the 'Zend' folder elsewhere
-
-
if ($class)
-
{
-
require_once (string) $class . EXT;
-
//Log::add('debug', "Zend Class $class Loaded");
-
}
-
else
-
{
-
//Log::add('debug', "Zend Class Initialized");
-
}
-
}
-
-
/**
-
* Zend Class Loader
-
*
-
* @param string $class class name
-
*/
-
function load($class)
-
{
-
require_once (string) $class . EXT;
-
//Log::add('debug', "Zend Class $class Loaded");
-
}
-
-
}
Do you have trouble figuring out how to use it?
See an example below, you'll get a nice grab just within seconds:
-
class Welcome_Controller extends Controller {
-
-
public function index()
-
{
-
$zend = Zend::instance('Zend/Service/Yahoo');
-
$yahoo = new Zend_Service_Yahoo('zendtesting');
-
$keywords = "kitty";
-
-
if ($results->totalResults()> 0) {
-
echo '
-
<p id="image">';
-
echo '
-
<h2>Image Search Results</h2>
-
';
-
foreach ($results as $result) {
-
echo "<a href="http://php.fallenray.com/wp-admin/%7B$result-%3EClickUrl%7D" title="$result->Title"><img src="http://php.fallenray.com/wp-admin/%7B$result-%3EThumbnail-%3EUrl-%3EgetUri%28%29%7D" /></a>";
-
}
-
echo '
-
';
-
}
-
}
-
-
}
Another way to do this also posted as an updated tutorial. Things are getting much simpler now. I still liked my own twisted approach though :p
Also you might want to check out the official tutorial.
Happy ending, i can use the best PHP5 frameworks along with the best PHP5 library. It cannot get better than this. Try it, if you miss this you'll be sorry.
PHP5: Screen scraping with DOM and XPath
This tutorial is continuation from previous yahoo screen-scraping using PHP4 tutorial.
We will try different method using DOM and XPath which only supported in PHP5.
First, a bit knowledge of XPath is required. More about XPATH can be read on:
http://www.zvon.org/xxl/XPathTutorial/General/examples.html
Also there's small concern that using XPATH is a bit slower than pure DOM Traversal. Read Speed: DOM traversal vs. XPath in PHP 5
But i personally also think that XPath is neat and easier.
Let's start. First we diagnose document structure using Mozilla Firebug.
Try a very easy case, which is to grab the title "Top Movies":

Copy XPath using Firebug and get this query:
/html/body/center/table[8]/tbody/tr/td[5]/table[4]/tbody/tr/td/font/b
- Firefox automatically fix broken html structure, and it also add tbody tag. So, we need to remove this tag.
- Only grab first row of table. Change .../tr/td/font/b into .../tr[1]/td/font/b
Now we get our first XPath query:
/html/body/center/table[8]/tr/td[5]/table[4]/tr[1]/td/font
Next harder case is to grab contents.

XPath query from Firebug is:
/html/body/center/table[8]/tbody/tr/td[5]/table[4]/tbody/tr[2]/td[2]/a/font/b
- Same problem here. Firefox automatically fix broken html structure, and it also add tbody tag. Remove tbody tag from XPath query.
- Grab all row of table. Change .../tr[2]/td[2]/a/font/b into .../tr/td[2]/a/font/b
Final XPath query for content is:
/html/body/center/table[8]/tr/td[5]/table[4]/tr/td[2]/a/font/b
Now final step is to put all two XPath queries into few lines of code, and we're done:
-
<?php
-
include ('Snoopy.class.php');
-
-
$snooper = new Snoopy();
-
if ($snooper->fetch('http://movies.yahoo.com/mv/boxoffice/')) {
-
$dom = new DomDocument();
-
$dom->loadHTML($snooper->results);
-
-
$x = new DomXPath($dom);
-
-
// /html/body/center/table[8]/tbody/tr/td[5]/table[4]/tbody/tr/td/font/b
-
$nodes = $x->query('/html/body/center/table[8]/tr/td[5]/table[4]/tr[1]/td/font/b');
-
-
// /html/body/center/table[8]/tbody/tr/td[5]/table[4]/tbody/tr[2]/td[2]/a/font/b
-
$nodes = $x->query('/html/body/center/table[8]/tr/td[5]/table[4]/tr/td[2]/a/font/b');
-
foreach ($nodes as $node) {
-
}
-
}
-
?>
Tags: php5, screen scraping, code example
CakePHP: How to add Search Engine Friendly (SEF) URLs
How many times you wondered how great it would be if your URLs didn't look so much like:
http://www.server.com/posts/view/1058
But more like:
http://www.server.com/posts/view/my_first_post
Read the rest on:
http://bakery.cakephp.org/articles/view/adding-friendly-urls-to-the-cake-blog-tutorial