Skip to content

Commit 6b1d3e2

Browse files
authored
Merge pull request #41 from paliarush/varnish_support
Varnish support
2 parents 837adfc + 906a358 commit 6b1d3e2

File tree

9 files changed

+294
-4
lines changed

9 files changed

+294
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2929
- Added ability to modify guest config files (PHP, Apache etc) directly from host IDE
3030
- Added ability to choose if PhpStorm configs should be removed during project reinitialization
3131
- Added ability to switch PHP version without destroying the project ("vagrant reload" is required)
32+
- Added ability to use Varnish full page caching automatically. (Using "vagrant reload" or m-varnish script)
3233

3334
## [v2.0.0] - 2016-02-05
3435

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* [View emails sent by Magento](#view-emails-sent-by-magento)
2323
* [Accessing PHP and other config files](#accessing-php-and-other-config-files)
2424
* [Switch between PHP 5.6 and 7.0](#switch-between-php-56-and-70)
25+
* [Activating Varnish](#activating-varnish)
2526
* [Multiple Magento instances](#multiple-magento-instances)
2627
* [Reset environment](#reset-environment)
2728

@@ -38,7 +39,7 @@ It is easy to [install multiple Magento instances](#multiple-magento-instances)
3839
[Project initialization script](init_project.sh) configures complete development environment:
3940

4041
1. Adds some missing software on the host
41-
1. Configures all software necessary for Magento 2 using [custom Ubuntu vagrant box](https://atlas.hashicorp.com/paliarush/boxes/magento2.ubuntu) (Apache 2.4, PHP 7.0 (or 5.5.9), MySQL 5.6, Git, Composer, XDebug, Rabbit MQ)
42+
1. Configures all software necessary for Magento 2 using [custom Ubuntu vagrant box](https://atlas.hashicorp.com/paliarush/boxes/magento2.ubuntu) (Apache 2.4, PHP 7.0 (or 5.5.9), MySQL 5.6, Git, Composer, XDebug, Rabbit MQ, Varnish)
4243
1. Installs Magento 2
4344
1. Configures PHP Storm project (partially at the moment)
4445

@@ -230,6 +231,15 @@ After editing configs in IDE it is still required to restart related services ma
230231
Set "use_php7: 1" for PHP7 and "use_php7: 0" for PHP5.6 in [config.yaml](etc/config.yaml.dist).
231232
PHP version will be applied after "vagrant reload".
232233

234+
### Activating Varnish
235+
236+
Set "use_varnish: 1" to use varnish along apache in [config.yaml](etc/config.yaml.dist).
237+
Running vagrant reload will apply your preference. Or by using m-varnish script.
238+
It will use default file etc/magento2_default_varnish.vcl.dist generated from a Magento instance.
239+
Varnish Version: 3.0.5
240+
241+
Note: command m-varnish with arguments disable or enable is also available in guest or host to enable or disable varnish without reloading machine.
242+
233243
### Multiple Magento instances
234244

235245
To install several Magento instances based on different code bases, just follow [Installation steps](#installation-steps) to initialize project in another directory on the host.

etc/config.yaml.dist

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
repository_url:
2+
# [To apply changes: init-project -c]
23
ce: "[email protected]:magento/magento2.git"
34
ee: ""
45
guest:
5-
# NFS will be used for folder synchronization on *nix and OSX hosts by default
6+
# [To apply changes: init-project -f]
7+
# NFS will be used for folder synchronization on *nix and OSX hosts by default.
68
use_nfs: 1
7-
# Default is 2Gb, around 3Gb is necessary to run functional tests
9+
# Default is 2Gb, around 3Gb is necessary to run functional tests.
10+
# [To apply changes: vagrant reload]
811
memory: 2048
912
ip_address: "192.168.10.2"
1013
forwarded_ssh_port: 3000
1114
environment:
12-
# If set to 0, PHP 5 will be installed. [Changeable: vagrant reload]
15+
# [To apply changes: vagrant reload]
16+
# If set to 0, PHP 5 will be installed.
1317
use_php7: 1
1418
composer_prefer_source: 0
19+
use_varnish: 0
1520
magento:
21+
# [To apply changes: init-project -f]
1622
host_name: "magento2.vagrant2"
23+
# [To apply changes: m-reinstall]
1724
admin_frontname: "admin"
1825
language: "en_US"
1926
timezone: "America/Chicago"

etc/magento2_default_varnish.vcl.dist

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import std;
2+
# The minimal Varnish version is 3.0.5
3+
4+
backend default {
5+
.host = "127.0.0.1";
6+
.port = "8080";
7+
}
8+
9+
acl purge {
10+
"127.0.0.1";
11+
}
12+
13+
sub vcl_recv {
14+
if (req.restarts == 0) {
15+
if (req.http.x-forwarded-for) {
16+
set req.http.X-Forwarded-For =
17+
req.http.X-Forwarded-For + ", " + client.ip;
18+
} else {
19+
set req.http.X-Forwarded-For = client.ip;
20+
}
21+
}
22+
23+
if (req.request == "PURGE") {
24+
if (client.ip !~ purge) {
25+
error 405 "Method not allowed";
26+
}
27+
if (!req.http.X-Magento-Tags-Pattern) {
28+
error 400 "X-Magento-Tags-Pattern header required";
29+
}
30+
ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
31+
error 200 "Purged";
32+
}
33+
34+
if (req.request != "GET" &&
35+
req.request != "HEAD" &&
36+
req.request != "PUT" &&
37+
req.request != "POST" &&
38+
req.request != "TRACE" &&
39+
req.request != "OPTIONS" &&
40+
req.request != "DELETE") {
41+
/* Non-RFC2616 or CONNECT which is weird. */
42+
return (pipe);
43+
}
44+
45+
# We only deal with GET and HEAD by default
46+
if (req.request != "GET" && req.request != "HEAD") {
47+
return (pass);
48+
}
49+
50+
# Bypass shopping cart and checkout requests
51+
if (req.url ~ "/checkout") {
52+
return (pass);
53+
}
54+
55+
# normalize url in case of leading HTTP scheme and domain
56+
set req.url = regsub(req.url, "^http[s]?://", "");
57+
58+
# collect all cookies
59+
std.collect(req.http.Cookie);
60+
61+
# static files are always cacheable. remove SSL flag and cookie
62+
if (req.url ~ "^/(pub/)?(media|static)/.*\.(ico|css|js|jpg|jpeg|png|gif|tiff|bmp|gz|tgz|bz2|tbz|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)$") {
63+
unset req.http.Https;
64+
unset req.http.Cookie;
65+
}
66+
67+
set req.grace = 1m;
68+
69+
return (lookup);
70+
}
71+
72+
sub vcl_hash {
73+
if (req.http.cookie ~ "X-Magento-Vary=") {
74+
hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1"));
75+
}
76+
77+
}
78+
79+
sub vcl_fetch {
80+
if (beresp.http.content-type ~ "text") {
81+
set beresp.do_esi = true;
82+
}
83+
84+
if (req.url ~ "\.js$" || beresp.http.content-type ~ "text") {
85+
set beresp.do_gzip = true;
86+
}
87+
88+
# cache only successfully responses and 404s
89+
if (beresp.status != 200 && beresp.status != 404) {
90+
set beresp.ttl = 0s;
91+
return (hit_for_pass);
92+
} elsif (beresp.http.Cache-Control ~ "private") {
93+
return (hit_for_pass);
94+
}
95+
96+
if (beresp.http.X-Magento-Debug) {
97+
set beresp.http.X-Magento-Cache-Control = beresp.http.Cache-Control;
98+
}
99+
100+
# validate if we need to cache it and prevent from setting cookie
101+
# images, css and js are cacheable by default so we have to remove cookie also
102+
if (beresp.ttl > 0s && (req.request == "GET" || req.request == "HEAD")) {
103+
unset beresp.http.set-cookie;
104+
if (req.url !~ "\.(ico|css|js|jpg|jpeg|png|gif|tiff|bmp|mp3|ogg|svg|swf|woff|woff2|eot|ttf|otf)(\?|$)") {
105+
set beresp.http.Pragma = "no-cache";
106+
set beresp.http.Expires = "-1";
107+
set beresp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
108+
set beresp.grace = 1m;
109+
}
110+
}
111+
}
112+
113+
sub vcl_deliver {
114+
if (resp.http.X-Magento-Debug) {
115+
if (obj.hits > 0) {
116+
set resp.http.X-Magento-Cache-Debug = "HIT";
117+
} else {
118+
set resp.http.X-Magento-Cache-Debug = "MISS";
119+
}
120+
} else {
121+
unset resp.http.Age;
122+
}
123+
124+
unset resp.http.X-Magento-Debug;
125+
unset resp.http.X-Magento-Tags;
126+
unset resp.http.X-Powered-By;
127+
unset resp.http.Server;
128+
unset resp.http.X-Varnish;
129+
unset resp.http.Via;
130+
unset resp.http.Link;
131+
}

m-varnish

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
vagrant_dir=$(cd "$(dirname "$0")"; pwd)
4+
cd ${vagrant_dir}
5+
vagrant ssh -c "m-varnish ${@}"

scripts/guest/configure_varnish

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env bash
2+
3+
# Get args
4+
while getopts 'f' flag; do
5+
case "${flag}" in
6+
f) force_restart_services=1 ;;
7+
*) error "Unexpected option ${flag}" ;;
8+
esac
9+
done
10+
11+
# Init environment variables
12+
vagrant_dir="/vagrant"
13+
14+
# Configure Varnish if enabled in config
15+
restart_services=0
16+
use_varnish=$(bash "${vagrant_dir}/scripts/get_config_value.sh" "environment_use_varnish")
17+
if [[ $use_varnish == 1 ]]; then
18+
19+
# Configure apache files to be ready for varnish
20+
if [[ -z "$(grep "VirtualHost\s\*:8080" /etc/apache2/sites-enabled/magento2.conf)" ]]; then
21+
sudo sed -ie "s/<VirtualHost .*/<VirtualHost \*:8080>/" /etc/apache2/sites-enabled/magento2.conf
22+
restart_services=1
23+
fi
24+
if [[ -z "$(grep "VirtualHost\s\*:8080" /etc/apache2/sites-available/magento2.conf)" ]]; then
25+
sudo sed -ie "s/<VirtualHost .*/<VirtualHost \*:8080>/" /etc/apache2/sites-available/magento2.conf
26+
restart_services=1
27+
fi
28+
if [[ -z "$(grep "Listen\s8080" /etc/apache2/ports.conf)" ]]; then
29+
sudo sed -ie "s/Listen 80/Listen 8080/" /etc/apache2/ports.conf
30+
restart_services=1
31+
fi
32+
33+
# Update Magento database to use varnish FPC
34+
mysql -D magento -e "INSERT INTO core_config_data
35+
( scope, scope_id, path, value ) VALUES
36+
( 'default', '0', 'system/full_page_cache/caching_application', '2' )
37+
ON DUPLICATE KEY UPDATE value = 2
38+
;"
39+
else
40+
41+
# Configure apache files to run without varnish
42+
if [[ -z "$(grep "VirtualHost\s\*:80\b" /etc/apache2/sites-enabled/magento2.conf)" ]]; then
43+
sudo sed -ie "s/<VirtualHost .*/<VirtualHost \*:80>/" /etc/apache2/sites-enabled/magento2.conf
44+
restart_services=1
45+
fi
46+
if [[ -z "$(grep "VirtualHost\s\*:80\b" /etc/apache2/sites-available/magento2.conf)" ]]; then
47+
sudo sed -ie "s/<VirtualHost .*/<VirtualHost \*:80>/" /etc/apache2/sites-available/magento2.conf
48+
restart_services=1
49+
fi
50+
if [[ -z "$(grep "Listen\s80\b" /etc/apache2/ports.conf)" ]]; then
51+
sudo sed -ie "s/Listen 8080/Listen 80/" /etc/apache2/ports.conf
52+
restart_services=1
53+
fi
54+
55+
# Update Magento database to not use varnish FPC
56+
mysql -D magento -e "INSERT INTO core_config_data
57+
( scope, scope_id, path, value ) VALUES
58+
( 'default', '0', 'system/full_page_cache/caching_application', '1' )
59+
ON DUPLICATE KEY UPDATE value = 1
60+
;"
61+
fi
62+
63+
# Check if need restart services
64+
if [[ $force_restart_services == 1 ]]; then
65+
echo "Force restarting Apache and Varnish"
66+
restart_services=1
67+
fi
68+
if [[ $restart_services == 1 ]]; then
69+
if [[ "$(ps -ax | pgrep varnish)" ]]; then
70+
sudo pkill varnishd
71+
fi
72+
sudo service apache2 restart
73+
if [[ $use_varnish == 1 ]]; then
74+
sudo varnishd -f /etc/varnish/default.vcl
75+
fi
76+
bash "${vagrant_dir}/scripts/guest/m-clear-cache"
77+
fi

scripts/guest/m-varnish

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
3+
if [[ $1 == "help" ]]; then
4+
echo "Usage: ./m-varnish enable/disable"
5+
exit 0
6+
fi
7+
8+
# Check if user created config.yaml file
9+
if [[ ! -f /vagrant/etc/config.yaml ]]; then
10+
echo "Please make sure you have create a config.yaml file copy from etc/config.yaml.dist"
11+
exit 1
12+
fi
13+
14+
if [[ -z "$(grep "use_varnish:" /vagrant/etc/config.yaml)" ]]; then
15+
sed -i '/environment:/a \ \ use_varnish: 0' /vagrant/etc/config.yaml
16+
fi
17+
18+
if [[ $1 == "enable" ]]; then
19+
sed -ie "s/use_varnish:.*/use_varnish: 1/" /vagrant/etc/config.yaml
20+
elif [[ $1 == "disable" ]]; then
21+
sed -ie "s/use_varnish:.*/use_varnish: 0/" /vagrant/etc/config.yaml
22+
else
23+
echo "Usage: ./m-varnish enable/disable"
24+
exit 1
25+
fi
26+
27+
configure_varnish
28+
29+
echo "Done."

scripts/provision/configure_environment_recurring.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ if [[ -f ${vagrant_dir}/.idea/deployment.xml ]]; then
5757
sed -i.back "s|magento2ce/var/generation|magento2ce/var|g" "${vagrant_dir}/.idea/deployment.xml"
5858
fi
5959

60+
# Copy varnish vcl file
61+
custom_vcl_config="${vagrant_dir}/etc/magento2_default_varnish.vcl"
62+
default_vcl_config="${vagrant_dir}/etc/magento2_default_varnish.vcl.dist"
63+
if [ -f ${custom_vcl_config} ]; then
64+
cp ${custom_vcl_config} /etc/varnish/default.vcl
65+
else
66+
cp ${default_vcl_config} /etc/varnish/default.vcl
67+
fi
68+
69+
# Configure Varnish FPC, if enabled
70+
bash "${vagrant_dir}/scripts/guest/configure_varnish" -f
71+
6072
# Setup PHP
6173
php_ini_paths=( /etc/php/7.0/cli/php.ini /etc/php/5.6/cli/php.ini )
6274
process_php_config ${php_ini_paths}

scripts/provision/upgrade_environment_recurring.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
#!/usr/bin/env bash
22

3+
function isServiceAvailable() {
4+
if service --status-all | grep -Fq ${1}; then
5+
echo 1
6+
else
7+
echo 0
8+
fi
9+
}
10+
311
# Enable trace printing and exit on the first error
412
set -ex
513

614
use_php7=$4
715

16+
# Delete obsolete repository
17+
sudo rm -f /etc/apt/sources.list.d/ondrej-php-7_0-trusty.list
18+
819
# Upgrade for vagrant box paliarush/magento2.ubuntu v1.1.0
920
if [[ ${use_php7} -eq 1 ]]; then
1021
if /usr/bin/php7.0 -v | grep -q '7.0.5' ; then
@@ -31,4 +42,11 @@ if [[ ${use_php7} -eq 1 ]]; then
3142
# Restart Apache
3243
service apache2 restart
3344
fi
45+
fi
46+
47+
# Install varnish if not installed
48+
is_varnish_installed="$(isServiceAvailable varnish)"
49+
if [[ ${is_varnish_installed} -eq 0 ]]; then
50+
apt-get update
51+
apt-get install -y varnish
3452
fi

0 commit comments

Comments
 (0)