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

In this tutorial, we’ll show you how to make a very simple config for Nginx and MySQL.

Creating a simple state file, and its needs for Nginx

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:

- managed
- source: salt://files/templates/nginx.conf
- template: jinja
- context:
- domain:
- symlink
- target: /etc/nginx/sites-available/

This little snippet makes sure that the file /etc/nginx/sites-available/ contains the rendered version of salt://files/templates/nginx.conf. So, how do we make that file? Easy! Create the file (and probably the directory under it).


And add the following:

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:
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;

The 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. Open:


and add the following:
- name:
- rev: master
- target: /home/
- require:
- pkg: base-packages

Creating a simple state file and its needs for our database

Lastly, we also want to make sure we have the setup of the database. Open the file:


and add the following contents:
- name: example_com
- password: randompassword
- host: localhost
- connection_user: root
- connection_charset: utf8
- require:
- pkg: mysqld
- pkg: mysql_python
- service: mysqld
- name: example_com
- connection_user: root
- connection_charset: utf8
- require:
- pkg: mysqld
- pkg: mysql_python
- service: mysqld
- grant: all privileges
- database: example_com.*
- user: example_com
- host: localhost
- connection_user: root
- connection_charset: utf8
- require:
- mysql_database:
- mysql_user:
- pkg: mysqld

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

- base.base
- base.webstack
- vhosts.example_com
- base.dbstack
- databases.example_com


After doing this tutorial series you should have created a saltmaster which can create and provision minions with a certain config.