Be careful of MySQL Query Cache

Configuring MySQL server for high traffic site is very hard. You have to consider many things. One of the pitfall for unweary person like me is query_cache_size. I must confess I thought it would make my mysql server would perform better with query_cache_size. So I gave it 400M.

query_cache_limit = 400M

But I was dead wrong.

The problem was when ever you run INSERT/UPDATE sql, MySQL gets rid of all of the cache. It also locks the Table. This is one of the main features which limits query cache effectiveness.

If you see “Waiting for query cache lock” when you run SHOW ENGINE INNODB STATUS\G. Then you know you have to set it 0.

For more details

11. September 2014 by Zakir Hyder
Categories: Linux, MySql, Server Management | Tags: , , | Comments

Prevent cronjobs from overlapping in Linux

Recently while fixing a bug, I found out that there are multiple copies of same cronjob running at the same time. Normally, the task completes in just a few seconds. If the task takes longer than a minute, however, we end up with multiple copies running at once. This situation can make havoc in you app. Its better to introduce some kind of locking mechanism. flock(1) is the right tool for that.

*/1 * * * * /usr/bin/flock -n /tmp/cronjob.lockfile /usr/bin/php /var/www/some.php >> /root/cronjob.txt 

The -n option tells flock not to wait for the lock, but to exit. Simply this one liner made my life way easier.

14. August 2014 by Zakir Hyder
Categories: Linux | Tags: , , , | Comments

Time Synchronisation with NTP in Ubuntu Server

Recently during some bug fixing, I found out that our database server’s time is 5 minutes off. First I thought probably the timezone was correct. Then I found out that for Ubuntu server you have to synchronisation the time. Every time server restarts, it update it’s time using ntpdate. You can do it using following code


You can setup ntp, which will automatically update server’s time.

sudo apt-get install ntp

Then edit /etc/ntp.conf to add/remove server lines. By default these servers are configured:

# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See for
# more information.

You can update these server according to your time zone from After you changing the conf file, reload the ntp

sudo /etc/init.d/ntp reload

11. August 2014 by Zakir Hyder
Categories: Linux, Ubuntu | Tags: , , , , | Comments

Fixing Varnish’s 503 Error

I have been trying to speed up Among many other things, I am trying to add Varnish cache. While setting Varnish on our test server, I have been facing 503 error. Basically this means that backend not responding correctly or taking more time.

This is what I got from varnishlog -c -m TxStatus:503

varnishlog -c -m TxStatus:503
3 VCL_call     c miss fetch
3 Backend      c 12 default default
3 FetchError   c http format error
3 VCL_call     c error deliver
3 VCL_call     c deliver deliver
3 TxProtocol   c HTTP/1.1
3 TxStatus     c 503

After many trail and error, I finally figure out the problem. Here is the solution worked for me.

First I tried to add .first_byte_timeout, .connect_timeout & .between_bytes_timeout in /etc/varnish/default.vcl because I know backend takes 1-2 secs to load.

