-
-
Save johndiego/9fb42c5baa535d7fff7d34671e8e9499 to your computer and use it in GitHub Desktop.
Install Varnish on Vesta CP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<IfModule mpm_prefork_module> | |
StartServers 1 | |
MinSpareServers 1 | |
MaxSpareServers 3 | |
MaxClients 6 | |
MaxRequestsPerChild 0 | |
</IfModule> | |
<IfModule mpm_worker_module> | |
StartServers 1 | |
MinSpareThreads 5 | |
MaxSpareThreads 15 | |
ThreadLimit 25 | |
ThreadsPerChild 5 | |
MaxClients 25 | |
MaxRequestsPerChild 0 | |
</IfModule> | |
<IfModule mpm_event_module> | |
StartServers 1 | |
MinSpareThreads 5 | |
MaxSpareThreads 15 | |
ThreadLimit 25 | |
ThreadsPerChild 5 | |
MaxClients 25 | |
MaxRequestsPerChild 0 | |
</IfModule> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
thread_cache_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// usefull links | |
// http://ryanfrankel.com/run-wordpress-digital-ocean-512mb-vps/ | |
// http://ryanfrankel.com/using-varnish-with-wordpress/ | |
// start | |
apt-get update | |
apt-get upgrade | |
// set different ssh port | |
mcedit /etc/ssh/sshd_config | |
// change vesta firewall rules | |
mcedit /usr/local/vesta/data/firewall/rules.conf | |
// update vesta | |
v-update-firewall | |
// check if ssh is working on a new port | |
service ssh restart | |
netstat -taupen | |
// install varnish | |
// https://www.vultr.com/docs/installing-varnish-4-with-apache2-on-ubuntu-14-04 | |
sudo apt-get install apt-transport-https | |
sudo curl https://repo.varnish-cache.org/ubuntu/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 | |
sudo apt-get update | |
sudo apt-get install varnish | |
// edit varnish config - change port to 80 | |
mcedit /etc/default/varnish | |
// edit varnish config - change ip and port to 8082 | |
mcedit /etc/varnish/default.vcl | |
// disable default nginx config | |
mcedit /etc/nginx/conf.d/defaultIP.conf | |
// change nginx port to 8082 | |
mcedit /etc/nginx/conf.d/yourIP.conf | |
// change proxy port to 8082 | |
mcedit /usr/local/vesta/conf/vesta.conf | |
// change web port to 8082 | |
mcedit /home/admin/conf/web/nginx.conf | |
// restart varnish | |
service varnish reload | |
service varnish restart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# VCL version 4 | |
vcl 4.0; | |
/* SET THE HOST AND PORT OF WORDPRESS | |
* *********************************************************/ | |
backend default { | |
.host = "127.0.0.1"; | |
.port = "8082"; | |
} | |
# SET THE ALLOWED IP OF PURGE REQUESTS | |
# ########################################################## | |
acl purge { | |
"localhost"; | |
"162.243.20.190"; | |
} | |
# THE RECV FUNCTION | |
# ########################################################## | |
sub vcl_recv { | |
# For Testing: If you want to test with Varnish passing (not caching) uncomment | |
# return( pass ); | |
# FORWARD THE IP OF THE REQUEST | |
if (req.restarts == 0) { | |
if (req.http.x-forwarded-for) { | |
set req.http.X-Forwarded-For = | |
req.http.X-Forwarded-For + ", " + client.ip; | |
} else { | |
set req.http.X-Forwarded-For = client.ip; | |
} | |
} | |
# CLEAN UP THE ENCODING HEADER. | |
# SET TO GZIP, DEFLATE, OR REMOVE ENTIRELY. WITH VARY ACCEPT-ENCODING | |
# VARNISH WILL CREATE SEPARATE CACHES FOR EACH | |
# DO NOT ACCEPT-ENCODING IMAGES, ZIPPED FILES, AUDIO, ETC. | |
# ########################################################## | |
if (req.http.Accept-Encoding) { | |
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") { | |
# No point in compressing these | |
unset req.http.Accept-Encoding; | |
} elsif (req.http.Accept-Encoding ~ "gzip") { | |
set req.http.Accept-Encoding = "gzip"; | |
} elsif (req.http.Accept-Encoding ~ "deflate") { | |
set req.http.Accept-Encoding = "deflate"; | |
} else { | |
# unknown algorithm | |
unset req.http.Accept-Encoding; | |
} | |
} | |
# IF THIS IS A PURGE REQUEST, THEN CHECK THE IPS SET ABOVE | |
# BLOCK IF NOT ONE OF THOSE IPS | |
# ########################################################## | |
if (req.method == "PURGE") { | |
if ( !client.ip ~ purge ) { | |
return (synth(405, "Not allowed.")); | |
} | |
return (purge); | |
} | |
# PIPE ALL NON-STANDARD REQUESTS | |
# ########################################################## | |
if (req.method != "GET" && | |
req.method != "HEAD" && | |
req.method != "PUT" && | |
req.method != "POST" && | |
req.method != "TRACE" && | |
req.method != "OPTIONS" && | |
req.method != "DELETE") { | |
return (pipe); | |
} | |
# ONLY CACHE GET AND HEAD REQUESTS | |
# ########################################################## | |
if (req.method != "GET" && req.method != "HEAD") { | |
return (pass); | |
} | |
# OPTIONAL: DO NOT CACHE LOGGED IN USERS (THIS OCCURS IN FETCH TO, EITHER | |
# COMMENT OR UNCOMMENT BOTH | |
# ########################################################## | |
if ( req.http.cookie ~ "wordpress_logged_in" ) { | |
return( pass ); | |
} | |
# IF THE REQUEST IS NOT FOR A PREVIEW, WP-ADMIN OR WP-LOGIN | |
# THEN UNSET THE COOKIES | |
# ########################################################## | |
if ( | |
!(req.url ~ "wp-(login|admin)") | |
&& !(req.url ~ "&preview=true" ) | |
){ | |
unset req.http.cookie; | |
} | |
# DISABLE CACHE FOR PHPMYADMIN | |
if ( | |
req.url ~ "phpmyadmin") | |
{ | |
return (pass); | |
} | |
# IF BASIC AUTH IS ON THEN DO NOT CACHE | |
# ########################################################## | |
if (req.http.Authorization || req.http.Cookie) { | |
return (pass); | |
} | |
# LIST URLS NOT TO BE CACHED | |
# USUALLY THIS INCLUDES THE PREVIEW QUERY STRING, A NOCACHE | |
# QUERY STRING, AND FILES THAT WILL BE SERVED BY THE CDN | |
# SUCH AS IMAGES, CSS, AND JS. IF YOU ARE NOT USING A CDN | |
# YOU CAN CACHE YOUR STATIC RESOURCES TOO | |
# ########################################################## | |
if ( | |
req.url ~ "preview" | |
|| req.url ~ "nocache" | |
|| req.url ~ "\.css$" | |
|| req.url ~ "\.js$" | |
|| req.url ~ "\.jpg$" | |
|| req.url ~ "\.jpeg$" | |
|| req.url ~ "\.gif$" | |
|| req.url ~ "\.png$" | |
) { | |
return (pass); | |
} | |
# IF YOU GET HERE THEN THIS REQUEST SHOULD BE CACHED | |
# ########################################################## | |
return (pass); | |
} | |
# HIT FUNCTION | |
# ########################################################## | |
sub vcl_hit { | |
# IF THIS IS A PURGE REQUEST THEN DO THE PURGE | |
# ########################################################## | |
if (req.method == "PURGE") { | |
return (synth(200, "Purged.")); | |
} | |
return (deliver); | |
} | |
# MISS FUNCTION | |
# ########################################################## | |
sub vcl_miss { | |
if (req.method == "PURGE") { | |
return (synth(200, "Purged.")); | |
} | |
return (fetch); | |
} | |
# FETCH FUNCTION | |
# ########################################################## | |
sub vcl_backend_response { | |
# I SET THE VARY TO ACCEPT-ENCODING, THIS OVERRIDES W3TC | |
# TENDANCY TO SET VARY USER-AGENT. YOU MAY OR MAY NOT WANT | |
# TO DO THIS | |
# ########################################################## | |
set beresp.http.Vary = "Accept-Encoding"; | |
# IF NOT WP-ADMIN THEN UNSET COOKIES AND SET THE AMOUNT OF | |
# TIME THIS PAGE WILL STAY CACHED (TTL) | |
# ########################################################## | |
if (!(bereq.url ~ "wp-(login|admin)") && !bereq.http.cookie ~ "wordpress_logged_in" ) { | |
unset beresp.http.set-cookie; | |
set beresp.ttl = 96h; | |
} | |
if (beresp.ttl <= 0s || | |
beresp.http.Set-Cookie || | |
beresp.http.Vary == "*") { | |
set beresp.uncacheable = true; | |
set beresp.ttl = 120s; | |
return (deliver); | |
} | |
return (deliver); | |
} | |
# DELIVER FUNCTION | |
# ########################################################## | |
sub vcl_deliver { | |
# IF THIS PAGE IS ALREADY CACHED THEN RETURN A 'HIT' TEXT | |
# IN THE HEADER (GREAT FOR DEBUGGING) | |
# ########################################################## | |
if (obj.hits > 0) { | |
set resp.http.X-Cache = "HIT"; | |
# IF THIS IS A MISS RETURN THAT IN THE HEADER | |
# ########################################################## | |
} else { | |
set resp.http.X-Cache = "MISS"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment