Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aalhour/7cc8bfa1681923016b1fcf6f8a64f7d9 to your computer and use it in GitHub Desktop.
Save aalhour/7cc8bfa1681923016b1fcf6f8a64f7d9 to your computer and use it in GitHub Desktop.
Django with Gunicorn + Nginx
SHELL := /bin/bash
#-----------------------------------------------
# __ __ _ __ _ _
# | \/ | __ _| | _____ / _(_) | ___
# | |\/| |/ _ | |/ / _ \ |_| | |/ _ \
# | | | | (_| | < __/ _| | | __/
# |_| |_|\__,_|_|\_\___|_| |_|_|\___|
#
#-----------------------------------------------
# Makefile to use:
# > Gunicorn + Nginx
# > APACHE2 + mod_wsgi
#-----------------------------------------------
# Author: elchinot7
# Email: efraazu@gmail.com
# Github: https://github.com/elchinot7
# Description: This Makefile is useful to:
# > Run, re-start and stop
# Nginx and Gunicrn
# > Run, re-start and stop
# apache2
# > Fixing a bug with MEDIA URLS
# in apache with mod_wsgi
# (pip-installed)
#-----------------------------------------------
# SETTINGS TO APACHE2
#-----------------------------------------------
#-----------------------------------------------
SERVER_ROOT = /etc/mod_wsgi-express-80
FIX_CHUNK = "Alias '/media' '/var/www/media'\n\n<Directory '/var/www/media'>\n Order allow,deny\n Allow from all\n</Directory>\n"
#-----------------------------------------------
#-----------------------------------------------
# SETTINGS TO GUNICORN
#-----------------------------------------------
USER = www-data
GROUP = www-data
PROJECT = mysite
HOST = 127.0.0.1
PORT = 8001
N_WORKERS = 3
PID_FILE = /var/www/logs/cosmoinsane_gunicorn_pid.log
#-----------------------------------------------
GUN_FLAGS = --workers $(N_WORKERS)
GUN_FLAGS += --user $(USER)
GUN_FLAGS += --group $(GROUP)
GUN_FLAGS += --bind $(HOST):$(PORT)
GUN_FLAGS += --pid $(PID_FILE)
GUN_FLAGS += --daemon
#-----------------------------------------------
#GUN_FLAGS_LOCAL = --workers $(N_WORKERS)
#GUN_FLAGS_LOCAL += --user $(USER)
#GUN_FLAGS_LOCAL += --group $(GROUP)
GUN_FLAGS_LOCAL += --bind $(HOST):$(PORT)
#GUN_FLAGS_LOCAL += --daemon
.PHONY: gunicorn start_nginx stop_nginx run start stop start_apache stop_apache fix_media help
default: start
start: gunicorn
stop: stop_gunicorn
local: run_local
start_nginx:
service gninx start
stop_nginx:
service gninx stop
gunicorn:
gunicorn $(GUN_FLAGS) $(PROJECT).wsgi
stop_gunicorn:
kill `cat $(PID_FILE)`
gunicorn_local:
gunicorn $(GUN_FLAGS_LOCAL) $(PROJECT).wsgi
run_local:
python manage.py runserver
run_modwsgi:
python manage.py runmodwsgi --port=80 --user=www-data --group=www-data --server-root=$(SERVER_ROOT)
start_apache:
@echo -e "\n\n$(REDC)apache2$(ENDC) + $(REDC)mod_wsgi$(ENDC) is running :) \n\n"
cd $(SERVER_ROOT) && sudo ./apachectl start
@echo ""
stop_apache:
@echo -e "\n\n$(REDC)apache2$(ENDC) + $(REDC)mod_wsgi$(ENDC) stopped :( \n\n"
cd $(SERVER_ROOT) && sudo ./apachectl stop
@echo ""
fix_media:
@echo ""
@echo "$(GREENC)WARNING:$(ENDC)"
@echo -e "\nmedia alias is fixed in httpd.conf\n"
@cd $(SERVER_ROOT) && echo -e $(FIX_CHUNK) >>httpd.conf
# -----------------------------
# Some styles and colors to be
# used in Terminal outputs
# -----------------------------
REDC = \033[31m
BOLD = \033[1m
GREENC = \033[32m
UNDERLINE = \033[4m
ENDC = \033[0m
# -----------------------------
# --------------------------------------------------------------
help:
@echo "------------------------------------------------------"
@echo -e " $(UNDERLINE)$(REDC) < APACHE2 + mod_wsgi >$(ENDC)"
@echo -e " $(GREENC) Makefile Menu$(ENDC)"
@echo "------------------------------------------------------"
@echo "Please use 'make <target>' where target is one of:"
@echo
@echo -e "$(REDC)default$(ENDC) > Default Action"
@echo
@echo -e " '$(GREENC)start$(ENDC)'"
@echo
@echo -e "$(REDC)start$(ENDC) > to start gunicorn "
@echo
@echo -e "$(REDC)start_nginx$(ENDC) > to start nginx "
@echo
@echo -e "$(REDC)stop_nginx$(ENDC) > to start nginx "
@echo
@echo -e "$(REDC)run_modwsgi$(ENDC) > to run apache2 in first place"
@echo -e " This produces a bug for 'media/' file"
@echo -e " sharing in production "
@echo
@echo -e "$(REDC)start_apache$(ENDC) > to start apache2 "
@echo
@echo -e "$(REDC)stop_apache$(ENDC) > to stop apache2 "
@echo
@echo -e "$(REDC)fix_media$(ENDC) > to fix the MEDIA permissions. "
@echo "------------------------------------------------------"
# --------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment