I know how frustration it is when you try to open a web page and it takes 10-15 seconds to load, so when you own a website its very much understandable why you want it to run fast and get pages loaded within 3-4 seconds.
I use wordpress and a couple of cache plugins to keep my website load fast but when I used Pingdom Website Speed Test to check the load time, it took 4 seconds to load, that is somewhat Okay but when I looked at the performance grade of 73, I knew that I can improve it a lot.
One of the areas where I got 0 score is “Parallelize downloads across domains” because all the dynamic (PHP) as well as static content (CSS, JS, Images) were getting loaded from my main domain which was causing it to load a bit slow.
So I thought of creating my own CDN cookie-less domain for all these static contents. Here I am providing steps to create own own self hosted CDN and setting it up in WordPress WP Cache plugin, that helped me in reducing load time to 2.30 seconds and performance grade of 83.
Create Subdomain for CDN
This is a simple step, just login to your hosting control panel, either cPanel or Plesk and create a subdomain with the name you want. The important thing to notice is that the directory location of the subdomain should be the same as your website. Check below image for better clarity.
Create CNAME for CDN domain
I use Hostgator VPS account and I have my own WebHost Manager, so creating a CNAME for me was very easy. Go to “Edit DNS Zone” and select your zone to edit. You might notice that there are few A names already created for the subdomain you just created. Just delete all of these and create only one CNAME similar to below image.
Cookie-Less CDN Domain
Usually WordPress and Google-Analytics sets a lot of cookies, so we need to do some config changes to make our main domain as the primary domain for cookies.
To set our main domain as primary domain for cookies for google analytics, add following code in the google analytics script.
For WordPress, open wp-config.php and add below configuration.
WP Super Cache CDN Settings
Before you proceed with this step, I would suggest you to load some static contents from your CDN subdomain and make sure it works fine.
This part is really easy, just go to CDN tab of WP Super Cache settings and enable CDN support by providing the CDN domain URL.
Thats it, we are using our own self hosted CDN cookie-less domain, after that I checked the performance of my website. Below image shows the loading time reduced from 4.08 seconds to 2.91 seconds that a lot of improvement.
But I noticed that my performance grade is still 73, it happened because all the static contents were getting loaded from my CDN domain. The solution was easy, I created two more subdomains for my CDN support and then updated WP Super Cache CDN settings to use multiple CDN servers. My final setting looks like below image.
Now when I run the website speed test again, I got a time of 2.30 (-1.78 seconds) seconds and performance grade of 83 (+10).