Saturday, February 4, 2012

Upgrading to Bluehost Pro Package

I am going to share my experience of hosting on bluehost for 1.5 years and why I decided to upgrade to their PRO package.

For the first year I had no problems with their shared hosting, everything worked great but as my site's traffic went up I started noticing small downtimes, which were quite minor and a few minutes is something I would not complain about since its a shared hosting and its was still within 99.5% uptime.

Their "unlimited" package is ofcourse limted, once you start getting high traffic you would often reach the SQL maximum concurrent connections limit and/or get slowed down by their CPU throttling mechanisms.
CPU Throttling Bluehost
Bluehost's CPU Throttling graph
I would say Bluehost can easily handle 1 page request per second (or around 80,000 pageviews/day) for a light-weight site with few queries, small page size, etc.  anything more than that and you would start hitting their SQL limits. This I feel is not bad for a shared hosting, but then from the past couple of months I started seeing more downtimes due to other sites hammering the server and database. At first I thought that their CPU throttling technology would prevent this from ever happening as it used to slow down my site in the past. But I was wrong as I was told by their support staff, I knew it from that moment that I had to get out of this shared server.

I had known about their PRO package before but did not know the details, so I found how it was different from the normal package:
5 GB Total MySQL Databases (up from 3 GB)
5 GB Single Database Size (up from 2 GB)
3000 Database Tables (up from 1000)
15 Mb/s Bandwidth (up from 8 Mb/s)
2000 Max Emails/Hour (up from 750)
More CPU time and server memory (they don't mention any details on this)

Interesting, but I didn't care much about it since I hardly required all these, all I was interested in was going to a server with fewer users or that which can handle more traffic. Well, I could be going with a VPS or Dedicated, but I am running a light-weight site and I think a shared hosting with a less crowded server should easily manage that. Most importantly I wanted minimum downtime and upgrading to their PRO package seemed easy and attractive.

So, finally I decided to upgrade, before starting the upgrade I did a backup and deleted the junk so the process of upgrading would be faster. I made the payment and after about 10 minutes my site went down.
Bluehost Pro Upgrade
Bluehost Upgrade in Progress Page
I was expecting a few hours downtime so I decided to do it at midnight but I was surprised when it took just a minute.
I checked that there were around 200 sites hosted on the same server, down from about 2000, not bad - 90% less crowded. Now, it's a PRO server and you can expect other sites to be high traffic sites as well, so now I am going to monitor for a few days and see if it's worth to upgrade.

Sunday, August 15, 2010

Twitter OAuth using PHP to make your own twitter web client

My last post on twitter was an introduction on how to use twitter, a lot of things have changed since then but that post just covers a few basics to get you started. This post is for developers on how to build your own twitter web client - which means instead of using twitter.com or any other client out there (echofon, chromed bird, TweetDeck, etc.) to tweet, you can make your own app to work with twitter. More specifically this post shows how to use OAuth to tweet using PHP.

OAuth (Open Authentication) is an authenticating protocol that allows internet users to approve an application to act on their behalf without the need for the user to share their password with the application. With OAuth the service provider (Twitter) issues tokens and it involves the exchange of tokens/keys and signing of requests thus making it a secure protocol. Twitter uses OAuth and Basic Auth (username and password used for authentication) but on 16-August-2010 Twitter is ending support for basic auth, which means all apps will have to start using OAuth.

Twitter OAuth Flow Chart

Implementing the OAuth protocol is a daunting task but luckily there are tons of libraries out there which makes life easy for us. Download abraham's twitteroauth library for PHP which I have used in the steps below.

Step 1: Register your application
Register a twitter application here and make sure to choose the settings as below:

Twitter Register App

Note down your CONSUMER_KEY and CONSUMER_SECRET, we will need this to identify our application to twitter.
Twitter Consumer Token

Step 2: Use Twitter OAuth Library for PHP
Copy the directory twitteroauth which have the two files (OAuth.php and twitteroauth.php) from abraham's twitteroauth source to your working directory.
Create two files index.php and callback.php in your working directory

index.php:
session_start();
require_once 'twitteroauth/TwitterOAuth.php';
define("CONSUMER_KEY", "xxx");
define("CONSUMER_SECRET", "xxx");
Replace "xxx" with the token from the last step.

Step 3: Get Request Token
Continuing the code from the last step, here we use our consumer tokens to get request tokens from twitter. Twitter returns request tokens for temporary use - oauth_token and oauth_token_secret which is unique for every request and we save these in session variables for use in the next step.

index.php:
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request_token = $connection->getRequestToken();
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];

