Ansible playbook NGINX + PHP

In this tutorial, I will deploy  Ansible playbook Nginx and PHP for remote server centos 7. Ansible the essential for DevOps Roles.

My system:

Control server: DevopsRoles —–> Remote server: webserver

Control server:

Installed Ansible “I use root account running Ansible, You are understand? Please careful

Remote server:

  •  Install webserver Nginx and PHP
  •  Create a user huupv with sudo privileges ( more details ref to linux create user )

Ansible playbook NGINX + PHP

Create a folder and file for Ansible Nginx

[root@DevopsRoles ~]# pwd
/root
[root@DevopsRoles ~]# mkdir ansible-nginx
[root@DevopsRoles ~]# mkdir ansible-nginx/inventory
[root@DevopsRoles ~]# vi ansible-nginx/webserver-main.yml

The content  webserver-main.yml file as below

- name: install and configure the web server
  hosts: webserver
  remote_user: huupv
  become: yes

  roles:
    - nginx
    - php

Create file hosts

[root@DevopsRoles ~]# vi ansible-nginx/inventory/hosts

The content is as below:

[webserver]
192.168.1.113

In this command, checking for communication use the ping module ansible.

[root@DevopsRoles ansible-nginx]# ansible all -i inventory/hosts -m ping

192.168.1.113 | SUCCESS => {
"changed": false, 
"ping": "pong"
}

Ansible roles Nginx and PHP

[root@DevopsRoles ~]# mkdir ansible-nginx/roles
[root@DevopsRoles ~]# cd ansible-nginx/roles
[root@DevopsRoles roles]#

create the role for Nginx and PHP. Using ansible-galaxy creates a template

[root@DevopsRoles roles]# ansible-galaxy init nginx
- nginx was created successfully
[root@DevopsRoles roles]# ansible-galaxy init php
- php was created successfully

Ansible roles Nginx create by ansible-galaxy

Ansible structure folder and file as below

nginx
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
└── main.yml

8 directories, 8 files

Setting ansible task Nginx

The modify nginx/tasks/main.yml for role Nginx

[root@DevopsRoles roles]# vi nginx/tasks/main.yml

The content main.yml file as below

# tasks file for nginx
- name: Install NGINX.
  yum: name=nginx
- command: chkconfig nginx on
- command: service nginx restart
- name: Install NGINX.
  yum: name={{ item }}
  with_items:
    - http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

  # enabled=0
  - replace: dest=/etc/yum.repos.d/{{ item }} regexp="enabled *= *1" replace="enabled=0"
  with_items:
     - nginx.repo
  - yum: name=nginx
  - command: chkconfig nginx on
  - command: service nginx restart

Ansible roles PHP create by ansible-galaxy

Ansible structure folder and file as below

php
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
└── main.yml

8 directories, 8 files

Setting ansible task PHP

The Modify php/tasks/main.yml for role PHP

[root@DevopsRoles roles]# vi php/tasks/main.yml

The content is as below:

# tasks file for php
- name: Install PHP
  yum: name={{ item }} state=present enablerepo=epel
  with_items:
   - php70u-cli
   - php70u-common
   - php70u-fpm
   - php70u-fpm-nginx
   - php70u-json
   - php70u-mysqlnd
   - php70u-opcache
   - php70u-pdo

Ansible tasks/main.yml installed modules of PHP from yum in succession. Convenient!

Run ansible playbook

Run check the syntax of the Ansible playbook

[root@DevopsRoles ansible-nginx]# ansible-playbook -C -i inventory/hosts webserver-main.yml

Checking result syntax ok run ansible-playbook again

[root@DevopsRoles ansible-nginx]# ansible-playbook -i inventory/hosts webserver-main.yml

The confirm installed Nginx and PHP on a host web server

Nginx version

Ansible playbook NGINX

PHP version

Ansible playbook PHP

Remember to customize the playbook and PHP-FPM configuration based on your specific requirements.

Conclusion

Through the article, you can use the Ansible playbook NGINX + PHP as above. I hope will this your helpful. For more details refer to the Ansible tutorial.

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.