Use Salt States to Configure a LAMP Stack on a Minion

Updated by Linode

Contribute on GitHub

Report an Issue | 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:

         - lamp
         - extras
         - lampconf
  2. Create and edit the /etc/salt/base/lampconf.sls file:

     1 2 3 4 5 6 7 8 910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
    #Apache Conguration for 2GB Linode
        - name: /etc/apache2/apache2.conf
        - pattern: 'KeepAlive On'
        - repl: 'KeepAlive Off'
        - show_changes: True
        - name: /etc/apache2/apache2.conf
        - text: |
            <IfModule mpm_prefork_module>
            StartServers 4
            MinSpareServers 20
            MaxSpareServers 40
            MaxClients 200
            MaxRequestsPerChild 4500
    # MySQL Configuration for 2GB Linode
        - 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
        - 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
        - name: /etc/php5/apache2/php.ini
        - pattern: ';error_log = php_errors.log'
        - repl: 'error_log = /var/log/php/error.log'
        - show_changes: True
        - user: www-data
        - group: root
        - dir_mode: 755
        - file_mode: 644
        - makedirs: True
    # Restart
        - name: apache2
        - enable: True
        - watch:
          - pkg: apache2
        - 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:

    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:

    salt '<hostname or Minion ID>' "a2dissite *default"

    For all Minions:

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

    salt '<hostname or Minion ID>' file.makedirs /var/www/
    salt '<hostname or Minion ID>' file.makedirs /var/www/
    salt '<hostname or Minion ID>' file.makedirs /var/www/
  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.

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

     1 2 3 4 5 6 7 8 91011121314151617
    # domain:
    # public: /var/www/
    <VirtualHost *:80>
      # Admin email, Server Name (domain name), and any aliases
      # Index file and Document Root (where the public files are located)
      DirectoryIndex index.html index.php
      DocumentRoot /var/www/
      # Log file locations
      LogLevel warn
      ErrorLog  /var/www/
      CustomLog /var/www/ combined

  5. Copy the vhost file from the Master to the /sites-available directory of the Minion:

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

    salt '<hostname or Minion ID>' "a2ensite"
    salt '<hostname or Minion ID>' "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.

See Also

comments powered by Disqus

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