Skip to content

Instantly share code, notes, and snippets.

@vdel26
Last active May 19, 2016 16:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vdel26/11284281 to your computer and use it in GitHub Desktop.
Save vdel26/11284281 to your computer and use it in GitHub Desktop.
Makefile for working with a remote Nginx
#### SETTINGS
NGINXDIR = /usr/local/openresty/nginx/
NGXCONF = nginx_dummycustomer.conf
USER = ubuntu
REMOTE = ec2-54-224-138-186.compute-1.amazonaws.com
REMOTEDIR = /home/$(USER)/dummycustomer/
PRIVATEKEY = /Users/victordg/.ssh/aws/vdg-3scale.pem
####
ngx_files = $(shell find . -name '*.lua' -o -name '*.conf')
ngx_bin = $(NGINXDIR)sbin/nginx
ngx_conf_dir = $(NGINXDIR)conf/
tmp_dir = /home/$(USER)/ngx-$(shell date +%s)
ifdef PRIVATEKEY
sshflags = -i $(PRIVATEKEY)
endif
rsyncflags = -e "ssh $(sshflags) $(PRIVATEKEY)" --checksum -rlvzu --exclude ".*" --exclude "*.tgz*" --exclude "Makefile"
all:
@echo "use any of the targets: start, stop, pull, push, sync, deploy, watch"
start:
@echo "starting Nginx"
ssh -i $(PRIVATEKEY) $(USER)@$(REMOTE) 'sudo $(ngx_bin) -c $(REMOTEDIR)$(NGXCONF)'
stop:
@echo "stopping Nginx"
ssh -i $(PRIVATEKEY) $(USER)@$(REMOTE) 'sudo $(ngx_bin) -c $(REMOTEDIR)$(NGXCONF) -s stop'
pull:
rsync --update $(rsyncflags) $(USER)@$(REMOTE):$(REMOTEDIR)/ .
push:
rsync $(rsyncflags) . $(USER)@$(REMOTE):$(REMOTEDIR)/
# synchronize with remote
sync: pull push
# push changes to remote and start Nginx with the config file specified by NGXCONF
deploy: push
ssh $(sshflags) $(PRIVATEKEY) $(USER)@$(REMOTE) 'sudo $(ngx_bin) -c $(REMOTEDIR)$(NGXCONF)'
# like deploy but when the remote Nginx is already running
redeploy: push
ssh $(sshflags) $(PRIVATEKEY) $(USER)@$(REMOTE) 'sudo $(ngx_bin) -c $(REMOTEDIR)$(NGXCONF) -s reload'
# watch local directory and push changes
# requires watchman -- https://github.com/facebook/watchman
watch:
@echo "Watching for changes in *.conf and *.lua files"
watchman watch $(shell pwd) -f --persistent --server-encoding=json
watchman -- trigger $(shell pwd) remake '*.conf' '*.lua' -- make push
.PHONY: watch deploy redeploy start stop push pull sync
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment