Having trouble thinking of a structure for Nginx+PHP setup, with multiple vhosts.

Hey,

I have several servers and each server runs Nginx and Php-fpm.

On each server I have several Vhosts, each Vhost (or a group of several) is owned by a linux user and is tied to a Php-fpm tool.

I think that I need to have the following roles:

  • User
  • Nginx Vhost
  • Php-fpm pool

My goal is to be able to add/remove vhosts and php pools using config file. Eventually, I want to have the following files set:
/etc/nginx/sites-enabled/{{ vhost }}
/etc/php5/fpm/pool.d/{{ username}}.{{ poolname }}.conf
/home/{{ username }}/{{ vhost }}/public

The problem is that I’m very confused of how to structure it.

One way of doing it, is to define my physical servers as [groups] and the hosts as my vhosts. Then I can set a group var that will use the same connection settings for all the hosts/vhosts.

For each host/vhost I’ll set a var that will indicate it’s linux user and a var for the php pool. Then the playbook will load var files according to the user and pool.

Is that a good idea? Is there another way of doing it?

Is it possible to define a JSON var and then iterate it inside a playbook?

Thanks for reading :slight_smile:

Ansible variables are defined in YAML, not JSON.

No, hosts should be actual things you should SSH to and not Apache vhosts.

You may want to manage them with parameterized roles though, and have a role to configure each vhost or a generic role to set up a vhost, etc.

Thanks Michael,

I’m aware of parameterized roles, can I do with_items on parameterized roles?

Each host may have several vhosts. So probably I’ll have a list of vhosts per server, and each vhost will have several vars.

Am I on the right direction?

Thanks.

No, the best thing to do is to use the with_items inside the role.

May be I’m not following best practices, but my production ansible’s nginx.yaml looks like this: https://gist.github.com/sashka/6644589

I hope it’d helped.

Alexander.

Thanks for your reply James.

So you’re saying that the role for vhost should deal with all the vhost configurations and not for a single vhost config? Did I get it right?

Thanks.