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:

/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 file (and probably the directory under it).

/srv/salt/files/templates/nginx.conf

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:
#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 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:

/srv/salt/vhosts/example_com.sls

and add the following:

checkout-git-example.com:
git.latest:
- name: https://github.com/laravel/laravel.git
- rev: master
- target: /home/example.com/html
- 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:

/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

Conclusion

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