drupal

Odd and evil things

Today I learned it's possible to add an extra submit handler when using hook_form_alter() in Drupal. En concreto, I can for example create new nodes when someone adds a new language in the locale module. If I were very evil, I could write a contributed module which would be very interesting for the community, but if someone doesn't take a look at my code, he wouldn't see I've added an extra submit function that deletes all nodes after he changes his files settings. Luckily I'm a decent person, I'll never do that :) I'm just very happy I found a solution without messing on the core files!

Evil also seems to be the combination of PHP5, apache and APC: the thing simply crashes every minute, especially when heavy calculations take place leaving nothing but segfault lines in your error log and dumping core in your directory. I've been running a server with lighttpd and XCache (xcache.lighttpd.net) now for a week and I haven't had any phone calls from people seeing the white screen of death, let's hope things stay that way, otherwise, well, I dunno what else for now ...

I also learned something - rather stupid - today: if you enter 0.0 (or 00.00, 0000.000000, etc) in your address bar, the browser takes you to your localhost. I'm not sure what happens if you don't run a local webserver, because I simply don't have any computer without it - and I'm to lazy right now to stop my lighty service.

Anyway, it has been a very interesting and stressy month and I can finally chill out a bit, maybe (finally) get to record some songs again, whatever and of course counting the days untill the first of march when I leave for the upcoming DrupalCon in Boston and a few days wandering around in New York, yay!

Topics 

drupal, lighttpd, apc, xcache

The init hook enigma, part 2

I struggled a few days with some functionality on the new CarChannel site where visitors are offered a splash page the first time they visit the site. I needed to built in some exceptions, like the special msn page, the xml calls and the game pages. Testing when you are logged in always worked, but when browsing anonymous and with normal caching on, things went wrong, especially with url aliases. Reading source code and documentation - or now when you are reading this article - can save your life, so when you have an init hook and you need the know the 'q' variable, add

drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);

at the top of your hook before doing anything else. This will save you many sleepless hours and frustrating tests!

Being the only *nix sysadmin (or at least, the only guy at work who knows a little more then just opening an ssh-session) also made me very stressed out the last couple of weeks because services suddenly died without leaving any information in logs. I spent the whole afternoon yesterday setting up extra monitoring services on 2 higly imported webservers which check apache, mysql, ssh and load average every five minutes and , if needed, takes the appropriate action (restart, renice etc). It was the first night in a week I finally slept the whole night through, so fingers crossed!

Topics 

drupal, work, linux, hooks

The init hook enigma

I wasted a few hours on figuring out why an init hook in one of my modules wasn't invoked when normal page caching was on. Turns out the boostrap field in the system table for my module was still 0, instead of 1. So remember boys and girls, when you allready enabled your own custom module and you add an hook_init, remember to disable and enable your module again so Drupal knows it exists. Or manually alter the record in the system table in your database if disabling the module breaks your site.

On the bright side, I think I know every line of bootstrap.inc and cache.inc file now by heart.

Topics 

drupal, hooks

Drupal on lighttpd with clean urls made easy

