I am available for freelance

Hi, I'mVishal Kanade

DevOps Engineer | Developer

Download Resume

Deploying LAMP stack and Installing WordPress using Ansible

Installing Ansible on Ubuntu 

  • apt-get install software-properties-common
  • apt-add-repository ppa:ansible/ansible
  • apt-get update
  • apt-get install ansible

Configuring the host file

  • Visit -> /etc/ansible/hosts and Edit the file 
  • Add your node (client) IP in the file and give it a group name

Here’s the playbook for installing Mysql server, PHP, Apache and WordPress

---
- hosts: server
  sudo: yes
  vars:
    db_name: WORDPRESSDBNAME
    db_user: WORDPRESSUSERNAME
    db_password: WORDPRESSPASSWORD
    loginuser: MYSQLUSERNAME
    loginpass: MYSQLPASSWORD
  
  tasks:
  - name: Installing Mysql server, PHP, Apache and WordPress
    apt:
      name: "{{item}}"
      state: present
      update_cache: yes
      cache_valid_time: 3600
    with_items:
      - mysql-server
      - apache2
      - libapache2-mod-php
      - php-mysql
      - python-mysqldb
      - python-httplib2
  
  - name: Creating WordPress DB
    mysql_db:
      name: "{{ db_name }}"
      state: present
      login_user: "{{ loginuser }}"
      login_password: "{{ loginpass }}"
  
  - name: Creating a WordPress user
    mysql_user:
      name: "{{ db_user }}"
      password: "{{ db_password }}"
      priv: "{{ db_name }}.*:ALL"
      state: present
      login_user: "{{ loginuser }}"
      login_password: "{{ loginpass }}"
  
  - name: Downloading WordPress
    get_url:
      url: http://wordpress.org/latest.tar.gz
      dest: /tmp/wordpress.tar.gz
  
  - name: Extracting WordPress to apache directory
    unarchive:
      src: /tmp/wordpress.tar.gz
      dest: /var/www/html/
      copy: no

  - name: Creating the directory for WordPress
    file:
      path: /var/www/html/wordpress/wp-content/uploads
      state: directory
      mode: 0755

  - name: Restarting Apache and MySQL services
    service: name={{ item }} state=restarted
    with_items:
    - apache2
    - mysql

Finally run the playbook using following command

ansible-playbook playbookname.yml

If all goes well, we should be able to see the result as below

ansible

Leave a comment