• Ansible Playbook for Nginx Php Mysql

    Posted by Apoorv Singh | Last Updated: 09-Jan-19

    Ansible is an open source automation platform. It is, exceptionally easy to set up but amazingly powerful. Ansible can assist you with the configuration management, application deployment, task automation. It can run errands in succession and make a chain of occasions which must occur on multiple different servers or gadgets. For eg, lets say if you have multiple servers behind a load balancer, and you need to deploy a service on all of them. Ansible will deploy that service easily. While deploying, ansible will remove the server from your Load balancer and disable it from your monitoring service, deploy your service and reinstall the server in load balancer. Hence, you can do a hectic task easily with the Ansible.

    To setup Nginx-Php-Mysql with Ansible:


    - name: restart nginx
      service: name=nginx state=restarted enabled=yes
    - name: Install nginx
      yum: name=nginx state=present
    - name: Copy nginx configuration for wordpress
      template: src=default.conf dest=/etc/nginx/conf.d/default.conf
      notify: restart nginx
    server {
            listen       80 default_server;
            server_name  {{ server_hostname }};
            root /opt/website ;
    	client_max_body_size 64M;
    	location ~* /(?:uploads|files)/.*\.php$ {
                    deny all;
            location / {
                    index index.php index.html index.htm;
                    try_files $uri $uri/ /index.php?$args;
            location ~* \.(gif|jpg|jpeg|png|css|js)$ {
                    expires max;
            location ~ \.php$ {
                    try_files $uri =404;
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    fastcgi_index index.php;
                    fastcgi_pass  unix:/var/run/php-fpm/wordpress.sock;
                    fastcgi_param   SCRIPT_FILENAME
                    include       fastcgi_params;



    - name: restart php-fpm
      service: name=php-fpm state=restarted
    - name: Install php-fpm and deps
      yum: name={{ item }} state=present
        - php
        - php-fpm
        - php-mysql
        - php-PHPMailer
        - php-curl
        - php-common
    - name: Disable default pool
      command: mv /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.disabled creates=/etc/php-fpm.d/www.disabled
      notify: restart php-fpm
    - name: Copy php-fpm configuration
      template: src=wordpress.conf dest=/etc/php-fpm.d/
      notify: restart php-fpm
    listen = /var/run/php-fpm/php-fpm.sock
    listen.owner = www-data = www-data
    listen.mode = 0660
    user = apoorv
    group = apoorv
    pm = dynamic
    pm.max_children = 20
    pm.start_servers = 1
    pm.min_spare_servers = 1
    pm.max_spare_servers = 2
    pm.max_requests = 500
    chdir = /opt/website/
    php_admin_value[open_basedir] = /opt/website/:/tmp


    Mysql : 

    - name: restart mysql
      service: name=mysqld state=restarted
    - name: Install Mysql package
      yum: name={{ item }} state=present
       - mysql-server
    - name: Create Mysql configuration file
      template: src=my.cnf.j2 dest=/etc/my.cnf
      - restart mysql
    - name: Start Mysql Service
      service: name=mysqld state=started enabled=yes
    port={{ mysql_port }}



    # These are the WordPress database settings
    wp_db_name: wordpress 
    wp_db_user: wordpress
    wp_db_password: secret
    mysql_port: 3306



    - name: Install MySQL, Nginx, and PHP-FPM
      hosts: all
      remote_user: root
      # remote_user: user
      # become: yes
      # become_method: sudo
        - mysql
        - nginx
        - php-fpm



    Ansible is modular and easy to understand.

    Executing the Website.yml file will set up Nginx, Mysql, Php-fpm on a server. You can add more variables and configurations as per your requiement. 


    Ansible Playbooks 

    The main strength of Ansible lies in its Playbooks. A playbook resembles a formula or a directions manual which instructs Ansible when it connects with each machine. Playbooks are written in YAML, which simply could be seen as XML yet human readable.

Tags: DevOps