diff --git a/apache/vhosts/standard.sls b/apache/vhosts/standard.sls new file mode 100644 index 0000000..b325270 --- /dev/null +++ b/apache/vhosts/standard.sls @@ -0,0 +1,31 @@ +{% from "apache/package-map.jinja" import apache with context %} + +include: + - apache + +{% for id, site in salt['pillar.get']('apache:sites', {}).items() %} + +{{ id }}: + file: + - managed + - name: {{ apache.vhostdir }}/{{ id }} + - source: {{ site.get('template_file', 'salt://apache/vhosts/standard.tmpl') }} + - template: {{ site.get('template_engine', 'jinja') }} + - context: + id: {{ id|json }} + site: {{ site|json }} + map: {{ apache|json }} + - require: + - pkg: apache + - watch_in: + - service: apache + +{% if grains.os_family == 'Debian' %} +a2ensite {{ id }}: + cmd: + - run + - require: + - file: {{ id }} +{% endif %} + +{% endfor %} diff --git a/apache/vhosts/standard.tmpl b/apache/vhosts/standard.tmpl new file mode 100644 index 0000000..cb095b6 --- /dev/null +++ b/apache/vhosts/standard.tmpl @@ -0,0 +1,64 @@ +{# Define default values here so the template below can just focus on layout #} +{% set sitename = site.get('ServerName', id) %} +{% set vals = { + 'interface': site.get('interface', '*'), + 'port': site.get('port', '80'), + + 'ServerName': sitename, + 'ServerAlias': site.get('ServerAlias', 'www.{0}'.format(sitename)), + + 'ServerAdmin': site.get('ServerAdmin', 'webmaster@{0}'.format(sitename)), + + 'LogLevel': site.get('LogLevel', 'warn'), + 'ErrorLog': site.get('ErrorLog', '{0}/{1}-error.log'.format(map.logdir, sitename)), + 'CustomLog': site.get('ErrorLog', '{0}/{1}-access.log'.format(map.logdir, sitename)), + + 'DocumentRoot': site.get('DocumentRoot', '{0}/{1}'.format(map.wwwdir, sitename)), + + 'Directory_default': '{0}/{1}'.format(map.wwwdir, sitename), + 'Directory': { + 'Options': '-Indexes FollowSymLinks', + 'Order': 'allow,deny', + 'Allow': 'from all', + 'AllowOverride': 'None', + }, +} %} + + + ServerName {{ vals.ServerName }} + {% if site.get('ServerAlias') != False %}ServerAlias {{ vals.ServerAlias }}{% endif %} + + {% if site.get('ServerAdmin') != False %}ServerAdmin {{ vals.ServerAdmin }}{% endif %} + + {% if site.get('LogLevel') != False %}LogLevel {{ vals.LogLevel }}{% endif %} + {% if site.get('ErrorLog') != False %}ErrorLog {{ vals.ErrorLog }}{% endif %} + {% if site.get('CustomLog') != False %}CustomLog {{ vals.CustomLog }}{% endif %} + + {% if site.get('DocumentRoot') != False %}DocumentRoot {{ vals.DocumentRoot }}{% endif %} + + {% for path, dir in site.get('Directory', {}).items() %} + {% set dvals = { + 'Options': dir.get('Options', vals.Directory.Options), + 'Order': dir.get('Order', vals.Directory.Order), + 'Allow': dir.get('Order', vals.Directory.Allow), + 'AllowOverride': dir.get('Order', vals.Directory.AllowOverride), + } %} + + {% if path == 'default' %}{% set path = vals.Directory_default %}{% endif %} + + + {% if dir.get('Options') != False %}Options {{ dvals.Options }}{% endif %} + {% if dir.get('Order') != False %}Order {{ dvals.Order }}{% endif %} + {% if dir.get('Allow') != False %}Allow {{ dvals.Allow }}{% endif %} + {% if dir.get('AllowOverride') != False %}AllowOverride {{ dvals.AllowOverride }}{% endif %} + + {% if dir.get('Formula_Append') %} + {{ dir.Formula_Append|indent(8) }} + {% endif %} + + {% endfor %} + + {% if site.get('Formula_Append') %} + {{ site.Formula_Append|indent(4) }} + {% endif %} + diff --git a/pillar.example b/pillar.example index 4a79e0b..5968db5 100644 --- a/pillar.example +++ b/pillar.example @@ -2,9 +2,54 @@ apache: server: apache2 service: apache2 - conf: /etc/apache2/conf.d + + confdir: /etc/apache2/conf.d + logdir: /var/log/apache2 + wwwdir: /srv/apache2 + +# ``apache.mod_wsgi`` formula additional configuration: +apache: mod_wsgi: mod_wsgi +# ``apache.vhosts`` formula additional configuration: +apache: + sites: + # Default values below are used unless disabled by setting to 'False'. + example.com: # must be unique; used as an ID declaration in Salt; also passed to the template context as {{ id }} + template_file: salt://apache/vhosts/standard.tmpl + template_engine: jinja + + interface: '*' + port: '80' + + ServerName: {{ id }} # uses the unique ID above unless specified + ServerAlias: www.{{ id }} + + ServerAdmin: webmaster@{{ id }} + + LogLevel: warn + ErrorLog: {{ logdir }}/{{ id }}-error.log # E.g.: /var/log/apache2/example.com-error.log + CustomLog: {{ logdir }}/{{ id }}-access.log # E.g.: /var/log/apache2/example.com-access.log + + DocumentRoot: {{ wwwdir }}/{{ id }} # E.g., /var/www/example.com + + Directory: + default: # "default" is a special case; Adds ``{{ wwwdir }}/{{ id }}`` e.g.: /var/www/example.com + Options: -Indexes FollowSymLinks + Order: allow,deny + Allow: from all + AllowOverride: None + Formula_Append: | + Additional config as a + multi-line string here + + Formula_Append: | + Additional config as a + multi-line string here + + example.net: + template_file: salt://apache/vhosts/minimal.tmpl + # ``apache.debian_full`` formula additional configuration: apache: register-site: