Creating a playbook for deploying a PHP application from a GitHub repository

Ansible has a module git that allows you to perform operations git on managed computers. Below is an example task to clone a GitHub repository to the folder /var/www/html/phpappname

- name: Clone phpappname repository
  git:
    repo: 'https://github.com/gitusername/phpappname.git'
    dest: /var/www/html/phpappname
Let's see an example of playbook (phpappname.yml) that will be executed on a computer that we call phpappname, which will be included in the hosts inventory file. The playbook includes a role, named phpappname.
Example: Playbook phpappname.yml
---

- name: Deploy phpappname PHP application from scratch
  hosts: phpappname
  roles:
    - phpappname
The role phpappname downloads Apache, PHP, and the application repository. Also, customize Apache to work on port 8080 instead of 80.
Example: Role phpappname
---

- name: Update package cache
  apt:
    update_cache: yes

- name: Install Apache and PHP
  apt:
    name: ['apache2', 'php']

- name: Clone phpappname repository
  git:
    repo: 'https://github.com/gitusername/phpappname.git'
    dest: /var/www/html/phpappname

- name: Change port to 8080 in /etc/apache2/ports.conf
  lineinfile:
    path: /etc/apache2/ports.conf
    regexp: '^Listen 80'
    line: 'Listen 8080'

- name: Change port to 8080 in /etc/apache2/sites-enabled/000-default.conf
  lineinfile:
    path: /etc/apache2/sites-enabled/000-default.conf
    regexp: '^<VirtualHost \*:80>'
    line: '<VirtualHost *:8080>'

- name: Restart Apache
  service:
    name: apache2
    state: restarted
After running the playbook with
$ ansible-playbook phpappname.yml --become

the app will be available in the folder phpappname on the provisioned server.

http://server_ip:8080/phpappname

No comments

Powered by Blogger.