Use Salt States to Configure a LAMP Stack on a Minion

Updated by Linode

Contribute on GitHub

View Project | View File | Edit File

This tutorial will configure a Minion’s LAMP stack with further use of Salt States. This tutorial is written for Debian 8 but can easily be adjusted for other Linux Distributions. You will need a working Salt master and minion configuration before starting this guide. If you need to set up that prerequisite, see our Salt installation guide to get started.

Create the LAMP Configuration States

The steps below configure all Salt Minions for a 2GB Linode, feel free to adjust as needed.

  1. Open the /etc/salt/base/top.sls file and add the additional line:

    /etc/salt/base/top.sls
    1
    2
    3
    4
    5
    base:
      '*':
         - lamp
         - extras
         - lampconf
    
  2. Create and edit the /etc/salt/base/lampconf.sls file:

    /etc/salt/base/lampconf.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
    #Apache Conguration for 2GB Linode
    /etc/apache2/apache2.conf-KA:
      file.replace:
        - name: /etc/apache2/apache2.conf
        - pattern: 'KeepAlive On'
        - repl: 'KeepAlive Off'
        - show_changes: True
    
    /etc/apache2/apache2.conf-IM:
      file.append:
        - name: /etc/apache2/apache2.conf
        - text: |
            <IfModule mpm_prefork_module>
            StartServers 4
            MinSpareServers 20
            MaxSpareServers 40
            MaxClients 200
            MaxRequestsPerChild 4500
            </IfModule>
    
    #MySQL Configuration for 2GB Linode
    /etc/mysql/my.cnf-br:
      file.blockreplace:
        - name: /etc/mysql/my.cnf
        - marker_start: '# * Fine Tuning'
        - marker_end: '# * Query Cache Configuration'
        - content: |
            #
            key_buffer             = 32M
            max_allowed_packet     = 1M
            thread_stack           = 128K
            thread_cache_size      = 8
            # This replaces the startup script and checks MyISAM tables if
            # needed the first time they are touched
            myisam-recover         = BACKUP
            max_connections        = 75
            table_cache            = 32
            #thread_concurrency    = 10
            #
        - backup: '.bak'
        - show_changes: True
    
    #PHP Configuration for 2GB Linode
    /etc/php5/apache2/php.ini-er:
      file.replace:
        - name: /etc/php5/apache2/php.ini
        - pattern: 'error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT'
        - repl: 'error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR'
        - show_changes: True
    
    /etc/php5/apache2/php.ini-el:
      file.replace:
        - name: /etc/php5/apache2/php.ini
        - pattern: ';error_log = php_errors.log'
        - repl: 'error_log = /var/log/php/error.log'
        - show_changes: True
    
    /var/log/php/error.log:
      file.managed:
        - user: www-data
        - group: root
        - dir_mode: 755
        - file_mode: 644
        - makedirs: True
    
    #Restart
    apache2-run-at-boot-restart:
      service.running:
        - name: apache2
        - enable: True
        - watch:
          - pkg: apache2
    
    mysql-run-at-boot-restart:
      service.running:
        - name: mysql
        - enable: True
        - watch:
          - pkg: mysql-server
    

    The above file uses the file and service Salt State modules.

  3. Transfer the State settings to the Minions:

    1
    salt '*' state.highstate
    

Create Virtual Hosts Files

Salt State Modules are used for settings across groups of Minions. To adjust a configuration on a single Minion, try using Salt Execution Modules. Note, there are many ways to use Salt.

  1. Disable the default Apache virtual host on either a single Minion or all Minions:

    For a specific Minion:

    1
    salt '<hostname or Minion ID>' cmd.run "a2dissite *default"
    

    For all Minions:

    1
    salt '*' cmd.run "a2dissite *default"
    
  2. Create directories for the website’s files, logs, and backups. Replace example.com with the name of the website:

    1
    2
    3
    salt '<hostname or Minion ID>' file.makedirs /var/www/example.com/pubic_html/
    salt '<hostname or Minion ID>' file.makedirs /var/www/example.com/log/
    salt '<hostname or Minion ID>' file.makedirs /var/www/example.com/backups/
    
  3. Create a directory on the Master to hold all of the Minion virtual host files. This directory can act as an index for all of the Minion websites.

    1
    mkdir /etc/salt/base/minionsites
    
  4. Create the /etc/salt/base/minionsites/example.com.conf vhost file for the specified Minion. Replace example.com throughout and in the following commands.

    /etc/salt/base/minionsites/example.com.conf
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    # domain: example.com
    # public: /var/www/example.com/public_html/
    
    <VirtualHost *:80>
      # Admin email, Server Name (domain name), and any aliases
      ServerAdmin webmaster@example.com
      ServerName  www.example.com
      ServerAlias example.com
    
      # Index file and Document Root (where the public files are located)
      DirectoryIndex index.html index.php
      DocumentRoot /var/www/example.com/public_html
      # Log file locations
      LogLevel warn
      ErrorLog  /var/www/example.com/log/error.log
      CustomLog /var/www/example.com/log/access.log combined
    </VirtualHost>
    
  5. Copy the vhost file from the Master to the /sites-available directory of the Minion:

    1
    salt-cp '<hostname or Minion ID>' /etc/salt/base/minionsites/example.com.conf /etc/apache2/sites-available/example.com.conf
    
  6. Enable the new website and restart Apache:

    1
    2
    salt '<hostname or Minion ID>' cmd.run "a2ensite example.com.conf"
    salt '<hostname or Minion ID>' cmd.run "service apache2 reload"
    

The above section used the cmdmod, file, and cp Salt Execution modules.

You should now have a configured LAMP stack across as many Minions as you wanted. Optionally, use grains for further customization and to apply specific variables to each host.

This guide is published under a CC BY-ND 4.0 license.