Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Thursday, April 16, 2009

Ubuntu 9.04: Installing Lighttpd 1.5 from source with PHP + mod_cache

To install PHP:
sudo apt-get install php5-cgi

Installing Lighttpd 1.5
The latest version I could find on their site:
http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModCache
http://opensu.se/~darix/lighttpd/
http://blog.lighttpd.net/articles/tag/1.5.0

wget http://www.lighttpd.net/download/lighttpd-1.5.0-r1992.tar.gz
wget http://www.linux.com.cn/modcache/lighttpd-1.5.0.r2294.modcache.v.1.6.1.tar.gz


This is the patched version of lighttpd with mod_cache, extract this to a folder

tar zxvf lighttpd-1.5.#######.gz
cd lighttpd-1.5.0.cache


We need to install some required libraries first

sudo apt-get install libssl-dev zlib1g-dev libbz2-dev libattr1-dev libpcre3-dev libmysqlclient15-dev libfam-dev libldap2-dev libfcgi-dev libgdbm-dev libmemcache-dev liblua5.1-0-dev pkg-config uuid-dev libsqlite3-dev libxml2-dev libkrb5-dev libglib2.0-dev libaio-dev libfam0 memcached


type ./configure --help to see configuration options

This is what I used
./configure --program-prefix= --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc --datadir=/usr/share --includedir=/usr/include --libdir=/usr/lib --libexecdir=/usr/libexec --localstatedir=/var --sharedstatedir=/usr/com --mandir=/usr/share/man --infodir=/usr/share/info --with-openssl --with-memcache --with-gdbm

make

sudo make install


after installing you should be able to type
lighttpd -V

and see some nice output from lighttpd

you can also type spawn-fcgi to make sure that installed as well

type sudo su

copy the sample config file
mkdir /etc/lighttpd
cp doc/lighttpd.conf /etc/lighttpd/

log folder
mkdir /var/log/lighttpd

we need to create a startup file in the /etc/init.d folder
Go here to download the startup script http://redmine.lighttpd.net/projects/lighttpd/wiki/ScriptsUbuntu
create a file called lighttpd in the /etc/init.d/ folder and then
chmod a+x /etc/init.d/lighttpd

Create a user to run lighttpd: if you have installed lighttpd 1.4 from apt or apache you can just use the www-data user again
adduser -m -d /var/www -s /sbin/nologin lighttpd

Modify the lighttpd file from /etc/init.d/ to match the user you want to run it as

chown www-data:www-data /var/log/lighttpd

then type
update-rc.d lighttpd defaults


Edit the lighttpd.conf file to enable php fastcgi

Make sure following modules are loaded:
server.modules += ( "mod_cache" "mod_proxy_core", "mod_proxy_backend_fastcgi" )

You can also enable other modules that you need, mod_cache has to be enable first!
cache.bases = ("/var/cache/lighttpd")
cache.enable = "enable"
cache.support-queries = "enable"
cache.debug = "enable"
cache.dynamic-mode = "enable"
Change
server.document-root = "/var/www/"
server.errorlog = "/var/log/lighttpd/error.log"
server.event-handler = "linux-sysepoll"
server.network-backend = "gthread-aio"
accesslog.filename = "/var/log/lighttpd/access.log"
compress.cache-dir = "/tmp/lighttpd/cache/compress/"
compress.filetype = ("text/plain", "text/html")
#### status module
status.status-url = "/server-status"
status.statistics-url = "/server-statistics"

cache.bases = ("/var/cache/lighttpd")
cache.enable = "enable"
cache.support-queries = "enable"
cache.debug = "enable"
cache.dynamic-mode = "enable"
cache.refresh-pattern = (
"/$" => "5 update-on-refresh no-expire-header", # update homepage every 5 minutes and on refresh requests without setting expire headers
"\.(?i)(flv)$" => "0 fetchall-for-range-request flv-streaming", # to work with mod_flv_streaming for flv files
"\.(?i)(js|css|xml)$" => "240", # update js/css/xml every 4 hours and on refresh requests
"\.(?i)(htm|html|shtml)$" => "30", # update html/htm/shtml every 30 minutes and on refresh requests
"\.(?i)(jpg|bmp|jpeg|gif|png)$" => "2880", # update graphics files every 2 days
"\.(?i)(rar|zip|wmv|avi|mp3|ape|rm|mpeg|mpg|wma|asf|rmvb|flv)$" => "0 fetchall-for-range-request", # cache media file forever
".(?i)php$" => "5", # update php request every 5 minutes
"." => "30 update-on-refresh" # default to update every 30 minutes and on refresh requests
)


Make sure /var/cache/lighttpd is writeable by lighttpd and it exists.

Configure fastcgi PGP module by appending following configuration directives:

$HTTP["url"] =~ "\.php$" {
proxy-core.balancer = "round-robin"
proxy-core.allow-x-sendfile = "enable"
# proxy-core.check-local = "enable"
proxy-core.protocol = "fastcgi"
proxy-core.backends = ( "unix:/tmp/php-fastcgi.sock" )
proxy-core.max-pool-size = 16
proxy-core.worked-with-modcache = "enable"
}


Make sure to leave the # in front of proxy-core.check-local = "enable" otherwise lighttpd won't start, don't know why.

Also don't forget to add proxy-core.worked-with-modcache = "enable" otherwise mod_cache won't do anything.

Save the config file and start lighttpd

sudo /etc/init.d/lighttpd start

If everything goes well you should see something like this
* Starting lighttpd [ OK ]
* Starting spawn-fcgi spawn-fcgi.c.206: child spawned successfully: PID: 15245
[ OK ]

inside the /var/www folder create index.php file and add something to it

test by browsing http://localhost/index.php
You should see php configuration options here


How to uninstall
go back to the lighttpd source
sudo make uninstall


References:
http://redmine.lighttpd.net/projects/lighttpd/wiki/InstallFromSource
http://www.cyberciti.biz/tips/rhel-lighttpd-15-installation-configuration-howto.html
http://www.cyberciti.biz/tips/howto-increase-lighttpd-performance-with-linux-aio.html
http://www.cyberciti.biz/tips/freebsd-linux-lighttpd-1-5-fastcgi-configuration.html
http://www.linux.com.cn/modcache/
http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModCache#Installation

Saturday, April 11, 2009

Traffic Shaping with Lighttpd + PHP

Here's how I manage traffic on my web site that's served over a basic cable connection. The site has a forum and an area for register members to download stuff from. Having a very limited upload capacity I have to make sure members don't suck everything and make the forum unresponsive.

So here's how my lighttpd.conf file is setup:

# this limits the amount of connection a user can make to the server
evasive.max-conns-per-ip = 10

# then on my site configuration I use a combination of connection.kbytes-per-secon and evasive.max-conns-per-ip to throttle user connections to the download script
$HTTP["host"] =~ "sitegeisha\.dyndns\.dk" {
connection.kbytes-per-second = 64
$HTTP["url"] =~ "dl\.php" {
connection.kbytes-per-second = 32
evasive.max-conns-per-ip = 3
}
}

This configuration allows me to limit the number of simultaneous downloads they can make, if they go over 3 they get a 403 error message.

Thursday, January 10, 2008

Update on GD vs ImageMagick

I decided it wasn't fair for me to compare gd and imagemagick when I was calling imagemagick through exec( "convert image.jpg -thumbnail 100x TN_image.jpg" ) so I decided to install the extension for php called imagick.

This had to be installed to PECL, it didn't work on my ubuntu 7.10 by just doing apt-get install php5-imagick.

Nice examples of how to use imagick on Mikko's blog. I couldn't follow his examples though as the version that was installed through PECL didn't have some of the functions he used. Would have been nice to put shadows on my thumbnails though.

Anyways I made the script that creates thumbnails through imagick and compared it to the script in my gallery that uses GD. It seems GD was ahead by just a bit this time, although the quality of the imagick thumbnails were far superior, probably because I had set the GD functions to use less resolution.


GD TEST


From this example the images were really huge (3000x4500 pixels 1.5-2mb), the thumbnails rendered by GD seems pixelated compared to what imagick produced.

IMAGICK TEST


Imagick created 12 thumbnails with very nice quality but was 2 seconds slower than GD.


Powered by ScribeFire.

Wednesday, January 9, 2008

GD vs ImageMagick

Well I've seen some code in php.net about creating thumbnails fast, basically some guy wrote an alternative to imagecopyresampled and called it fastimagecopyresampled. I decided to see how fast it really is, so to compare I created a script that would create thumbnails from a bunch of images using ImageMagick.

And here are the results:

Using ImageMagick to generate 6 thumbnails from a gallery.



Now using GD to generate the same thumbnails, I deleted the old ones.



It is clear who the winner is, definitely GD.



References:
http://us.php.net/manual/en/function.imagecopyresampled.php
http://www.php.net/manual/en/function.imagick-thumbnailimage.php
http://valokuva.org/?paged=2



Powered by ScribeFire.

Monday, May 28, 2007

Speedup PHP

So I was reading this article in slashdot and was playing with the server. I pretty much have done everything they have on that article except for using eaccelerator or apc.

So I decided to give eaccelerator a try, it seems to be easy to install and found a lot of stuff online about it. It wasn't on the default ubuntu repositories so I had to manually install it.



That one is the one I followed to install it on my ubuntu dapper box. http://2bits.com/articles/installing-eaccelerator-0-9-5-1-on-ubuntu-feisty-7-04.html (to get phpize install php-dev from the deb sources)

Seems to work great and I am loving it! Hope you guys see a difference on the server performance now. I observed that galleries when cached load up in about 0.01-0.14 seconds.