Friday, July 26, 2013

WTF GoDaddy and Optimizing My Sites on Another ISP - DreamHost

One of the websites I have been working on was hosted at GoDaddy. I had a hosting plan there for years. several weeks ago, I had requested SSH shell access to my account. For some reason, GoDaddy had to move my hosted web account to another server in order to give me SSH access.
Well, the move itself was not smooth. Turns out that one of my MySQL databases was running on an older MySQL server that was not supported on the newer host. So the migration ended up stuck in limbo. I found out that it was the database issue after I contacted GoDaddy support regarding the migration taking such a long time. They told me to upgrade the database. The only problem was, with the migration stuck in limbo, I couldn't access the databases from their control panel. I contacted their support again and explained the situation and told them just to delete the database because I didn't need that one.
Well, that was too complicated for their "level 1" support, so they kicked it up to "level 2" support, where they upgraded the database, completed the migration, and fixed up all my PHP configuration files to point to the shiny new databases on the new database. All seemed to be good.
The first time I tried to visit a site on the new server, all I got was one of those server error messages showing up in my browser where my site was supposed to be. After retrying the various sites that I have hosted their, the websites finally started appearing normally. I figured it must have been some transient temporary issue that occurred after the move to the new server.
However, later I noticed that when I was in meetings with the groups that I created the websites for, that when I tried to pull up a website during the meeting, I was regularly disappointed by how slow the sites would load. They were loading very slowly, which made it difficult to discuss them during the meetings.
Later, I used the free online analyzing tool, WebPageTest. It will give you real-world timings of all the aspects of loading a web page. And Wow. I couldn't believe how poor the performance was on the Godaddy server. It was taking more than 10 seconds just to get the first byte of response from the initial website request! Also, it was taking sometimes over a minute for the site to completely finish loading! There must have been something terribly wrong with the new server. So I contacted Godaddy support again.
I had explained to them in the new ticket I submitted in detail what the issue was, what timings I was getting, and that I would periodically get server error messages. The response was that I should "upgrade" from a shared plan to a "hosted" plan for better performance, a "level 1" response I presume, especially given that I was already on a "hosted" plan.
Then it came to me - I need to move my sites to another ISP. I have sites on BlueHost which are doing well. I also have a site on an Amazon EC2 micro server that screams. However, I noticed that DreamHost has a pretty good deal on virtual private servers (VPS) that have unlimited bandwidth and storage. I also like DreamHost because they are carbon neutral and employee owned. When you sign up, you also get a week for free on a VPS. Their VPS is also configurable on a sliding scale, literally. There is a slider control where you can dynamically change the amount of memory your server has, which also changes the amount that you are billed. So, you only pay for what you use.
The DreamHost VPS is not as fast as an Amazon EC2 instance, but you get easy management through DreamHost's very own custom control panel. This makes it easy to set up multiple websites on your VPS, whereas on Amazon EC2, you have to do all the management yourself through SSH, or whatever applications you install.
So, I chose to go with DreamHost, because the performance is good, and I can perform optimizations and use a cloud service to get even greater performance. I began by copying my site files from my GoDaddy server to the DreamHost server. I have many media files on one site, which would take a long time to download and then re-upload again. To speed the process up, I just connected via SSH to my new DreamHost server and used SCP to directly copy the files from my GoDaddy server. This worked like a charm. Then, I backed up my databases on GoDaddy and imported them into new databases at DreamHost. I got the sites all up and running and already they were performing much much better than they were with GoDaddy.
Now for the optimizing. I ran WebPageTest on one of my sites and this is what I got:
As you can see, it got some F's in some of the ratings. It also has an "X" for CDN which means that I am not using any cloud services for distributing static content. The first thing I did was to optimize some of the JPG images that I have on the site. The second step was to add some code to the ".htaccess" file on the server that is in the root website folder. I added these lines:

<IfModule mod_expires.c>
     # Activate mod_expires for this directory
     ExpiresActive on

     # locally cache common image types for 7 days
     ExpiresByType image/jpg "access plus 7 days"
     ExpiresByType image/jpeg "access plus 7 days"
     ExpiresByType image/gif "access plus 7 days"
     ExpiresByType image/png "access plus 7 days"
     ExpiresByType image/x-icon "access plus 7 days"

     # cache CSS, js, and other files
     ExpiresByType text/css "access plus 7 days"
     ExpiresByType application/x-javascript "access plus 7 days"
     ExpiresByType application/javascript "access plus 7 days"
     ExpiresByType text/javascript "access plus 7 days"
     ExpiresByType application/x-shockwave-flash "access plus 7 days"
</IfModule>

#Gzip
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css
  AddOutputFilterByType DEFLATE application/x-javascript application/javascript
  AddOutputFilterByType DEFLATE text/javascript image/x-icon
</IfModule>
#End Gzip

The first set of lines enables caching of much of the static content on the client browsers computer. This speeds up browsing in that the browser only has to download the static content (images, etc.) once, instead of with each page load. It is important to note that if you are working on the site and are editing images, javascript files or other files that are cached, you will want to turn off caching in order to make it easier to test your changes.
The second group of lines (under Gzip) tells the server to compress certain file types, which decreases the time that it takes for the browser to download these files. All of the standard, compressible file types are listed there. I also added the "image/x-icon" types. I wanted to provide a site icon file that supports higher resolution images. These icon files with higher resolutions can become large fast, because they are essentially raw bitmap files. The good news is that they are highly compressible, so by adding it to the "Gzip" list, you don't have to worry so much about the size.
So, now for another test:
The scores are much better here. 5 seconds for the first load is still a bit long. I might be able to shave that a bit more by installing a Joomla extension which combines the javascript and stylesheet files into one javascript file and one css file so that the browser only has to perform one download request for each type. However, I decided for now to move straight to enabling a cloud service. DreamHost supports easy integration with a free (yes, that's right - free) cloud service called CloudFlare. CloudFlare is ridiculously easy to use, especially with DreamHost. To enable it, just "edit" your "domain" for your website and turn it on:
That is all there is too it. You have to wait several hours for it to fully take effect. I waited until the next day to run another test:
Look at that! First view is now down to 2.4 seconds. Note that the "CDN" score now has a check mark in it. I am quite happy now with the performance improvements I was able to obtain.
So, in conclusion, you can dump GoDaddy, move to a smaller ISP, such as DreamHost, and get near enterprise performance for your website for low cost. You don't have to dump more money into GoDaddy in an attempt to get reasonable service.