BigDropGR
BigDropGR

Reputation: 345

Install and Configure varnish cache on Ubuntu server with VestaCP

I would like to install and configure Varnish Cache to work properly on a VPS. The vps OS is Ubuntu 14.04 and I have installed and using VestaCP. Both Apache and nginx are installed on my vps.

I find no problem installing the varnish cache on the vps, my problem is that I cannot configure it to work properly. All the documentation I find is for servers using either Apache or Nginx, but VestaCP installed both on my machine. I need to move both apache and nging to some other ports and move the varnish on port 80. So, I need info on how to do that.

Thank you all.

Upvotes: 2

Views: 1877

Answers (2)

BigDropGR
BigDropGR

Reputation: 345

First you need to install VestaCP on your server. To do so, you need to root access your server and then type:

curl -O http://vestacp.com/pub/vst-install.sh

and

bash vst-install.sh

To install varnish, type:

sudo install varnish -y

You have to setup Varnish so it will run on port 80. The scenario will be like this: Varnish listen on port 80, Nginx on port 8082, then Apache on port 8080.

Web request = Varnish (80) -> Nginx (8082) -> Apache (8080)

Follow this steps:

  1. You mast edit the Varnish configuration file. The file is located in this directory: /etc/sysconfig/varnish (or /etc/varnish/varnish.params). You can edit through ftp by using your favourite text editor.

    nano /etc/sysconfig/varnish or nano /etc/varnish/varnish.params

  2. Search and find VARNISH_LISTEN_PORT. Change the port number to 80.

  3. Scroll down the page to find the VARNISH_STORAGE_SIZE. Set it to reasonable amount of RAM you want to allocate it as storage for the cache files made by Varnish. (All the cached files will be loaded from ram)

  4. Also change VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}" to this:VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}"

  5. Save changes and exit the text editor.

  6. Configure Varnish default VCL file located in /etc/varnish/. That VCL file holds configuration to tell varnish where to look for the webserver content: /etc/varnish/default.vcl

    nano /etc/varnish/default.vcl

Here what you gonna do:

change .host ip from 127.0.0.1 to your server public IP
change .port 80 to .port 8082 which where nginx should listening.
uncomment (remove the # symbol) at sub vcl_recv section
add one } symbol at the end of that section

Find the following sub vcl_backend_response block, and add the following lines to it:

    set beresp.ttl = 10s;
    set beresp.grace = 1h;
  1. Go to /etc/nginx/conf.d/ directory and see if there is configuration file of your public IP:

    cd /etc/nginx/conf.d
    ls
    
  2. Edit that .conf file. Replace xxx.xxx.xxx.xxx with your own actual public IP:

    nano xxx.xxx.xxx.xxx.conf
    
  3. Now change port :80 to :8082

  4. Also edit vesta.conf file located at /usr/local/vesta/conf/. Use your favorite text editor or in my situation I use Nano:

    nano /usr/local/vesta/conf/vesta.conf
    

change PROXY_PORT from 80 to 8082

  1. Edit nginx.conf file for each Vesta CP user located at /home/user/conf/web. This step is quite not efficient if you have several Vesta CP users as you have to edit them all. In my example I will edit nginx.conf file for user admin:

    nano /home/admin/conf/web/nginx.conf
    

Again, change port 80 to 8082 at the listen line.

  1. Open up your favorite web browser, login to your Vesta CP dashboard as admin then click the Firewall menu on top of the page. 12.1. Edit the /WEB section of the firewall 12.2. Now ad 8082 in the Port field and hit the green Save button.

  2. Before you run a test, you have to firstly restart Nginx and start Varnish Cache server:

    service nginx restart
    service varnish start
    

I followed the instructions found on this page: http://www.servermom.org/varnish-cache-vesta-cp/2564/ http://www.servermom.org/vestacp-configuration-varnish-cache/2580/

Upvotes: 1

Ronald
Ronald

Reputation: 2932

You can configure the Varnish port on Ubuntu by changing the '-a' option in '/etc/default/varnish'.

For further options see: https://www.varnish-cache.org/docs/4.0/reference/varnishd.html#http-accelerator-daemon

The backend system to which varnish forwards can be configured in '/etc/varnish/default.vcl'.

Upvotes: 0

Related Questions