Sentora Support Forums

Full Version: Setup Varnish Cache with VHosts (Ubuntu 14.04) 64bit
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
[Image: Ubuntu-14.04_64bit-orange.svg?style=flat-square] [Image: Varnish-4.0.3-lightgrey.svg?style=flat-square] [Image: Sentora-1.0.0-blue.svg?style=flat-square] 
How to Setup Varnish Cache with VHosts
Learn how to install and configure Varnish 4 with Sentora and Virtual Hosts.

[Image: 90x60.png]
What's Varnish?
Varnish Cache is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents. Varnish Cache is really, really fast. It typically speeds up delivery with a factor of 300 - 1000x, depending on your architecture.
read more at the varnish-cache.com about page or watch a quick video about varnish (2min)


System Requirements
Make sure you have the following:
  • Root Access.
  • 64bit OS (this guide is only for Ubuntu 14.04, but Varnish can be installed on other systems).
  • 1Gb of RAM is recommended.
more details on the official varnish-cache.com requirments page


Installation
Login to your server via SSH, make sure you are logged in as root.

Enter the following commands in the Terminal:
Code:
apt-get install apt-transport-https curl https://repo.varnish-cache.org/GPG-key.txt | apt-key add - echo "deb https://repo.varnish-cache.org/ubuntu/ trusty varnish-4.0" >> /etc/apt/sources.list.d/varnish-cache.list apt-get update apt-get install varnish


Setup Varnish
Installing was easy, not let's get our hands dirty and begin the fun part.
I will try to avoid server downtime, however keep in mind that you will need to restart apache at least once.

By default Varnish listens on port 6081 and uses the backend (apache) on port 8080;
In a production server we want Varnish to listen on port 80.


Test Drive Varnish (optional step) 
Let's just verify if Varnish is up and running, to do this we need to change the default.vcl file.
Open /etc/varnish/default.vcl on your favorite text editor and change the .port to 80 (should be line 18):
PHP Code:
# Default backend definition. Set this to point to your content server.
backend default {
 
   .host "127.0.0.1";
 
   #.port = "8080";
 
   .port "80";

see the default default.vcl file at the varnish GitHub Repository

Restart the Varnish service to apply the changes (execute the command on the terminal):
Code:
service varnish restart

Now open your website on port 6081 (http://domain.tld);
If Varnish is running correctly you should see your website, for further confirmation you can check the website headers (link to header-checker tool) for the  information Via => 1.1 varnish-v4.

Now that we tested it is working revert your default.vcl file back to.port 8080 (no need to restart varnish service right now).


Change Varnish Listening Port
First we will change the Varnish listen port from 6081 to 80;
Open /etc/varnish/default.vcl on your favorite text editor and change the DAEMON_OPTS="-a to 80 (~ line 40):
PHP Code:
#DAEMON_OPTS="-a :6081 \
DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m" 


Change Apache Port
Now we need to switch apache to port 8080;
Let's use the setso --set command that Sentora provides to easily change settings;
The first command changes the global apache port and the second one changes the Sentora Panel port.
Code:
setso --set apache_port 8080 setso --set sentora_port 8080


Launch Varnish Smile 
Login to Sentora Control Panel as zadmin and on Module Manager select Apache Config and switch the Apache Port from 80 to 8080 scroll to the end of the page and check the Force Update box to ensure the vhosts file will be updated on the next Daemon Run.

WARNING: After you Save the settings your server will be switched to port 8080, this means that every website in your server will not be accessibleTo minimize downtime we need to make sure that the following commands are done quickly.

Let's manually trigger the sentora Daemon and restart Varnish to apply the changes:
Code:
php -q /etc/sentora/panel/bin/daemon.php service varnish restart
If everything went ok, without errors, Varnish should now be in front of your server (on all VHosts).
Verify your website (normal url, no port required) and check the website headers (link to header-checker tool) for the  information Via => 1.1 varnish-v4.




Last Comments
Not being hard to install and use, Varnish is a advanced tool and should be used with care.

Soon I will create a new guide about basic VCL scripting and how you can use your VCL files to cache only some websites on your Sentora Server.




If this guide helped you, please press the Thanks button and consider giving me some Positive Reputation using the Rate button.
I'll be happy  Wink
Hello,

Just installed !

Perfect just one thing this code come in /etc/default/varnish not in /etc/varnish/default.vcl
PHP Code:
#DAEMON_OPTS="-a :6081 \
DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m" 
(11-01-2015, 05:49 AM)Ilia Wrote: [ -> ]Hello,

Just installed !

Perfect just one thing this code come in /etc/default/varnish not in /etc/varnish/default.vcl
PHP Code:
#DAEMON_OPTS="-a :6081 \
DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m" 

Probably because you are on CentOS Smile
lol no on ubuntu !
(11-01-2015, 10:38 PM)Ilia Wrote: [ -> ]lol no on ubuntu !

Then update your profile with your correct server info... Angel
Lol no sorry again !

My production server is on Zpanel Centos !

The one where i installed varnish is on Sentora Ubuntu !

To comeback to the subject as show in the github link of default.vcl it stops at line 40

and modificaton asked is in line 80.

lol
(11-02-2015, 08:29 AM)Ilia Wrote: [ -> ]Lol no sorry again !

My production server is on Zpanel Centos !

The one where i installed varnish is on Sentora Ubuntu !

To comeback to the subject as show in the github link of default.vcl it stops at line 40

and modificaton asked is in line 80.

lol

Hmmm, that's the problem with 'external' repositories, they may have changed something Sad
If you experience some flaws with your panel and certain modules you can skip de panel via varnish by adding this code in default.vcl:

Code:
sub vcl_recv {    if (req.http.Host == "subdomain.domain.com") {        return (pipe);    } }


No need to use if your panel is via SSL.
@[bbspike] nice touch!! Thank you!
I tried it but after setting it up I can't access the websites using varnish. Hmm. But I can access on port 8080. So I reverted it all back to defaults. Hmm. I'm using 32bit os because their website said it should work.
Pages: 1 2