Configure Apache with Salt Stack
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Salt is a powerful configuration management tool. In this guide you will create Salt state files that are capable of installing and configuring Apache on Ubuntu 18.04, Debian 9, or CentOS 7.
Before You Begin
You will need at least two Linodes with Salt installed. If you have not already, read our Getting Started with Salt - Basic Installation and Setup Guide and follow the instructions for setting up a Salt master and minion.
The following steps will be performed on your Salt master.
root or with the sudo prefix. For more information on privileges, see our
Users and Groups guide.Setting Up Your Salt Master and Managed Files
Salt Master SLS Files
Create the
/srv/saltdirectory if it does not already exist:mkdir /srv/saltCreate a Salt top file in
/srv/saltthat will be Salt’s entry point to the Apache configuration:- File: /srv/salt/top.sls
1 2 3 4 5 6 7 8base: 'G@os_family:Debian': - match: compound - apache-debian 'G@os:CentOS': - match: compound - apache-centos
This top file uses compound matching to target your minions by operating system using Salt Grains. This will allow Salt to choose the appropriate Apache configuration depending on the Linux distribution. These matchers could be extended to be even more specific. For instance, if you wanted to only target minions with the ID of
web-serverthat are running on Ubuntu, you can typeweb* and G@os:Ubuntu.
Pillar Files
Create the
/srv/pillardirectory if it does not already exist:mkdir /srv/pillarCreate a Pillar top file. This top file references the
apache.slsPillar file that you will create in the next step:- File: /srv/pillar/top.sls
1 2 3base: '*': - apache
Create the
apache.slsfile that was referenced in the previous step. This file defines Pillar data that will be used inside our Apache state file in the next section, in this case your domain name. Replaceexample.comwith your domain:- File: /srv/pillar/apache.sls
1domain: example.com
Website Files
Create a directory for your website files in the
/srv/saltdirectory. Replaceexample.comwith your website domain name:mkdir /srv/salt/example.comThis directory will be accessible from your Salt state files at
salt://example.com.Create an
index.htmlfile for your website in the/srv/salt/example.comdirectory, substitutingexample.comfor the folder name you chose in the previous step. You will use this file as a test to make sure your website is functioning correctly.- File: /srv/salt/example.com/index.html
1 2 3 4 5<html> <body> <h1>Server Up and Running!</h1> </body> </html>
Configuration Files
Create a folder for your additional configuration files at
/srv/salt/files. These files will be accessible atsalt://files.mkdir /srv/salt/filesCreate a file called
tune_apache.confin/srv/salt/filesand paste in the following block:- File: /srv/salt/files/tune_apache.conf
1 2 3 4 5 6 7<IfModule mpm_prefork_module> StartServers 4 MinSpareServers 20 MaxSpareServers 40 MaxClients 200 MaxRequestsPerChild 4500 </IfModule>
This MPM prefork module provides additional tuning for your Apache installation. This file will be managed by Salt and installed into the appropriate configuration directory in a later step.
If you will be installing Apache on a CentOS machine, create a file called
include_sites_enabled.confin/srv/salt/filesand paste in the following:- File: /srv/salt/files/include_sites_enabled.conf
1IncludeOptional sites-enabled/*.conf
This file will allow us to use file directories like those found on Debian installations to help organize the Apache configuration.
Creating the Apache State File for Debian and Ubuntu
Individual Steps
This guide will be going through the process of creating the Apache for Debian and Ubuntu state file step by step. If you would like to view the entirety of the state file, you can view it at the end of this section.
Create a state file named
apache-debian.slsin/srv/saltand open it in a text editor of your choice.Instruct Salt to install the
apache2package and start theapache2service:- File: /srv/salt/apache-debian.sls
1 2 3 4 5 6 7 8 9 10 11apache2: pkg.installed apache2 Service: service.running: - name: apache2 - enable: True - require: - pkg: apache2 ...
Here Salt makes sure the
apache2package is installed withpkg.installed. Likewise, it ensures theapache2service is running and enabled underservice.running. Also underservice.running,apache-debian.slsusesrequireto ensure that this command does not run before theapache2package is installed. Thisrequirestep will be repeated throughoutapache-debian.sls.Lastly, a
watchstatement is employed to restart theapache2service if your site’s configuration file changes. You will define that configuration file in a later step. Note that this configuration file is named using the domain you supplied when creating your Salt Pillar file in the first section. This Pillar data will be used throughoutapache-debian.sls.Turn off KeepAlive:
- File: /srv/salt/apache-debian.sls
1 2 3 4 5 6 7 8 9 10 11... Turn Off KeepAlive: file.replace: - name: /etc/apache2/apache2.conf - pattern: 'KeepAlive On' - repl: 'KeepAlive Off' - show_changes: True - require: - pkg: apache2 ...
KeepAlive allows multiple requests to be sent over the same TCP connection. For the purpose of this guide KeepAlive will be disabled. To disable it, Salt is instructed to find the KeepAlive directive in
/etc/apache2/apache2.confby matching a pattern and replacing it withKeepAlive Off.show_changesinstructs Salt to display any changes it has made during a highstate.Transfer
tune_apache.confto your minion and enable it:- File: /srv/salt/apache-debian.sls
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15... /etc/apache2/conf-available/tune_apache.conf: file.managed: - source: salt://files/tune_apache.conf - require: - pkg: apache2 Enable tune_apache: apache_conf.enabled: - name: tune_apache - require: - pkg: apache2 ...
This step takes the
tune_apache.conffile you created in the Configuration Files step and transfers it to your Salt minion. Then, Salt enables that configuration file with the apache_conf module.Create the necessary directories:
- File: /srv/salt/apache-debian.sls
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15... /var/www/html/{{ pillar['domain'] }}: file.directory /var/www/html/{{ pillar['domain'] }}/log: file.directory /var/www/html/{{ pillar['domain'] }}/backups: file.directory /var/www/html/{{ pillar['domain'] }}/public_html: file.directory ...
Disable the default virtual host configuration file:
- File: /srv/salt/apache-debian.sls
1 2 3 4 5 6 7 8... 000-default: apache_site.disabled: - require: - pkg: apache2 ...
This step uses Salt’s apache_site module to disable the default Apache virtual host configuration file, and is the same as running
a2dissiteon a Debian-based machine.Create your site’s virtual host configuration file:
- File: /srv/salt/apache-debian.sls
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16... /etc/apache2/sites-available/{{ pillar['domain'] }}.conf: apache.configfile: - config: - VirtualHost: this: '*:80' ServerName: - {{ pillar['domain'] }} ServerAlias: - www.{{ pillar['domain'] }} DocumentRoot: /var/www/html/{{ pillar['domain'] }}/public_html ErrorLog: /var/www/html/{{ pillar['domain'] }}/log/error.log CustomLog: /var/www/html/{{ pillar['domain'] }}/log/access.log combined ...
This step uses Salt’s apache module, (not to be confused with the
apache_sitemodule used in the previous step), to create your site’s virtual host configuration file. Thethisvariable signifies what would traditionally be include withVirtualHostwithin angle brackets in an Apache configuration file:<VirtualHost *:80>.Enable your new virtual host configuration file:
- File: /srv/salt/apache-debian.sls
1 2 3 4 5 6 7 8... {{ pillar['domain'] }}: apache_site.enabled: - require: - pkg: apache2 ...
This step uses the same
apache_sitemodule you used to disable the default virtual host file to enable your newly created virtual host file.apache_site.enabledcreates a symlink from/etc/apache2/sites-available/example.com.confto/etc/apache2/sites-enabled/example.com.confand is the same as runninga2ensiteon a Debian-based machine.Transfer your
index.htmlwebsite file to your minion:- File: /srv/salt/apache-debian.sls
1 2 3 4 5... /var/www/html/{{ pillar['domain'] }}/public_html/index.html: file.managed: - source: salt://{{ pillar['domain'] }}/index.html
Any changes made to your
index.htmlfile on your Salt master will be propagated to your minion.Note Since Salt is not watching configuration files for a change to trigger a restart for Apache, you may need to use the command below from your Salt master.
salt '*' apache.signal restart
Complete State File
The complete apache-debian.sls file looks like this:
- File: /srv/salt/apache-debian.sls
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69apache2: pkg.installed apache2 Service: service.running: - name: apache2 - enable: True - require: - pkg: apache2 Turn Off KeepAlive: file.replace: - name: /etc/apache2/apache2.conf - pattern: 'KeepAlive On' - repl: 'KeepAlive Off' - show_changes: True - require: - pkg: apache2 /etc/apache2/conf-available/tune_apache.conf: file.managed: - source: salt://files/tune_apache.conf - require: - pkg: apache2 Enable tune_apache: apache_conf.enabled: - name: tune_apache - require: - pkg: apache2 /var/www/html/{{ pillar['domain'] }}: file.directory /var/www/html/{{ pillar['domain'] }}/log: file.directory /var/www/html/{{ pillar['domain'] }}/backups: file.directory /var/www/html/{{ pillar['domain'] }}/public_html: file.directory 000-default: apache_site.disabled: - require: - pkg: apache2 /etc/apache2/sites-available/{{ pillar['domain'] }}.conf: apache.configfile: - config: - VirtualHost: this: '*:80' ServerName: - {{ pillar['domain'] }} ServerAlias: - www.{{ pillar['domain'] }} DocumentRoot: /var/www/html/{{ pillar['domain'] }}/public_html ErrorLog: /var/www/html/{{ pillar['domain'] }}/log/error.log CustomLog: /var/www/html/{{ pillar['domain'] }}/log/access.log combined {{ pillar['domain'] }}: apache_site.enabled: - require: - pkg: apache2 /var/www/html/{{ pillar['domain'] }}/public_html/index.html: file.managed: - source: salt://{{ pillar['domain'] }}/index.html
Creating an Apache State File for CentOS
Individual Steps
Create a file called
apache-centos.slsin/srv/saltand open it in a text editor of your choice.On CentOS Apache is named
httpd. Instruct Salt to installhttpdand run thehttpdservice:- File: /srv/salt/apache-centos.sls
1 2 3 4 5 6 7 8 9 10 11 12 13httpd: pkg.installed httpd Service: service.running: - name: httpd - enable: True - require: - pkg: httpd - watch: - file: /etc/httpd/sites-available/{{ pillar['domain'] }}.conf ...
Here Salt makes sure the
httpdpackage is installed withpkg.installed. Likewise, it ensures thehttpdservice is running and enabled underservice.running. Also underservice.running,apache-debian.slsusesrequireto ensure that this command does not run before thehttpdpackage is installed. Thisrequirestep will be repeated throughoutapache-centos.sls.Lastly, a
watchstatement is employed to restart thehttpdservice if your site’s configuration file changes. You will define that configuration file in a later step. Note that this configuration file is named using the domain you supplied when creating your Salt Pillar file in the first section. This Pillar data will be used throughoutapache-centos.sls.Turn off KeepAlive:
- File: /srv/salt/apache-centos.sls
1 2 3 4 5 6 7 8 9 10 11... Turn Off KeepAlive: file.replace: - name: /etc/httpd/conf/httpd.conf - pattern: 'KeepAlive On' - repl: 'KeepAlive Off' - show_changes: True - require: - pkg: httpd ...
KeepAlive allows multiple requests to be sent over the same TCP connection. For the purpose of this guide KeepAlive will be disabled. To disable it, Salt is instructed to find the KeepAlive directive in
/etc/httpd/conf/httpd.confby matching a pattern and replacing it withKeepAlive Off.show_changesinstructs Salt to display any changes it has made during a highstate.Change the DocumentRoot:
- File: /srv/salt/apache-centos.sls
1 2 3 4 5 6 7 8 9 10 11 12... Change DocumentRoot: file.replace: - name: /etc/httpd/conf/httpd.conf - pattern: 'DocumentRoot "/var/www/html"' - repl: 'DocumentRoot "/var/www/html/{{ pillar['domain'] }}/public_html"' - show_changes: True - require: - pkg: httpd ...
Similar to the last step, in this step
salt-centos.slsinstructs Salt to search for the DocumentRoot directive in Apache’shttpd.conffile, and replaces that line with the new document root. This allows for the use of a Debian-style site directory architecture.Transfer the
tune_apache.confandinclude_sites_enabled.confto your minion.- File: /srv/salt/apache-centos.sls
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15... /etc/httpd/conf.d/tune_apache.conf: file.managed: - source: salt://files/tune_apache.conf - require: - pkg: httpd /etc/httpd/conf.d/include_sites_enabled.conf: file.managed: - source: salt://files/include_sites_enabled.conf - require: - pkg: httpd ...
Create the necessary directories:
- File: srv/salt/apache-centos.sls
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18... /etc/httpd/sites-available: file.directory /etc/httpd/sites-enabled: file.directory /var/www/html/{{ pillar['domain'] }}: file.directory /var/www/html/{{ pillar['domain'] }}/backups: file.directory /var/www/html/{{ pillar['domain'] }}/public_html: file.directory ...
Create your site’s virtual host configuration file:
- File: /srv/salt/apache-centos.sls
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17... /etc/httpd/sites-available/{{ pillar['domain'] }}.conf: apache.configfile: - config: - VirtualHost: this: '*:80' ServerName: - {{ pillar['domain'] }} ServerAlias: - www.{{ pillar['domain'] }} DocumentRoot: /var/www/html/{{ pillar['domain'] }}/public_html file.symlink: - target: /etc/httpd/sites-enabled/{{ pillar['domain'] }}.conf - force: True ...
This step uses Salt’s apache module to create your site’s virtual host configuration file. The
thisvariable signifies what would traditionally be include withVirtualHostwithin angle brackets in an Apache configuration file:<VirtualHost *:80>.Transfer your
index.htmlwebsite file to your minion:- File: /srv/salt/apache-debian.sls
1 2 3 4 5 6 7... /var/www/html/{{ pillar['domain'] }}/public_html/index.html: file.managed: - source: salt://{{ pillar['domain'] }}/index.html ...
Any changes made to your
index.htmlfile on your Salt master will be propigated to your minion.Configure your firewall to allow http and https traffic:
- File: /srv/salt/apache-centos.sls
1 2 3 4 5 6 7 8 9... Configure Firewall: firewalld.present: - name: public - ports: - 22/tcp - 80/tcp - 443/tcp
Note It is imperative that you list all ports you need open to your machine in this section. Failure to list these ports will result in their closure by Salt.
Complete State File
The complete apache-centos.sls file looks like this:
- File: /srv/salt/apache-centos.sls
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82httpd: pkg.installed httpd Service: service.running: - name: httpd - enable: True - require: - pkg: httpd - watch: - file: /etc/httpd/sites-available/{{ pillar['domain'] }}.conf Turn off KeepAlive: file.replace: - name: /etc/httpd/conf/httpd.conf - pattern: 'KeepAlive On' - repl: 'KeepAlive Off' - show_changes: True - require: - pkg: httpd Change DocumentRoot: file.replace: - name: /etc/httpd/conf/httpd.conf - pattern: 'DocumentRoot "/var/www/html"' - repl: 'DocumentRoot "/var/www/html/{{ pillar['domain'] }}/public_html"' - show_changes: True - require: - pkg: httpd /etc/httpd/conf.d/tune_apache.conf: file.managed: - source: salt://files/tune_apache.conf - require: - pkg: httpd /etc/httpd/conf.d/include_sites_enabled.conf: file.managed: - source: salt://files/include_sites_enabled.conf - require: - pkg: httpd /etc/httpd/sites-available: file.directory /etc/httpd/sites-enabled: file.directory /var/www/html/{{ pillar['domain'] }}: file.directory /var/www/html/{{ pillar['domain'] }}/backups: file.directory /var/www/html/{{ pillar['domain'] }}/public_html: file.directory /etc/httpd/sites-available/{{ pillar['domain'] }}.conf: apache.configfile: - config: - VirtualHost: this: '*:80' ServerName: - {{ pillar['domain'] }} ServerAlias: - www.{{ pillar['domain'] }} DocumentRoot: /var/www/html/{{ pillar['domain'] }}/public_html file.symlink: - target: /etc/httpd/sites-enabled/{{ pillar['domain'] }}.conf - force: True /var/www/html/{{ pillar['domain'] }}/public_html/index.html: file.managed: - source: salt://{{ pillar['domain'] }}/index.html Configure Firewall: firewalld.present: - name: public - ports: - 22/tcp - 80/tcp - 443/tcp
Running the Apache State File
On your Salt master, issue a highstate command:
salt '*' state.apply
After a few moments you should see a list of Salt commands and a summary of their successes. Navigate to your website’s domain name if you have your DNS set up already, or your website’s public IP address. You should see your index.html file. You have now used Salt to configure Apache. Visit the links in the section below for more information.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on