How to use SaltStack to manage Nginx and MySQL (4/4)
saltstack

How to use SaltStack to manage Nginx and MySQL (4/4)

SaltStack makes managing vhosts and other configuration options very simple - Let's start with a very basic example of managing a file with the following state file: Open the file /srv/salt/vhosts/example.com.sls

/etc/nginx/sites-available/www.example.com.conf:
  file:
    - managed
    - source: salt://files/templates/nginx.conf
    - template: jinja
    - context:
      - domain: example.com

/etc/nginx/sites-enabled/www.example.com.conf:
  file:
    - symlink
    - target: /etc/nginx/sites-available/www.example.com.conf

This little snippet makes sure that the file, /etc/nginx/sites-available/www.example.com.conf - contains the rendered version of salt://files/templates/nginx.conf. So, how do we make that file? Easy! Create the directory /srv/salt/files/templates and edit the file nginx.conf:

server {
    listen 80 ssl;
    server_name {{ domain }} www.{{domain}};
    access_log /var/log/nginx/{{ domain }}.access.log;
        error_log /var/log/nginx/{{ domain }}.error.log;
    root /home/{{ domain }}/html/public
    index index.php;
    underscores_in_headers on;
    ignore_invalid_headers   off;
    location / {
               # First attempt to serve request as file, then
               # as directory, then fall back to index.php
               try_files $uri $uri/ /index.php?$args;
                 expires       340h;
       }
    location ~ .php$ {
                fastcgi_split_path_info ^(.+.php)(/.+)$;
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                # With php5-cgi alone:
                #fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
}

The {{ domain }} tags get substituted with the domain variable inside the context array in the state file. Another nice thing we can use salt for is to automatically check out a git repository. Add the following to /srv/salt/vhosts/example_com.sls

checkout-git-example.com:
  git.latest:
    - name: https://github.com/laravel/laravel.git
    - rev:  master
    - target: /home/example.com/html
    - require:
        - pkg: base-packages

Lastly, we also want to make sure we have the databases setup. Open the file /srv/salt/databases/example_com.sls and add the following contents:

mysql-example.com-user:
  mysql_user.present:
    - name: example_com
    - password: randompassword
    - host: localhost
    - connection_user: root
    - connection_charset: utf8
    - require:
      - pkg: mysqld
      - pkg: mysql_python
      - service: mysqld

mysql-example.com-database:
  mysql_database.present:
    - name: example_com
    - connection_user: root
    - connection_charset: utf8
    - require:
      - pkg: mysqld
      - pkg: mysql_python
      - service: mysqld

mysql-example.com-grant:
  mysql_grants.present:
    - grant: all privileges
    - database: example_com.*
    - user: example_com
    - host: localhost
    - connection_user: root
    - connection_charset: utf8
    - require:
      - mysql_database: mysql-example.com-database
      - mysql_user: mysql-example.com-user
      - pkg: mysqld

Now, edit your top.sls file to match the following:

base:
  ‘‘:
    - base.base
  ‘web‘:
    - base.webstack
    - vhosts.example_com
  ‘db*’:
    - base.dbstack
    - databases.example_com

Was this article helpful?