I’m running some of my sites on bluehost, it is stable and good until my traffic starts to increase. There is always error message about “mysql is gone away”. Apparently, they have set limits on the mysql connections. So I decided to install memcache
I got answer from bluehost tech support:
The Memcache php extension is installed on the servers and is located in /usr/lib64/php/modules/ (memcache.so is the name of the file) and all you really need to do is add the following line of code to the php.ini located in your public_html folder :
Also, make sure in your php.ini that your extension_dir is set to /usr/lib64/php/modules and that in cPanel under PHP Config you have the php5 setting set to PHP 5 (single php.ini) so that all subfolders will also recognize your public_html/php.ini settings, otherwise you’ll need to copy the php.ini into any folder that needs to use memcache.
I also found this thread from
Bluehost is a pretty good web hosting company, cheap and leaving you in total control of your website.
That is if you know your way around.
I highly recommend them for dirt cheap hosting yet access to powerful features.
A common complaint against any cheap hosting is the lack of memcached installation by default.
It really doesn’t make sense even business-wise for them because memcached would allow to use less CPU/Memory per site hosted, meaning less total servers, meaning less power drawn, meaning big savings.
The hosting plans charging usually a fixed amount per month or year, reducing CPU/Memory/Servers would really make sense as the savings would be on directly on them.
Anyway, now we need to setup memcached ourselves on a machine we don’t have root access to.
No big deal, we will just have to compile everything ourselves.
1/ Create an installation folder.
As we cannot write to /usr/, we cannot install any software system wide, so everything will remain local in our home folder.
Let’s call it ~/install/
It will be where you install all your compiled software, acting like the /usr/ folder we don’t have access to.
1 bash# mkdir ~/install
2/ Compile libevent
Why libevent? Because we will need it later to compile memcached.
1 bash# wget http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz
2 bash# tar zxf libevent-1.4.13-stable.tar.gz
3 bash# cd libevent-1.4.13-stable
4 bash# ./configure -prefix=~/install/
5 bash# make
6 bash# make install
libevent is now neatly installed in our home directory at ~/install/
3/ Compile memcached
1 bash# wget http://memcached.googlecode.com/files/memcached-1.4.4.tar.gz
2 bash# tar xzf memcached-1.4.4.tar.gz
3 bash# cd memcached-1.4.4
4 bash# ./configure –with-libevent=~/install/ –prefix=~/install/
5 bash# make
6 bash# make install
Neat, now we have memcached installed in ~/install/bin/, so let’s try to run it.
1 bash# cd ~/install/bin
2 bash# ./memcached
3 failed to set rlimit for open files. Try running as root or requesting smaller maxconns value.
4 bash# ./memcached -c 100
5 ^CSIGINT handled
So we hit a limitation: as we’re not root, our memcached server is limited to only 100 connections instead of the default 1024.
Maybe we can live with that.
Now the server part is done, we need to be able to use the PHP commands to communicate with memcached.
This is found in the PEAR packages.
4/ Install the PHP memcache client
(the memcache client doesn’t have the ‘d’ at the end)
1 bash# pecl install memcache
2 downloading memcache-2.2.5.tgz …
3 Starting to download memcache-2.2.5.tgz (35,981 bytes)
4 ……….done: 35,981 bytes
5 11 source files, building
6 running: phpize
7 Configuring for:
8 PHP Api Version: 20041225
9 Zend Module Api No: 20060613
10 Zend Extension Api No: 220060519
11 Enable memcache session handler support? [yes] :
12 Notice: Use of undefined constant STDIN – assumed ‘STDIN’ in CLI.php on line 304
14 Warning: fgets(): supplied argument is not a valid stream resource in CLI.php on line 304
16 Warning: fgets(): supplied argument is not a valid stream resource in /root/lib/php/PEAR/Frontend/CLI.php on line 304
17 building in /usr/local/src/pear/pear-build-bottoml8/memcache-2.2.5
18 ERROR: could not create build dir: /usr/local/src/pear/pear-build-bottoml8/memcache-2.2.5
Ouch, we’re not root so pecl fails.
No big deal: pecl already downloaded memcache-2.2.5.tgz for us, let’s compile it manually.
1 bash# tar xzf memcache-2.2.5.tgz
2 bash# cd memcache-2.2.5
3 bash# phpize
4 bash# ./configure –prefix=~/install/
5 bash# make
6 bash# make test
7 bash# make install
8 Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-20060613/
9 cp: cannot create regular file `/usr/lib/php/extensions/no-debug-non-zts-20060613/#[email protected]#’: Read-only file system
10 make: *** [install-modules] Error 1
12 bash# ls modules/
13 ./ ../ memcache.so*
14 bash# cp memcache.so ~/install/lib/
Ok, make install failed, but we have already what we need: memcache.so in the memcache-2.2.5/modules/ directory.
It’s the extension we need to load in PHP (in the php.ini file) so we can use the PHP commands to access memcached.
We just moved it to ~/install/lib/ so it’s comfy with the other libraries we compiled earlier (libevent and memcached).
5/ Load the memcache module for PHP
Now for the tricky part:
As we don’t have root access, we cannot just change the server’s php.ini file to include our freshly brewed memcache.so and restart Apache to make the changes taken into account.
But Bluehost provides us with a neat way to use our own php.ini file.
By default this option is disabled.
In their cPanel it’s listed under the category ‘Software / Services’ as ‘PHP Config’.
By default the setting is Standard PHP5, we need to change that to the third option: PHP5 (FastCGI)
It will directly load the php.ini it finds in your ~/public_html/ directory.
On this same page of the cPanel, there’s an interesting button “INSTALL PHP.INI MASTER FILE”.
Just click it and magically the server’s php.ini file is copied in ~/public_html/php.ini.default
It’s now our base php.ini we’ll use to load memcache. Let’s edit it.
1 bash# cd ~/public_html/
2 bash# mv php.ini.default php.ini
3 bash# vi php.ini
In php.ini, find the line that reads:
extension_dir = “/usr/local/lib/php/extensions/no-debug-non-zts-20060613″
and replace it with
extension_dir = “/home/your_bluehost_user_name/install/lib”
so that we have access to our memcache.so lib.
At the end if php.ini, before the last line that loads the Zend Optimizer, include this line to load memcache.so:
There should be some other libraries being loaded here:
As we changed the extension_dir path these libraries are not accessible anymore so you can just comment them.
extension_dir does not support listing multiple directories so I wish we didn’t have to do that.
At some point there was a proposal to make it happen, don’t know if it’s implemented yet, I couldn’t find anything about it.
Worst case scenario if you need PDO (and you probably do), you just need to compile it and install it in ~/install/lib/
Now that php.ini is modified, the server will pick up the configuration automagically.
But you may have to wait a little bit as php.ini will not be read again right away, you have to wait for the next poll, a few minutes.
6/ Let’s use memcached!
Now everything’s installed, let’s finally use memcached.
Create a memcache.php test file that will connect to memcached and just return the server version:
1 addServer(‘localhost’, 11211);
4 $memcache->connect(‘localhost’, 11211);
6 echo $memcache->getVersion();
Start the memcached in verbose mode so we can see our connection arriving:
1 bash# cd ~/install/bin/
2 bash# ./memcached -h
3 memcached 1.4.4
4 -p TCP port number to listen on (default: 11211)
5 -U UDP port number to listen on (default: 11211, 0 is off)
6 -s UNIX socket path to listen on (disables network support)
7 -a access mask for UNIX socket, in octal (default: 0700)
8 -l <ip_addr> interface to listen on (default: INADDR_ANY, all addresses)
9 -d run as a daemon
10 -r maximize core file limit
11 -u assume identity of (only when run as root)
12 -m max memory to use for items in megabytes (default: 64 MB)
13 -M return error on memory exhausted (rather than removing items)
14 -c max simultaneous connections (default: 1024)
15 -k lock down all paged memory. Note that there is a
16 limit on how much memory you may lock. Trying to
17 allocate more than that would fail, so be sure you
18 set the limit correctly for the user you started
19 the daemon with (not for -u user;
20 under sh this is done with ‘ulimit -S -l NUM_KB’).
21 -v verbose (print errors/warnings while in event loop)
22 -vv very verbose (also print client commands/reponses)
23 -vvv extremely verbose (also print internal state transitions)
24 -h print this help and exit
25 -i print memcached and libevent license
26 -P save PID in , only used with -d option
27 -f chunk size growth factor (default: 1.25)
28 -n minimum space allocated for key+value+flags (default: 48)
29 -L Try to use large memory pages (if available). Increasing
30 the memory page size could reduce the number of TLB misses
31 and improve the performance. In order to get large pages
32 from the OS, memcached will allocate the total item-cache
33 in one large chunk.
34 -D Use as the delimiter between key prefixes and IDs.
35 This is used for per-prefix stats reporting. The default is
36 “:” (colon). If this option is specified, stats collection
37 is turned on automatically; if not, then it may be turned on
38 by sending the “stats detail on” command to the server.
39 -t number of threads to use (default: 4)
40 -R Maximum number of requests per event, limits the number of
41 requests process for a given connection to prevent
42 starvation (default: 20)
43 -C Disable use of CAS
44 -b Set the backlog queue limit (default: 1024)
45 -B Binding protocol – one of ascii, binary, or auto (default)
46 -I Override the size of each slab page. Adjusts max item size
47 (default: 1mb, min: 1k, max: 128m)
49 bash# ./memcached -c 100 -vvv -l 127.0.0.1
50 slab class 1: chunk size 96 perslab 10922
51 slab class 2: chunk size 120 perslab 8738
52 slab class 3: chunk size 152 perslab 6898
53 slab class 4: chunk size 192 perslab 5461
55 slab class 40: chunk size 616944 perslab 1
56 slab class 41: chunk size 771184 perslab 1
57 slab class 42: chunk size 1048576 perslab 1
58 59 60 61
62 —- Access the memcache.php file here in your browser —-
64 65 66 29: going from conn_new_cmd to conn_waiting
67 29: going from conn_waiting to conn_read
68 29: going from conn_read to conn_parse_cmd
69 29: Client using the ascii protocol
70 29 VERSION 1.4.4
72 29: going from conn_parse_cmd to conn_write
73 29: going from conn_write to conn_new_cmd
74 29: going from conn_new_cmd to conn_waiting
75 29: going from conn_waiting to conn_read
76 28: going from conn_new_cmd to conn_waiting
77 28: going from conn_waiting to conn_read
78 28: going from conn_read to conn_closing
Tada! And magically the browser is displaying a magnificent “1.4.4“.
You got yourself a memcached server running on BlueHost.