Archive

Archive for the ‘Web’ Category

Speedup Mysql and Webserver with Intel Compiler and tcmalloc

February 20th, 2009

After reading some recent benchmark reguarding tcmalloc  performance on mysql. I decide to rebuild my whole webhosting stack with it.

ICC is intel’s c++ compiler, which has faster performance is also memtioned on mysql website.

Most distros should already has google performance tools prepackaged. Installation of ICC is slightly more complicated, you can download it directly from intel’s website which is free download for non-commercial use. Archlinux and Gentoo both have packaged installer. On ubuntu/debian system you probably also need build-essential and apt-build to rebuilt packages. On archlinux you will need base-devel and abs.

For most packages, the following bash script can be used before configuration/make step.  Don’t ommit the dot on first line and change the path of iccvars.sh to your installation directory.

 . /opt/intel/Compiler/11.0/081/bin/iccvars.sh intel64
CC=icc
CFLAGS="-xHOST -O3 -no-prec-div "
LD=xild
AR=xiar
CXX=icpc
CXXFLAGS="-xHOST -O3 -no-prec-div "
LDFLAGS=-ltcmalloc_minimal
export CC CFLAGS LD AR CXX CXXFLAGS LDFLAGS

These setting seems safe for all packages. Here is a summary of package specific cflags setting.

  Mysql Cherokee Nginx Varnish PHP Memcached
-static No No No No N/A No
-ipo No No Yes No N/A Yes
LDFLAGS=-ltcmalloc_minimal Yes Yes Yes Yes Yes Yes
configure option –disable-shared –with-mysqld-libs=-ltcmalloc_minimal None None –disable-jemalloc Failed with ICC None

This might disappoint you. But the rebuilt software stacks show no improvement whatsoever in my benchmark.

Bookmark and Share  
 

Yejun Linux, Web , , , , , , , ,

How to use SimpleCDN with WordPress

February 15th, 2009

Update 2/24/2009: This method is deprecated. I wrote a plugin to rewite urls.
Update again: SimpleCDN is slower comparing to CloudFront, I moved all my link to CF. If you have problem copying files to S3, you can read this post.

WordPress is my favorite bloging software, with many builtin features and powerful plugins. WordPress.com, the blogging platform by automattic, has been using CDN for a long time. However there is no public available solution to make self hosted WordPress to use a CDN as well. Fortunately the php code itself is very clean to read, so it took me about 5 minutes to find where I should modify to fully integrate my new CDN address.

Before you proceed, please read my previous posts on how to use CNAME with SimpleCDN and how to process javascript and css files for WordPress. Make sure your SimpleCDN’s CNAME and bucket working properly. I used a mirror bucket which points to my blog address, that is probably the simplest way to mirror your static assets. And use ‘-s1′ pre-url to insure client side cache working, because SimpleCDN does not use 304 not modified http header at all. I used bucket name ’s.mudy.info’ for my own blog.

Once your DNS full propagated, open your favorite text editor open following 3 files.

wp-includes/class.wp-scripts.php‘: near line 75, change

$src = $this->base_url . $src;

to

$src = 'http://yourcdnbucketname' . $src;

There is no trailing slash after your bucketname. If you want to make sure client browser always use cached version, you can also comments out the following line.

// $src = add_query_arg('ver', $ver, $src);

But this change may break WordPress upgrade.

wp-include/class.wp-styles.php‘: near line 79, do the same change as above.

wp-include/theme.php‘: near line 504, change

return apply_filters('theme_root_uri', content_url('themes'), get_option('siteurl'));

to

return 'http://yourcdnbucketname/wp-content/themes';

That’s it. Now clean your cache and test your blog.

Bookmark and Share  
 

Yejun Web , , ,

Optimize WordPress javascript placement

February 13th, 2009

YSlow rule 6: Put JS at the bottom. This is a very simple rule, but it has noticeable improvement on actual page loading time. Because javascript in head will block both page elements loading and rendering. The placement of javascript in WordPress 2.7.1 is hard coded to be placed in html head, because most ajax plugin would expect javascript library to be fully loaded before page load. If you use WordPress without any fancy ajax related plugins, it is very possible to load javascript at bottom of every page without breaking any blog function. I also tested it with Lightbox 2 without any noticeable problems. The measurable page loading time difference is near half a second when browser cache is empty. Before you proceed to make any change to WordPress php files, I would recommend you to do a full backup of database and related php files. Now open ‘wp-includes/default-filters.php‘, search wp_print_scripts. Near line 184, you should find,

