Skip to content

Commit fa14504

Browse files
author
Hayder Sharhan
committed
Varnish Support
- Works now by invoking vagrant reload or by running "m-varnish -c enable" command.
1 parent 26f0f66 commit fa14504

File tree

8 files changed

+84
-16
lines changed

8 files changed

+84
-16
lines changed

CHANGELOG.md

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

3132
## [v2.0.0] - 2016-02-05
3233

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ PHP version will be applied after "vagrant reload".
228228
### Activating Varnish
229229

230230
Set "use_varnish: 1" to use varnish along apache in [config.yaml](etc/config.yaml.dist).
231-
Running m-reinstall script will apply your preference.
231+
Running vagrant reload will apply your preference. Or by using m-varnish script.
232232
It will use default file etc/magento2_default_varnish.vcl.dist generated from a Magento instance.
233233
Varnish Version: 3.0.5
234234

etc/config.yaml.dist

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
repository_url:
2+
# [Changeable: init-project -c]
23
ce: "git@github.com:magento/magento2.git"
34
ee: ""
45
guest:
5-
# NFS will be used for folder synchronization on *nix and OSX hosts by default
6+
# [Changeable: 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+
# [Changeable: 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+
# [Changeable: vagrant reload]
16+
# If set to 0, PHP 5 will be installed.
1317
use_php7: 1
1418
composer_prefer_source: 0
1519
use_varnish: 0
1620
magento:
21+
# [Changeable: init-project -f]
1722
host_name: "magento2.vagrant2"
23+
# [Changeable: m-reinstall]
1824
admin_frontname: "admin"
1925
language: "en_US"
2026
timezone: "America/Chicago"

m-varnish

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
3+
set -ex
4+
5+
# Get args
6+
while getopts 'fh:c:' flag; do
7+
case "${flag}" in
8+
h) help=1 ;;
9+
c) command=$OPTARG ;;
10+
f) force_restart_services=1 ;;
11+
*) help=1 ;;
12+
esac
13+
done
14+
15+
if [ $help ]; then
16+
echo "Usage: ./m-varnish -c enable/disable (-f to force restart services)"
17+
exit 0
18+
fi
19+
20+
# Check if user created config.yaml file
21+
if [ ! -f ./etc/config.yaml ]; then
22+
echo "Please make sure you have create a config.yaml file copy from etc/config.yaml.dist"
23+
exit 0
24+
fi
25+
26+
if [[ $command == "enable" ]]; then
27+
sed -ie "s/use_varnish:.*/use_varnish: 1/" ./etc/config.yaml
28+
elif [[ $command == "disable" ]]; then
29+
sed -ie "s/use_varnish:.*/use_varnish: 0/" ./etc/config.yaml
30+
else
31+
echo "Usage: ./m-varnish -c enable/disable (-f to force restart services)"
32+
exit 0
33+
fi
34+
35+
vagrant_dir=$(cd "$(dirname "$0")"; pwd)
36+
cd ${vagrant_dir}
37+
if [[ $force_restart_services == 1 ]]; then
38+
vagrant ssh -c "sudo bash /vagrant/scripts/guest/configure_varnish -f"
39+
else
40+
vagrant ssh -c "sudo bash /vagrant/scripts/guest/configure_varnish"
41+
fi

scripts/guest/m-reinstall-scripts/configure_varnish renamed to scripts/guest/configure_varnish

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
# Enable trace printing and exit on the first error
44
set -ex
55

6+
# Get args
7+
while getopts 'f' flag; do
8+
case "${flag}" in
9+
f) force_restart_services=1 ;;
10+
*) error "Unexpected option ${flag}" ;;
11+
esac
12+
done
13+
614
# Init environment variables
715
vagrant_dir="/vagrant"
816

@@ -29,6 +37,7 @@ if [ $use_varnish -eq 1 ]; then
2937
mysql -D magento -e "INSERT INTO core_config_data
3038
( scope, scope_id, path, value ) VALUES
3139
( 'default', '0', 'system/full_page_cache/caching_application', '2' )
40+
ON DUPLICATE KEY UPDATE value = 2
3241
;"
3342
else
3443

@@ -45,9 +54,20 @@ else
4554
sudo sed -ie "s/Listen 8080/Listen 80/" /etc/apache2/ports.conf
4655
restart_services=1
4756
fi
57+
58+
# Update Magento database to not use varnish FPC
59+
mysql -D magento -e "INSERT INTO core_config_data
60+
( scope, scope_id, path, value ) VALUES
61+
( 'default', '0', 'system/full_page_cache/caching_application', '1' )
62+
ON DUPLICATE KEY UPDATE value = 1
63+
;"
4864
fi
4965

5066
# Check if need restart services
67+
if [[ $force_restart_services == 1 ]]; then
68+
echo "Force restarting Apache and Varnish"
69+
restart_services=1
70+
fi
5171
if [ $restart_services -eq 1 ]; then
5272
if [ "$(ps -ax | pgrep varnish)" ]; then
5373
sudo pkill varnishd

scripts/guest/m-reinstall

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ php ${install_cmd}
7777
# Comment out the line above and uncomment the one below to debug Magento Setup script
7878
# php -d xdebug.remote_host=192.168.10.1 -d xdebug.idekey=PHPSTORM -d xdebug.remote_connect_back=0 -d xdebug.remote_autostart=1 ${install_cmd}
7979

80-
# Configure Varnish FPC, if enabled
81-
sh ${vagrant_dir}/scripts/guest/m-reinstall-scripts/configure_varnish
82-
8380
# Enable Magento cron jobs
8481
echo "* * * * * php ${MAGENTO_ROOT}/bin/magento cron:run &" | crontab -u vagrant -
8582

scripts/provision/configure_environment.sh

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,6 @@ a2ensite magento2.conf
2929
# Disable default virtual host
3030
sudo a2dissite 000-default
3131

32-
# Copy varnish vcl file
33-
custom_vcl_config="${vagrant_dir}/etc/magento2_default_varnish.vcl"
34-
default_vcl_config="${vagrant_dir}/etc/magento2_default_varnish.vcl.dist"
35-
if [ -f ${custom_vcl_config} ]; then
36-
cp ${custom_vcl_config} /etc/varnish/default.vcl
37-
else
38-
cp ${default_vcl_config} /etc/varnish/default.vcl
39-
fi
40-
4132
# Configure composer
4233
composer_auth_json="${vagrant_dir}/etc/composer/auth.json"
4334
if [ -f ${composer_auth_json} ]; then

scripts/provision/configure_environment_recurring.sh

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

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

0 commit comments

Comments
 (0)