How to Run Ghost with Varnish Cache
#howto ,#tutorial #VPS – How to Run Ghost with Varnish Cache – This tutorial explains step by step guide (with pics) about How to Run Ghost with Varnish Cache
Ghost is really a phenomenal new kid in the block in term of blogging platform. It offers simplicity, elegant UI and straight to the point usability. I talked about it in some of my previous posts. The platform is surely getting so much attention from developers as well bloggers who are seeking another great, powerful and simple alternative to WordPress.
As a refresh, Ghost is a blogging script developed to run on Node.js while the Node.js itself does not require third-party web server. However it is good practice to not letting Node.js running alone in port 80 and let it handle production traffic. It will be better to put advanced frontend proxy software like what we did before with nginx. So it’s time to give Varnish Cache a shot replacing nginx in frontend part while keeping Node.js running in the backend.
This article explains how to setup Ghost blogging using Varnish Cache and Node.js on Ubuntu and CentOS vps.
..used in this tutorial:
- A VPS running CentOS / Ubuntu. I use CentOS 6 32-bit running on a 128MB* low end VPS by RamNode (SSD OpenVZ plan). You can use 64-bit especially if your box has more than 1024MB of RAM.
- Putty or Terminal on Mac and Linux to access your server.
- Basic knowledge about common bash command to manage a Linux vps.
- A cup of coffee or tea.
* Varnish can store cache files either in RAM or Disk. However if you plan to use RAM cache, so you better purchase a VPS with high RAM.
Step 1 – Open up Putty or Terminal then login to your server as root or any user with root / sudo privilege.
Step 2 – In order to have Ghost lives on your server, the main requirement is Node.js must be available there. So go ahead install Node.js following my previous guide.
Step 3 – Next, install Ghost on your server via this guide. The steps are clear enough but one thing to note for this tutorial: pay attention to Step 12 point 3. Do not replace port 2368 with 80. You can leave the port as it is or use common secondary web port like 81, 82, or 8080. In this guide I stay with port 2368 (default).
Also, in the host part, use 127.0.0.1.
Install Varnish Cache
Step 4 – Install Varnish Cache on your server. I posted a guide about it:
- How to install Varnish on Ubuntu (step 2)
- How to install Varnish on CentOS (step 3)
As for this tutorial, I gonna use this command in my CentOS vps:
rpm -Uvh http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release/varnish-release-3.0-1.noarch.rpm
Now this one to start installing Varnish:
yum install varnish
Ubuntu users should use apt-get install varnish but make sure you did read my previous guide.
Setup Varnish Cache
Step 5 – In CentOS, Varnish configuration file located in “/etc/sysconfig/varnish” while in Ubuntu it should be at “/etc/default/varnish“. You can use either vi or Nano editor to edit that file:
Step 6 – There will be several config parameters, please refer to my previous guide: CentOS here at how to configure Varnish section while Ubuntu is here at step 3. The main points are:
Change Varnish listen port to 80.
Change Varnish Storage Size to reasonable amount of your RAM or Disk.
In the VARNISH_STORAGE=, change file to malloc if you wish to use RAM-based caching. In this example I keep file because I want to use disk-based caching on my low end 128MB vps.
That’s it. Now save (Control+O) and exit (Control+X).
Step 7 – Now you can edit default Varnish .vcl file:
Now make sure in the host part is 127.0.0.1 while in the port use the one defined in your Ghost’s config.js file (see step 3 above).
Done? Save that file (Control+O) and exit (Control+X).
Step 8 – Now start Varnish service for the very first time
service varnish start
Step 9 – Then go to your Ghost directory and fire it up:
cd /var/www/ghost npm start --production
Step 10 – Now go ahead test it on your browser and congratulation!
And if you view your Ghost’s server headers, it should look similar like this:
X-Powered-By: Express Cache-Control: public, max-age=0 Content-Type: text/html; charset=utf-8 Etag: "-1869483144" Content-Length: 2817 Accept-Ranges: bytes Date: Mon, 27 Jan 2014 16:23:03 GMT x-varnish: 233293695 Age: 0 via: 1.1 varnish
That’s it and enjoy..
Varnish configuration above is pretty basic. For production environment, you can do some tweaking, optimizations and advanced modifications to Varnish default.vcl file to handle what to cache, what not, how long, how to handle cookies, etc. Need a good ready to use common (perfected) .vcl? Try this one from Mattias Geniar in Github. Or may be you developed your own .vcl specially for Ghost? Don’t hesitate to share it with us (me and many other readers). Happy blogging..
This post How to Run Ghost with Varnish Cache is part of ServerMom.