
When dealing with high traffic Magento installations you definitely want to implement caching mechanisms to ensure proper response times for your visitors. Apart from internal caching and indexing functionality provided by Magento there exist a couple of (external) extensions that help you speed of content delivery by e.g. caching entire pages, such as PageCache for Varnish.
Simply put, Varnish represents a reverse-proxy cache that serves as entry-point into you Magento installation and returns cached content rather than requesting Magento to re-build entire pages.
The following steps show you can setup Varnish cache server for Magento 1.9 CE on Mac OSX.
First off, make sure you have Homebrew installed and configured properly. Based on Homebrew we can easily setup Varnish by issuing the following command:
bash-3.2$ brew install varnish ==> Installing varnish dependency: pkg-config ==> Downloading http://pkgconfig.freedesktop.org/releases/pkg-config-0.28.tar.gz ######################################################################## 100,0% ==> ./configure --prefix=/usr/local/Cellar/pkg-config/0.28 --disable-host-tool --with-internal-glib --with-pc-path=/usr/local/lib/p ==> make ==> make check ==> make install /usr/local/Cellar/pkg-config/0.28: 10 files, 600K, built in 97 seconds ==> Installing varnish dependency: pcre ==> Downloading ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.bz2 ######################################################################## 100,0% ==> ./configure --prefix=/usr/local/Cellar/pcre/8.33 --enable-utf8 --enable-unicode-properties --enable-pcregrep-libz --enable-pcregrep-libbz2 -- ==> make ==> make test ==> make install /usr/local/Cellar/pcre/8.33: 140 files, 4,1M, built in 49 seconds ==> Installing varnish ==> Downloading http://repo.varnish-cache.org/source/varnish-3.0.3.tar.gz ######################################################################## 100,0% ==> ./configure --prefix=/usr/local/Cellar/varnish/3.0.3 --localstatedir=/usr/local/var ==> make install ==> Caveats To have launchd start varnish at login: ln -sfv /usr/local/opt/varnish/*.plist ~/Library/LaunchAgents Then to load varnish now: launchctl load ~/Library/LaunchAgents/homebrew.mxcl.varnish.plist Warning: /usr/local/sbin is not in your PATH You can amend this by altering your ~/.bashrc file ==> Summary /usr/local/Cellar/varnish/3.0.3: 51 files, 1,8M, built in 43 seconds
As shown in Brew’s output you can start Varnish by issuing the following command:
==> Caveats To have launchd start varnish at login: ln -sfv /usr/local/opt/varnish/*.plist ~/Library/LaunchAgents Then to load varnish now: launchctl load ~/Library/LaunchAgents/homebrew.mxcl.varnish.plist
In case the following error occurs:
bash-3.2$ sudo launchctl load ~/Library/LaunchAgents/homebrew.mxcl.varnish.plist" launchctl: Couldn't" stat("~/Library/LaunchAgents/homebrew.mxcl.varnish.plist"): No such file or directory nothing found to load
try a re-install first (and additionally do a brew-update/updgrade):
bash-3.2$ sudo brew uninstall varnish Uninstalling /usr/local/Cellar/varnish/3.0.3...
After a brew update/upgrade Varnish 4.0.0 is now being installed:
bash-3.2$ sudo brew install varnish ==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/varnish-4.0.0.mavericks.bottle.tar.gz ######################################################################## 100,0% ==> Pouring varnish-4.0.0.mavericks.bottle.tar.gz ==> Caveats To have launchd start varnish at login: ln -sfv /usr/local/opt/varnish/*.plist ~/Library/LaunchAgents Then to load varnish now: launchctl load ~/Library/LaunchAgents/homebrew.mxcl.varnish.plist ==> Summary /usr/local/Cellar/varnish/4.0.0: 93 files, 1,9M
Unfortunately, this does not fix the missing plist file problem every time.
Nevertheless, you can easily create the plist file yourself, as mentioned here https://github.com/Homebrew/homebrew/issues/15922:
Simply copy the contents for the plist file by calling brew cat varnish:
bash-3.2$ sudo brew cat varnish require 'formula' class Varnish < Formula homepage 'http://www.varnish-cache.org/' url 'https://repo.varnish-cache.org/source/varnish-4.0.0.tar.gz' sha1 '6ad4fcf42a505a748ae993cb6ed579159e023633' bottle do sha1 "190bba2cf0e521b489cbd64cc698ba769b18cccd" => :mavericks sha1 "fd12d468c2328179bd402f1592b65ef69d6f1baa" => :mountain_lion sha1 "0e9717e705541a34906947eb7e49ab800204e2ff" => :lion end depends_on 'pkg-config' => :build depends_on 'pcre' resource "docutils" do url "https://pypi.python.org/packages/source/d/docutils/docutils-0.11.tar.gz" sha1 "3894ebcbcbf8aa54ce7c3d2c8f05460544912d67" end def install ENV.prepend_create_path "PYTHONPATH", buildpath+"lib/python2.7/site-packages" resource("docutils").stage do system "python", "setup.py", "install", "--prefix=#{buildpath}" end system "./configure", "--disable-dependency-tracking", "--prefix=#{prefix}", "--localstatedir=#{var}", "--with-rst2man=#{buildpath}/bin/rst2man.py", "--with-rst2html=#{buildpath}/bin/rst2html.py" system "make install" (var+'varnish').mkpath end test do system "#{opt_sbin}/varnishd", "-V" end def plist; <<-EOS.undent <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>#{plist_name}</string> <key>ProgramArguments</key> <array> <string>#{opt_sbin}/varnishd</string> <string>-n</string> <string>#{var}/varnish</string> <string>-f</string> <string>#{etc}/varnish/default.vcl</string> <string>-s</string> <string>malloc,1G</string> <string>-T</string> <string>127.0.0.1:2000</string> <string>-a</string> <string>0.0.0.0:80</string> </array> <key>KeepAlive</key> <true/> <key>RunAtLoad</key> <true/> <key>WorkingDirectory</key> <string>#{HOMEBREW_PREFIX}</string> <key>StandardErrorPath</key> <string>#{var}/varnish/varnish.log</string> <key>StandardOutPath</key> <string>#{var}/varnish/varnish.log</string> </dict> </plist> EOS end end
The plist content in this case is:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>#{plist_name}</string> <key>ProgramArguments</key> <array> <string>#{opt_sbin}/varnishd</string> <string>-n</string> <string>#{var}/varnish</string> <string>-f</string> <string>#{etc}/varnish/default.vcl</string> <string>-s</string> <string>malloc,1G</string> <string>-T</string> <string>127.0.0.1:2000</string> <string>-a</string> <string>0.0.0.0:80</string> </array> <key>KeepAlive</key> <true/> <key>RunAtLoad</key> <true/> <key>WorkingDirectory</key> <string>#{HOMEBREW_PREFIX}</string> <key>StandardErrorPath</key> <string>#{var}/varnish/varnish.log</string> <key>StandardOutPath</key> <string>#{var}/varnish/varnish.log</string> </dict> </plist>
Now starting Varnish via launchctl succeeds:
bash-3.2$ sudo launchctl load ~/Library/LaunchAgents/homebrew.mxcl.varnish.plist
Time to test the service
Open a browser and navigate to 127.0.0.1:8080 (which is the port Varnish is listening to set by the configuration in the plist file from above) and check out the headers:
Accept-Ranges bytes, bytes Age 0 Connection keep-alive Content-Language de Content-Length 1087 Content-Type text/html; charset=utf-8 Date Fri, 30 May 2014 07:31:12 GMT Server Apache/2.4.4 (Unix) PHP/5.4.7 OpenSSL/1.0.1e Vary accept-language,accept-charset Via 1.1 varnish X-Varnish 1661470811
Congratulations, as you can see Varnish is working properly!
The next step is to configure Apache to listen to port 8080 (or any other port apart from 80) and Varnish on port 80.
That’s it 🙂