add_action('wp_head', 'wp_print_scripts');

Change this to

add_action('wp_footer', 'wp_print_scripts');

Clear your WordPress page cache and test your WordPress. Ideally one could write a plugin hooking into filter ‘print_scripts_array’ to remove plugins from html head conditionally. But I have not found one yet. There are a lot other methods to safely speedup javascript loading in html header. Unfortunately they are not very easily implemented and cross different browsers. Steve, author of Yslow had a post and talk explaining the whole situation.

Bookmark and Share  
 

Yejun Web , , , , ,

SimpleCDN CNAME Confusion

February 9th, 2009

I am using SimpleCDN to serve some static scripts and images of WordPress on my website.

Not like AWS CloudFront, Amazon’s CDN service, which only allows expiration date carried from S3 file meta. SimpleCDN supports fine tuning Expiration, P3P and gzip encoding through something called Pre-URL. Which look like these

http://s.mudy.info.simplecdn.net/
http://s.mudy.info-s1.simplecdn.net/
http://s.mudy.info-e7.simplecdn.net/

Here ‘-s1′ means special 1 with expire and gzip; ‘-e7′ means 10 years expire without gzip. And a lot more with different combo effects.

SimpleCDN also supports CNAME to any of these urls. Because there is not a lot document on how to use simplecdn. I mistakenly CNAMEed ’s.mudy.info’ to the first URL

s.mudy.info.simplecdn.net

Run my website through Yslow, I got a lot F score due to lack of expire headers and non-gzip on static files. I realize I have to use these different Pre-URLs in order to get http headers, so I substitute ’s.mudy.info’ in WordPress with ’s.mudy.info-s1.simplecdn.net’ or ’s.mudy.info-e7.simplecdn.net’. Originally very simple urls’ become long and urgly.

Due to some DNS setting error, I accidentally pinged 2 different SimpleCDN pre-urls. The results are 2 different IPs. I reallized I made another mistake today, these pre-urls are supposed to be used as CNAME to get different http headers and gzip encoding, because these hostname all has their own IPs. A quick testing I CNAME ’s.mudy.info’ to

s.mudy.info-s1.simplecdn.net

which gives nicely gzipped files with correct expiration settings.

This design is what I initially did not expect. Because traditional CDN usually use HTTP hostname to distinguish objects, so I normally would think I have to use different hostnames. However SimpleCDN use anycast , so they use same IP block on on all edge servers, which make it possible to use different IP addresses to distinguish requests.

Bookmark and Share  
 

Yejun Web , ,

Correct way to password protect Cherokee webserver

February 8th, 2009

I have played Cherokee server on my personal blog a couple of days. I used it for some private file. Some directories need to be password protected. Initially I set it up in this way.

Incorrect password protection for Cherokee webserver

Incorrect password protection for Cherokee webserver

Then I tested against some static files, it obviously worked. However a couple days later, I realize the php script inside this directory was not protected at all including index.php. A rather easy fix, I added the same auth method to php handler as well. However as I add more stuff into my private directory, some of them require individual handler to work correct, so I added same http authentication method to all of them. It is really a pain to maintain such a long auth list, suddenly I realized I must have done this in a wrong way.

After digging into the cherokee document and cookbook, I find this simple solution to protect a whole directory.

  1. Add a directory rule which match the directory you want to protect.
  2. Set the handler to None in Handler tab
  3. Set authentication method in Security tab.
  4. Move this rule to the top and uncheck final.

Correct way to password protect cherokee webserver.

Correct way to password protect cherokee webserver.

That’s it.

Bookmark and Share  
 

Yejun Linux, Web , , ,

How to do http redirect on varnish

February 7th, 2009

There is no built in function to do redirecting in Varnish.  After googling around, here is the result.

Using error page of varnish to return an object with location header, since obj is not available within vcl_recv.

sub vcl_recv {
if (req.http.host ~ "^(www.)?mudy.info$")
{
error 302;
}
}
sub vcl_error {
if (obj.status == 302 && req.http.host ~ "^(www.)?mudy.info$")
{
set obj.http.Location = "http://blog.mudy.info/";
}
}

I am unsure whether there is a simpler way to verify the request, so I test the same condition again inside vcl_error.

If you want request url appended to redirection, you can use this instead.

set obj.http.Location = "http://blog.mudy.info" req.url;
Bookmark and Share  
 

Yejun Linux, Web , , , , ,