Apache is and will be one of the best/popular webservers out there for years for most websites, but nowadays, when it comes to scalability, load, performance and speed, my first choice of webserver is lighttpd, also called lighty. It took me some months to create the perfect configuration for a Drupal site with a lot modules but I think I finally found it. My setup is used for Drupal 5.3 (and for the first time on a new site called CarChannel, cf http://realize.be/carchannel), but will be more or less the same for the upcoming 6.x release in a few months. And even if you keep using apache, the rest of this article still is interesting to gain more speed for your Drupal setup with more caching tips!

Lighttpd and clean URL's - http://www.lighttpd.net/
I was very impressed with a previous project we launched at work using lighttpd instead of apache. The load on the machine constantly kept under 0.40 having more then 1 million hits per day. Time to check if Drupal would run also as smooth on this server. Lighttpd doesn't support mod_rewrite like Apache does, but after lots of googling, I finally found the solution which also enables clean urls and keeps everything running as it should. The answer is LUA and mod_magnet. No more exotic rewrite rules in your lighttpd.conf which always caused problems at some point. I'm not going to explain it here again for you, all credits go to other authors for creating this configuration. Read following pages to make your sites fly

When all is done, your Drupal setup works with clean urls and yes imagecache works fine! An excerpt of my local lighttpd.conf configuration is underneath:

server.modules = (
   "mod_access",
   "mod_fastcgi",
   "mod_magnet",
   "mod_simple_vhost",
)

fastcgi.server =
   ( ".php" =>
     ( "localhost" =>
       (
         "socket" => "/var/run/lighttpd/php-fastcgi.socket",
         "bin-path" => "/usr/bin/php-cgi"
       )
     )
   )

url.access-deny = ( "~", ".inc", ".engine", ".install", ".module", ".sh", "sql", ".theme", ".tpl.php", ".xtmpl", "Entries", "Repository", "Root" )

$HTTP["host"] =~ "(^|\.)connector$" {
  index-file.names = ( "index.php" )
  simple-vhost.server-root   = "/home/"
  simple-vhost.default-host  = "projects"
  simple-vhost.document-root = "connector"
  magnet.attract-physical-path-to = ( "/etc/lighttpd/drupal.lua" )
  server.errorlog             = "/var/log/lighttpd/connector-error.log"
  accesslog.filename             = "/var/log/lighttpd/connector-access.log"
}

XCache - http://xcache.lighttpd.net
XCache is a fast, stable PHP opcode cacher that has been tested and is now running on production servers under high load. It is tested (on linux) and supported on all of the latest PHP cvs branches such as PHP_4_3 PHP_4_4 PHP_5_0 PHP_5_1 PHP_5_2 HEAD(6.x). ThreadSafe/Windows is also supported. It overcomes a lot of problems that has been with other competing opcachers such as being able to be used with new PHP versions. Does the same thing like APC and I really prefer this one, because running APC with PHP5 on lighty was a nightmare. XCache seems to do the job well without segfaulting every hour.

APC - http://be.php.net/apc
The Alternative PHP Cache (APC) is a free and open opcode cache for PHP. It was conceived of to provide a free, open, and robust framework for caching and optimizing PHP intermediate code. This framework will keep your scripts in a compiled form in memory, so every script doesn't have to be interpreted with every call you make to your website. The PECL package is not bundled in the PHP package itself, but is easy installable by just typing 'pecl install apc'. After that just add 'extension=apc.so' to your php.ini and restart your webserver. Warning: running with PHP5 is unstable, PHP4 runs perfect.

Both opcache codes optimize the speed results sometimes by 5!

MySQL Query Cache - http://dev.mysql.com/doc/refman/5.0/en/query-cache.html
The query cache stores the text of a SELECT statement together with the corresponding result that was sent to the client. If an identical statement is received later, the server retrieves the results from the query cache rather than parsing and executing the statement again. No further explanation is needed I guess.

Drupal settings
Drupal comes with it's own caching and speed tricks. Enable normal page caching for anonymous users which results in less database queries and faster page loads. Logged-in visitors don't benefit from the caching, but read on if you also want to have a decent caching for those users! You can also aggregate css files when your site is production. Drupal 6 also supports this feature for all javascript files, great! Drupal core also ships the throttle module which handles site congestion. I've personally never used it, but it's worth mentioning it.

Module: Block Cache - http://drupal.org/project/blockcache
"This module creates a cached version of each block. Block caching happens separately from page caching and so it will work for logged-in users whether or not page caching is enabled for the site." This is a huge advantage because it can reduce complex queries or calculations needed for some blocks. The interface also permits you to set a cache expiry time and whether it's a page and/or user specific content block. Highly recommended! Note: block cache is in core starting from Drupal 6.x!

Module: Advanced Cache - http://drupal.org/project/advcache
"The advanced caching module is mostly a set of patches and a supporting module to bring caching to Drupal core in places where it is needed yet currently unavailable. These include caching nodes, comments, taxonomy (terms, trees, vocabularies and terms-per-node), path aliases, and search results." The only small downside in my opinion is that when a new release of Drupal core is out, you really need to re-apply these patches and sometimes need to patch files manually. At the time of this writing, an official Drupal 5.3 release isn't out there, but the patches are allready available on http://drupal.org/node/184522. Only use if if you think it's really worth it.

Module: Memcache - http://drupal.org/project/memcache
An API for using Memcached and the PECL Memcache library with Drupal. Instead of storing the data into the database, cached data is available in memory. At this time, it seems to be broken with the 5.3 release. Tests with 5.2 work fine, so let's hope a new release is out soon, because together with all previous settings, the boost performance is enormous!

That's it. After reading this, you should be able to make your new site fly, happy tuning! When I find some time, I'll try and generate some benchmark reports, but I'm pretty sure you will feel the difference yourself. You can also read http://drupal.org/node/2601 on drupal.org which handles the same topic, interesting for all you sysadmins out there.

CarChannel

From a far away place I can announce my colleagues at One Agency succesfully launched a new Drupal-powered site, called CarChannel. Although my father is an auto mechanician, I've never been very interested in what's really going on inside a car and I don't really prefer one model. The fact that the CarChannel site has everything to do with cars didn't change that fact, I was mostly interested making sure I got a topspeed site with Drupal with cool features which also looked nice. It's built on 5.3 and for the first time I'm running it on a lighttpd server *with* clean URLS enabled without any problems, even the imagecache module behaves like it should! But anyways, all you people interested in the latest news on cars with lot's of pictures and cool video's, do check out the site. You can even create your own video channel but only with external video content in fase 1. In fase 2 (I guess somewhere in december) you'll be able to add your home made video's of your tuned car!

Topics 

drupal, work, cars

Pages

Subscribe to RSS - drupal

You are here