Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Managing WordPress with Ansible

David Murphy
December 04, 2015

Managing WordPress with Ansible

Slides from my talk at WordCamp US, December 2015

David Murphy

December 04, 2015
Tweet

More Decks by David Murphy

Other Decks in Technology

Transcript

  1. if you are going to do something more than once,

    automate it — me (and many others) 13 — David Murphy @ WordCamp US, December 2015
  2. an abundance of choices: Ubuntu • Debian FreeBSD • OpenBSD

    • NetBSD Fedora • CentOS openSUSE • Slackware Gentoo • Arch Linux CoreOS 18 — David Murphy @ WordCamp US, December 2015
  3. a plethora of choices: Bluehost • InMotion Hosting • DreamHost

    HostGator • WP Engine • GoDaddy Pagely • Media Temple DigitalOcean • Linode • Vultr Amazon Web Services Google Cloud Platform Microsoft Azure 22 — David Murphy @ WordCamp US, December 2015
  4. Ansible is a powerful automation tool that you can learn

    quickly. 32 — David Murphy @ WordCamp US, December 2015
  5. sample playbook --- - hosts: webservers remote_user: root tasks: -

    name: ensure apache is at the latest version yum: pkg=httpd state=latest - name: write the apache config file template: src=/srv/httpd.j2 dest=/etc/httpd.conf - hosts: databases remote_user: root tasks: - name: ensure postgresql is at the latest version yum: name=postgresql state=latest - name: ensure that postgresql is started service: name=postgresql state=running 42 — David Murphy @ WordCamp US, December 2015
  6. yum - name: install the latest version of Apache yum:

    name=httpd state=latest - name: remove the Apache package yum: name=httpd state=absent - name: install the latest version of Apache from↩ the testing repo yum: name=httpd enablerepo=testing state=present - name: install one specific version of Apache yum: name=httpd-2.2.29-1.4.amzn1 state=present 44 — David Murphy @ WordCamp US, December 2015
  7. template # Example from Ansible Playbooks - template: src=/mytemplates/foo.j2 dest=/etc/file.conf↩

    owner=bin group=wheel mode=0644 # The same example, but using symbolic modes equivalent to 0644 - template: src=/mytemplates/foo.j2 dest=/etc/file.conf↩ owner=bin group=wheel mode="u=rw,g=r,o=r" 45 — David Murphy @ WordCamp US, December 2015
  8. service # Example action to start service httpd, if not

    running - service: name=httpd state=started # Example action to stop service httpd, if running - service: name=httpd state=stopped # Example action to restart service httpd, in all cases - service: name=httpd state=restarted # Example action to reload service httpd, in all cases - service: name=httpd state=reloaded # Example action to enable service httpd, and not touch↩ the running state - service: name=httpd enabled=yes 46 — David Murphy @ WordCamp US, December 2015
  9. modules, modules, modules → command, get_url → apt, apt_key, apt_repository

    → digital_ocean, linode, ec2, gce, docker → irc, slack, hipchat, mail → mysql_db, mysql_user → git, subversion 47 — David Murphy @ WordCamp US, December 2015
  10. famous 5-minute install1 → download/unzip → create database → edit

    config → upload 1 http://codex.wordpress.org/InstallingWordPress#Famous5- Minute_Install 51 — David Murphy @ WordCamp US, December 2015
  11. a better example → install packages → create database, then

    database user → download/unzip → edit config → configure webserver → ensure webserver is running 52 — David Murphy @ WordCamp US, December 2015
  12. first, some variables vars: db_name: wcus db_user: wcus db_password: correcthorsebatterystaple

    site_name: wcus.schwuk.com site_alias: wcus 53 — David Murphy @ WordCamp US, December 2015
  13. install the packages - name: Install mysql-server apt: name=mysql-server state=present↩

    update_cache=yes cache_valid_time=3600 - name: Install apache2 apt: name=apache2 state=present - name: Install libapache2-mod-php5 apt: name=libapache2-mod-php5 state=present - name: Install php5-mysql apt: name=php5-mysql state=present - name: Install python-mysqldb apt: name=python-mysqldb state=present - name: Install python-httplib2 apt: name=python-httplib2 state=present 54 — David Murphy @ WordCamp US, December 2015
  14. install the packages, take two - name: Install required packages

    apt: name: "{{ item }}" state: present update_cache: yes cache_valid_time: 3600 with_items: - mysql-server - apache2 - libapache2-mod-php5 - php5-mysql - python-mysqldb - python-httplib2 55 — David Murphy @ WordCamp US, December 2015
  15. setup the database - name: Create WordPress database mysql_db: name:

    "{{ db_name }}" state: present - name: Create WordPress user mysql_user: name: "{{ db_user }}" password: "{{ db_password }}" priv: "{{ db_name }}.*:ALL" state: present 56 — David Murphy @ WordCamp US, December 2015
  16. install WordPress - name: Create directory for WordPress file: path:

    /srv/{{ site_alias }} state: directory mode: 0755 - name: Get WordPress get_url: url: http://wordpress.org/latest.tar.gz dest: /tmp/wordpress.tar.gz - name: Extract WordPress unarchive: src: /tmp/wordpress.tar.gz dest: /srv/{{ site_alias }} copy: no 57 — David Murphy @ WordCamp US, December 2015
  17. edit the config - name: Generate Keys and Salts uri:

    url: https://api.wordpress.org/secret-key/1.1/salt/ dest: /srv/{{ site_alias }}/keys.php creates: /srv/{{ site_alias }}/keys.php - name: Configure WordPress template: src: wp-config.php dest: /srv/{{ site_alias }}/wordpress/wp-config.php owner: root group: root mode: 0644 58 — David Murphy @ WordCamp US, December 2015
  18. wp-config.php <?php define('DB_NAME', '{{ db_name }}'); define('DB_USER', '{{ db_user }}');

    define('DB_PASSWORD', '{{ db_password }}'); define('DB_HOST', 'localhost'); define('DB_CHARSET', 'utf8'); define('DB_COLLATE', ''); include dirname( __FILE__ ) . '../keys.php'; $table_prefix = 'wp_'; define('WP_DEBUG', false); if ( !defined('ABSPATH') ) define('ABSPATH', dirname(__FILE__) . '/'); require_once(ABSPATH . 'wp-settings.php'); 59 — David Murphy @ WordCamp US, December 2015
  19. configure the webserver - name: Configure webserver template: src: apache-site.conf

    dest: /etc/apache2/sites-available/{{ site_alias }}.conf owner: root group: root mode: 0644 notify: - restart apache - name: Disable default site_alias command: a2dissite 000-default - name: Enable WordPress site command: a2ensite {{ site_alias }} 60 — David Murphy @ WordCamp US, December 2015
  20. there's more than one way to do it — Perl

    community (and Larry Wall) 68 — David Murphy @ WordCamp US, December 2015