backend default {
	.host = "";  # IP address of your backend (Apache, nginx, etc.)
	.port = "8080";       # Port your backend is listening on
	.connect_timeout = 10s;
	.first_byte_timeout = 20s;
	.between_bytes_timeout = 10s;

Still the problem was not solved. Then I tried to add http_max_hdr. Default value http_max_hdr is 64. I increased it to 4 folds and added to /etc/default/varnish

DAEMON_OPTS="-a :80 \
    -T localhost:6082 \
    -f /etc/varnish/default.vcl \
    -u www-data -g www-data \
    -S /etc/varnish/secret \
    -p http_max_hdr=256 \
    -s malloc,2G"

After that 503 went away :D.

12. May 2014 by Zakir Hyder
Categories: Caching, Varnish | Tags: , , , , , , | Comments

Setup Sub Domain in OSX 10.9 Mavericks

First of all, setup Apache, MySQL, PHP in you mac. You can do that by following

Edit your /private/etc/hosts file

sudo nano /private/etc/hosts

It should look like this

If you put in you browser you will see “It works”. We have to update our /private/etc/apache2/httpd.conf to allow subdomains

sudo nano +477 /private/etc/apache2/httpd.conf

Uncomment Include /private/etc/apache2/extra/httpd-vhosts.conf line. It should look like this

Now we have to edit httpd-vhosts.conf to point document root for

sudo nano  /private/etc/apache2/extra/httpd-vhosts.conf

then add the following

<VirtualHost *:80>
    DocumentRoot "/Users/zakirhyder/Sites/swi"
    ErrorLog "/private/var/log/apache2/"
    UseCanonicalName Off

Now restart apache and your subdomain is ready.

sudo apachectl restart

06. May 2014 by Zakir Hyder
Categories: Apple, Server Management | Tags: , , , , | Comments

Implement Twitter OAuth 1.0A in Laravel using OAuth 1 Lib

If you want to integrate twitter API in your web app, you will find a lot of php libraries which can help you do that. You can see 11 of them in All the libraries uses OAuth. Some of the libraries are specifically developed for twitter only. If you want to add other OAuth enabled API sites like Xing, tumblr, Pocket,, skyrock in your web app then have to add one library for each of them of atleast update the libraries so that it can work with multiple networks. Instead of doing that you can use OAuth 1 Lib and Guzzle to implement different networks in you web app.

We are going to use and In this post I am gong to integrate Twitter. I am going to add more networks in coming weeks. So keep in your watch.

First we need to add OAuth 1 Lib in our Laravel app. If you go to then you can see how easy it is to add github in library Laravel app. But If you do not want to read whole post then just add following in composer.json

"repositories": [
            "type": "vcs",
            "url": ""
"require": {
		"laravel/framework": "4.1.*",
		"zakirhyder/oauth-1-lib": "dev-master"


composer dump-autoload

Before we start, Lets me show you how I configured the Laravel app. First I have created an environment for my MBP according to You have to changed it according to your computer name. The full code base is here Then I have added env.local.php file where The Twitter app’s key and secret is saved.

return array(
    'twitter_client_key'     => '',
    'twitter_client_secret' => '',

Then these values are added credential.php

return array(
    'twitter_client_key'     => $_ENV['twitter_client_key'],
    'twitter_client_secret' => $_ENV['twitter_client_secret'],
    'twitter_base_url' => '',

I added Bootstrap 3.0 in this laravel app. For layout I used I added main.blade.php file in app/views/layouts folder. I added hello.blade.php. Its look like this

Now the interesting parts so when you click the “Click here To Auth & Tweet” button – it will redirect you to TwitterProfileAccountsController‘s getCreate function. I am not going to post the whole code here but you if you want you can open it in another tab and go through as I explain every thing.

First I removed twitter_oauth_token,twitter_oauth_token_secret and twitter_request_token from session. First two are need to get access token from twitter. I am saving the access_token in session using twitter_request_token name. The $redirect_url is the url twitter sends the user after the user authorise the app. Then app will redirect the user to getSaveTwitterProfile function. $request_token_url is the url where app will send a get request to get temporary oauth_token and oauth_token_secret.

$consumer = new OAuth\OAuthConsumer($consumer_key, $consumer_secret);
$request = OAuth\OAuthRequest::from_consumer_and_token($consumer, NULL,"GET", $request_token_url, $args);
$request->sign_request(new OAuth\OAuthSignatureMethodHMACSHA1(), $consumer, NULL);
$url = $request->to_url();

The purpose of these lines is to build a url to send request to twitter server. After The url is build, app sends a get request using Guzzle.

$client = new Guzzle\Http\Client($url, array(
	'request.options' => array(
		'verify' => false
$response = $client->get()->send();

If the request is successful, we will get oauth_token and oauth_token_secret from the response. We will save them in session. Then app will redirect user to twitter with oauth_token

Session::put('twitter_oauth_token', $request_token['oauth_token']);
Session::put('twitter_oauth_token_secret', $request_token['oauth_token_secret']);
return Redirect::to("{$request_token['oauth_token']}");

After user authorise the app Twitter will redirect the user to getSaveTwitterProfile() function which is the $redirect_url in getCreate function. First app will check the oauth_token value with session’s oauth_token. The app will build another url to get the access token

$consumer = new OAuth\OAuthConsumer($consumer_key, $consumer_secret);
$token = new OAuth\OAuthConsumer($oauth_token, $oauth_token_secret);
$request = OAuth\OAuthRequest::from_consumer_and_token($consumer, $token,"GET", $access_token_url, $args);
$request->sign_request(new OAuth\OAuthSignatureMethodHMACSHA1(), $consumer, $token);
$url = $request->to_url();

Then app sends a get request to get the access tokens

$client = new Guzzle\Http\Client($url, array(
	'request.options' => array(
		'verify' => false
$response = $client->get()->send();

If the request was successful we will get json encoded access token and we will save it session as twitter_request_token.

$response_body = $response->getBody();
$request_token = OAuth\OAuthUtil::parse_parameters($response_body);
Session::put('twitter_request_token', $request_token);

Now app has the user’s access token, so app can post a tweet in user’s profile. The pricedure is same as before. App Will build a url. But this time app will add a parameter which is our tweet and app will send request as POST.

$args['status'] = "#laravel #oauth #Guzzle";
$consumer = new OAuth\OAuthConsumer($consumer_key, $consumer_secret);
$token = new OAuth\OAuthConsumer($request_token['oauth_token'], $request_token['oauth_token_secret']);
$request = OAuth\OAuthRequest::from_consumer_and_token($consumer, $token,"POST", $post_url, $args);
$request->sign_request(new OAuth\OAuthSignatureMethodHMACSHA1(), $consumer, $token);
$url = $request->get_normalized_http_url();
parse_str($request->to_postdata(), $post_data);

Now app will post the request

$client = new Guzzle\Http\Client();
$request = $client->post($url, false, $post_data);
$request->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false);
$response = $request->send();

BTW you should not use CURLOPT_SSL_VERIFYPEER=false in production server. See for reason.

Now on you twitter profile you will see “#laravel #oauth #Guzzle” tweet.

01. May 2014 by Zakir Hyder
Categories: Laravel, Library, PHP, Web Development | Tags: , , , , , , , , , , | Comments

Add your own github library in Laravel using composer

Adding a random library is quite easy in Laravel. There are two ways you can add an library. Both ways use composer. I am going to use as our example library. It is basically with OAuth namespace and some other changes.

The Easiest Way

Download the oauth-1-lib. Then create folder in app/. Lets name it “library”. So the path to library folder is app/library. Put the src folder of oauth-1-lib in library folder. Rename src folder to “OAuth”. If you want can rename to whatever you want. Then add the library path in the autoload section of composer.json file.

"autoload": {
	"classmap": [

Then run the dump-autoload command

composer dump-autoload

Now you can use it in you project.

$consumer = new OAuth\OAuthConsumer($consumer_key, $consumer_secret);

The Cool Way

We will composer’s VCS. But before that we have to add a composer.json in the library. We create a composer.json file and add the following.

    "name": "zakirhyder/oauth-1-lib",
    "description": "This library includes all the classes needed for OAuth 1/1.0a",
    "license": "MIT",
    "authors": [
            "name": "Zakir Hyder",
            "email": ""
    "minimum-stability": "dev",
    "require": {
        "php": ">=5.3.0"
    "autoload": {
        "psr-0": {
            "OAuth": "src/"

Now lets add our library in a laravel project. First we will add repository laravel’s composer.json

"repositories": [
            "type": "vcs",
            "url": ""

Then we add our library in require key

"require": {
		"laravel/framework": "4.1.*",
		"zakirhyder/oauth-1-lib": "dev-master"

One thing about name -I made mistake thinking “zakir-hyder/oauth-1-lib(from the url of” is the name I have use. But No I have to use name I used in library’s composer.json.

26. April 2014 by Zakir Hyder
Categories: Composer, Laravel, PHP | Tags: , , , | Comments

← Older posts

Newer posts →