Page 2 of 3

Installing XCache on a server with Cpanel

Traffic on my VPS jumped over the past few weeks and I needed a better caching solution so I installed W3 Total Cache. It provided you the option of caching pages, databases queries, features an object cache and even allows you to configure a CDN to serve your files. Trouble is, my server only had eaccelerator installed and it wasn’t compiled to use object-caching. Instead of upgrading recompiling eaccelerator, I opted to install XCache on my CPanel-enabled VPS.

Here’s a complete guide on installing Xcache on Cpanel.

First get the latest sources for XCache. As of publishing this guide, it’s 1.3.1

cd /usr/src/

After which, unarchive the source and cd into the directory

tar -xzvf xcache-1.3.1.tar.gz
cd xcache-1.3.1

Time to configure the sources for the current PHP environment

[root@host xcache-1.3.1]# phpize
Configuring for:
PHP Api Version:         20041225
Zend Module Api No:      20060613
Zend Extension Api No:   220060519

Once you see the message above, XCache is ready to install.

./configure && make && make install

This will check for errors and run the installation script. Once complete, you should see this line:

Installing shared extensions: /usr/local/lib/php/extensions/no-debug-non-zts-20060613/

If like me, you have a different OPCode cache installed like eaccelerator, you need to comment any instances of these from your php.ini file. In my case it was

After that copy these into the php.ini. Note that these are the settings on my server. Tweak them as you need or please.

xcache.admin.auth = off
xcache.admin.enable_auth = off

; admin installation:
xcache.admin.user = "admin username" ;if you're going to use this
xcache.admin.pass = "md5 hash of your password" ;change accordingly

xcache.shm_scheme = "mmap"
; to disable: xcache.size=0
; to enable : xcache.size=64M etc (any size > 0) and your system mmap allows
xcache.size = 128M
; set to cpu count (cat /proc/cpuinfo |grep -c processor)
xcache.count = 4
; just a hash hints, you can always store count(items) > slots
xcache.slots = 8K
; ttl of the cache item, 0=forever
xcache.ttl = 13300
xcache.cacher = On
; interval of gc scanning expired items, 0=no scan, other values is in seconds
xcache.gc_interval = 0

; same as aboves but for variable cache
xcache.var_size = 2M
xcache.var_count = 2
xcache.var_slots = 1K
; default ttl
xcache.var_ttl = 3600
xcache.var_maxttl = 7200
xcache.var_gc_interval = 300

xcache.test = Off
; N/A for /dev/zero
xcache.readonly_protection = Off
; for *nix, xcache.mmap_path is a file path, not directory.
; Use something like "/tmp/xcache" if you want to turn on ReadonlyProtection
; 2 group of php won't share the same /tmp/xcache
; for win32, xcache.mmap_path=anonymous map name, not file path
xcache.mmap_path = "/dev/zero"

; leave it blank(disabled) or "/tmp/phpcore/"
; make sure it's writable by php (without checking open_basedir)
xcache.coredump_directory = ""

; per request settings
xcache.cacher = On
xcache.stat = On
xcache.optimizer = Off

; per request settings
; enable coverage data collecting for xcache.coveragedump_directory and xcache_coverager_start/stop/get/clean() functions (will hurt executing performance)
xcache.coverager = Off

After editing your PHP ini, type:

 php -v

to check if things loaded properly. This is the output from my server:

[root@host xcache-1.3.1]# php -v
PHP 5.2.11 (cli) (built: Oct 27 2009 16:43:13) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
    with XCache v1.3.1, Copyright (c) 2005-2010, by mOo
    with the ionCube PHP Loader v3.3.20, Copyright (c) 2002-2010, by ionCube Ltd., and
    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

And you’re done. After which i simply set W3 Total Cache to use XCache for Database and Object Caching, flushed all existing caches and voila!

Just upgraded ;)

I bit the bullet.

I’ve managed to complete a few projects this year which gave me enough money to sponsor the upgrade :).

Yep it’s a costly upgrade (but I still have a few more things to get, like 2 SSDs, one for my older Core 2 13″ MBP and the new one) but it’s well worth every cent spent.
Continue reading

cPanel error: httpd failed – A restart was attempted automagically

While my recent server transfer has been smooth sailing, I did encounter one error that bugged the hell out of me.

At certain points of the day, I’d be receiving a notice that my VPS had it’s httpd process fail and restart automatically.  A copy of the email is posted below:

httpd failed @ Sun Apr 10 07:07:09 2011.. A restart was attempted automagically.
Service Check Method:  [tcp connect]

Failure Reason: Unable to connect to port 80

After a quick bout of google-fu I found our that the server is exceeding the allowed number of connections 🙁 Not good considering that the new server is quite powerful enough to handle more than it’s current peak traffic. Reading further into this, I had to execute this line:

grep MaxClients /usr/local/apache/logs/error_log

which then netted me this response:

WARNING: MaxClients of 512 exceeds ServerLimit value of 256 servers,
 lowering MaxClients to 256.  To increase, please see the ServerLimit
[Sat Apr 09 12:47:10 2011] [error] server reached MaxClients setting, consider raising the MaxClients setting

Soooo the problem is how apache is setup. The fix? Simply log in to cPanel and set the setting for MaxClients to something higher (in my case I doubled the number from 256 to 512) as well as he ServerLimit (to the same number). Of course if you want to do this manually, simply edit the MaxClients settings on apache’s httpd.conf file and restart the server.

Migrating my blogs to WiredTree

Because of several stability issues with my current VPS provider (not for this site), I decided to move forward and transfer to a new host.