Step 4: Send user for authorization
In this step we use our request tokens from the previous step to build the Authorization URL where the user will be redirected. The user is redirected to a page where if the user is not signed in it will be asked to sign in and then the user can grant or deny access to the application.

index.php:
$url = $connection->getAuthorizeURL($request_token);
header('Location: ' . $url);

Twitter Authorize Page

Twitter Authorize Page

Step 5: Exchange Request Token for Access Token
Once the user allows access in the previous step, they will be redirected to our callback page. Here twitter will return the oauth_token that we sent earlier, to make sure it is of the same session we compare this to the one we had earlier saved in session variable. Now we use the request tokens we got in Step 4 to exchange for Access token which is unique to every user (this token can be used for tweeting on behalf of the user for future requests).

callback.php:
session_start();
require_once 'twitteroauth/TwitterOAuth.php';
define("CONSUMER_KEY", "xxx");
define("CONSUMER_SECRET", "xxx");
if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) {
echo 'Session expired';
}
else {
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$token_credentials = $connection->getAccessToken();
Replace "xxx" with the token from the step 1

Step 6: Tweet on user's behalf
Finally we can use our access tokens from the previous step to tweet on behalf of the user
callback.php:
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $token_credentials['oauth_token'], $token_credentials['oauth_token_secret']);
$tweetmsg = 'Hello World, I am tweeting from my own twitter app!';
$result = $connection->post('statuses/update', array('status' => $tweetmsg));
$httpCode = $connection->http_code;
if ($httpCode == 200) {
$resultmsg = 'Tweet Posted: '.$tweetmsg;
}
else {
$resultmsg = 'Could not post Tweet. Error: '.$httpCode.' Reason: '.$result->error;
}
}

That's it! This is just the basics to get you started on building a full featured client. If you are migrating from basic auth to OAuth and have only a single user's account to manage, then in step1 you can choose the app type as client, get your access token from the My Access Token link after registering your app, skip steps 2,3,4,5 and go directly to step6 where you replace the token_credentials with your account's access token.

Saturday, August 7, 2010

Installing Windows 7 from a bootable USB drive

Installing Windows 7 from a USB drive is easy but you need to know that just copying the installation files from a DVD disc to a USB stick won't help, the installation media has to be bootable and the BIOS has to have the feature to boot from that media.

Installing the OS from a USB has its advantages - USB 2.0 is much faster than reading from a DVD disc making your installation faster. If your optical drive or your disc has stopped working you could always use your USB from one of the many USB ports in your desktop or laptop. If you don't have an optical drive you can use a USB drive but alternatively you could also get an external USB connected optical drive and use your disc as normal.

The following are the steps I found to make any USB drive bootable and to turn it into a Windows 7 installation media (Note: all these steps have been done on Windows 7, Vista will also work)

Requirements:
  • USB drive with atleast 4 GB of space
  • Windows 7 installation disc

Steps:
1. Insert your USB drive and format it by running the following commands in command prompt (running with administrator privileges):
diskpart
list disk
Shows the list of storage devices, identify your USB disk # by looking at the size.
select disk 1
disk # that you identified in the last command, mine was disk 1 yours can be different
clean
create partition primary
select partition 1
active
format fs=NTFS quick
assign
exit

2. Insert your Windows 7 disc and make the USB drive bootable by running the following commands again in command prompt:
F:
the drive letter of your optical drive, yours will be different
cd boot
bootsect /nt60 G:
the drive letter of your usb drive, yours will be different
exit
3. Copy all the files from the Windows 7 disc to your USB drive

That's it your USB drive is now bootable and ready to install windows 7! Make sure to set the motherboard BIOS to boot from your USB stick.