By reputation, it was a choice between Servint, WiredTree and KnownHost. I chose WiredTree out of curiosity and I was pleased to find out that their sign up process was a breeze. I did have to wait for several hours for the phone-call to verify my order since I had placed it outside of their office hours but overall it’s been smooth sailing so far.

I’m mildly surprised by the server’s speed. It’s much more snappier than the old Core 2 server that I was hosted on. To top it off, Wired Tree also offers free DNS hosting, a feature that had me sticking to my old host. Now it’s just a matter of waiting for the DNS to propagate and check things out.

I’ll open a ticket to support just to be sure that I did everything right.

uninitialized constant Rack::Test (NameError)

Cucumber-rails 0.4.0 was released for Rails 3.x. If you try to use it with anything below that (Rails 2.3.x), you’ll encounter the error:

uninitialized constant Rack::Test (NameError)

Is the rack-test driver missing? Nope. Well you still need that installed but the root cause of the breakage is lack of a proper test environment for Rails 2.3 whereas Rails 3.0 has this covered.

The solution? Simple.

Use an older version of cucumber. If you missed that from the first two sentences of the post then… :(.

For me it works using this configuration for the config/cucumber.rb file (that’s our environment file, you can also opt to place this in your test.rb file instead):

config.gem 'cucumber-rails',   :lib => false, :version => '0.3.2'

or if you’re using bundler:

gem "cucumber-rails", "~> 0.3.2"

or if you’re really lazy and don’t have anything to automatically install gems for your environment:

gem install cucumber-rails -v=0.3.2

Resize or reprocess all Paperclip attachments

Update: This post was written based on an older version of paperclip which didn’t have a default rake task for this.

I’ve been using the Paperclip plugin/ gem for image attachment in several of my apps and I’ve encountered a scenario wherein I needed to change one of the styles configured in the model to accommodate the client’s request. At that point, there are already a bunch of images in the project so I need to resize or in Paperclip’s terms reprocess all of them to fit the new configuration.

Here’s how I did it:

If you have paperclip installed as a plugin

If Paperclip is installed as a plugin, you can do this:

rake paperclip:refresh:thumbnails CLASS=Attachment

Just replace Attachment with whatever classname you are using for with Paperclip

If it’s installed as a gem, do this inside script/console:

Attachment.all.each {|s| s.logo.reprocess! if s.logo}

Just replace Attachment with whatever class name you are using for with Paperclip and logo with whatever attribute name you are using for the image.

And you’re done 🙂

New Macbooks are out

Apple’s 2011 refresh for it’s Macbook Pro line is out and wow am I excited to get one (read more via TechieXplorer).

I’m looking to get this SKU:

  • 2.0GHz quad-core Intel Core i7 or 2.2GHz quad-core Intel Core i7
  • 4GB 1333MHz RAM
  • 500GB 5400-rpm or 750GB 5400-rpm hard disk drive
  • Intel HD Graphics 3000 integrated graphics card
  • AMD Radeon HD 6490M with 256MB GDDR5 or AMD Radeon HD 6750M with 1GB GDDR5 dedicated GPU
  • Built-in battery (7 hours)

The price? $1799.  Pretty expensive yep, but I think I can manage with my current income – expenditures.  I just have to wait until I build my new gaming rig before shelling out cash for a new Macbook Pro

Autofit an image in a div using JQuery

I had a problem with one of my current projects where I was supposed to have an image enclosed in a div with fixed dimensions.

<div id="resize-div">
<img src="someimage.jpg", :id="resize-div">

For the dimensions of the div, you can set it inline or use css file:

#resize-div {

Now since not all images will fit this aspect ratio you’ll encounter problems easily enough. With images smaller than the div, I had little problem letting the css to handle it so it’s situated perfectly center in the div. I didn’t include it here to shorten the codes i need to display :).

Judging from the code shown above, you’ll not that if you have larger images that exceeded the div’s set height and width (especially those width different aspect ratios), a pure css solution simply won’t work (or at least I couldn’t make it work…. :/) so I thought of using JQuery to work it out for me.

Here’s my script to autofit images in a div using JQuery.

	var divElem = $("#resize-div");
	var maxht = divElem.height();
	var maxwdt = divElem.width();
	var imgElem = $("#resize-image");
	var ht = imgElem.height();
	var wdt = imgElem.width();
	if (ht < wdt){
		if (wdt > maxwdt)
			$("#resize-image").css({'height' : "auto", 'width' : maxwdt});
			$("#resize-image").css({'height' : "auto", 'width' : wdt});
	else if (ht > wdt){
		if (ht > maxht)
			$("#resize-image").css({'height' : maxht, 'width' : "auto"});
			$("#resize-image").css({'height' : ht, 'width' : "auto"});

Hurrah! A new server

I decided to move this blog together into a new VPS to host my personal rails projects :). Now, I’m not sure if this’ll be a permanent thing since I sort of jumped on the offer upon impulse but so far, I’m liking the service.

This server is running Ubuntu 10.4 with Pushion Passenger, Nginx, Ruby Enterprise Edition and PHP-FPM.

So why Nginx and not Apache? Well, aside from having it bundled with the latest version of Passenger, nginx historically has consumed less memory than Apache. The trade off of course is that I’ll still be running ruby 1.8.7 but I guess that’s fine since some of the plugisn I’ve been using have conflicts with 1.9.x.

I’ll be pushing a post detailing how I configured this server in a bit.

Looking forward to my camera for next year :)

I’ll be getting one of these next year and possibly an SB-800 (an SB-900 if my budget can still afford one).

While I know I should be thinking of investing in a full-frame, I think I should amass a healthy set of FX lenses before making the jump to full frame.