Wednesday, July 17, 2013

Ansbile EC2 - setting up Nginx, MySQL, php, git

In this post, we will write a playbook that's going to set up a EC2 machine for a fully workable php environment.

Starting from a fresh machine with an attached ebs volume, we will do the following:

  1. Format the new ebs volume with XFS and mount it as /vol
  2. Install php, mysql and nginx
  3. Create a mysql user and create a database
  4. Copy the public and private keys into the targeted machine
  5. Checkout a project from github

Begin by spinning a fresh EC2 AMI and attach a ebs volume to it. Read Ansible - how to launch EC2 instances and setup the php environment.


Format the new ebs volume with XFS and mount it as /vol

We will mount the new ebs volume /dev/xvdf as /vol and format it with XFS

    - name: update machine with latest packages
      action: command yum -y update
    - name: install xfsprogs
      action: yum pkg=xfsprogs state=latest
    - name: format new volume
      filesystem: fstype=xfs dev=/dev/xvdf
    - name: edit fstab and mount the vol
      action: mount name={{mount_dir}} src=/dev/xvdf opts=noatime fstype=xfs state=mounted


Install php, mysql and nginx

    - name: install php
      action: yum pkg=php state=latest
    - name: install php-mysql
      action: yum pkg=php-mysql state=latest
    - name: install nginx
      action: yum pkg=nginx state=latest
    - name: ensure nginx is running
      action: service name=nginx state=started
    - name: install mysql server
      action: yum pkg=mysql-server state=latest
    - name: make sure mysql is running
      action: service name=mysqld state=started


Create a mysql user and a database

    - name: install python mysql
      action: yum pkg=MySQL-python state=latest
    - name: create database user
      action: mysql_user user=admin password=1234qwer priv=*.*:ALL state=present
    - name: create db
      action: mysql_db db=ansible state=present


Copy the public and private keys into the targeted machine

We want the target machine to be able to do a git pull without username and password prompts.

mkdir ~/.ssh
ssh-keygen -t rsa -C "you@email.com"

You will see:
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Just press Enter on the above prompts.

Two files will be generated: id_rsa, id_rsa.pub

Log in to Github and then Go to Account Settings -> SSH Keys

Add new key by giving it a name and pasting the content of id_rsa.pub

Test it by:
ssh -T git@github.com
Here are the Ansible tasks:

    - name: install git
      action: yum pkg=git state=latest
    - name: copy private key
      action: template src=~/.ssh/id_rsa.pub dest=~/.ssh/id_rsa.pub
    - name: copy public key
      action: template src=~/.ssh/id_rsa dest=~/.ssh/id_rsa


Checkout a project from github

    - name: git checkout source
      action: git repo=ssh://git@github.com:{your_git_repo}.git dest={{work_dir}} version=unstable


Full Ansible Playbook source:

- name: configure instances
hosts: ec2-servers
vars:
work_dir: /vol/src/application
user: ec2-user
sudo: yes
tasks:
- name: update machine with latest packages
action: command yum -y update
- name: install xfsprogs
action: yum pkg=xfsprogs state=latest
- name: format new volume
filesystem: fstype=xfs dev=/dev/xvdf
- name: edit fstab and mount the vol
action: mount name={{mount_dir}} src=/dev/xvdf opts=noatime fstype=xfs state=mounted
- name: install php
action: yum pkg=php state=latest
- name: install php-mysql
action: yum pkg=php-mysql state=latest
- name: install nginx
action: yum pkg=nginx state=latest
- name: ensure nginx is running
action: service name=nginx state=started
- name: install mysql server
action: yum pkg=mysql-server state=latest
- name: make sure mysql is running
action: service name=mysqld state=started
- name: install python mysql
action: yum pkg=MySQL-python state=latest
- name: create database user
action: mysql_user user=admin password=1234qwer priv=*.*:ALL state=present
- name: create db
action: mysql_db db=ansible state=present
- name: create src directory
action: file path={{work_dir}} state=directory owner=root group=root mode=0751
- name: install git
action: yum pkg=git state=latest
- name: copy private key
action: template src=~/.ssh/id_rsa.pub dest=~/.ssh/id_rsa.pub
- name: copy public key
action: template src=~/.ssh/id_rsa dest=~/.ssh/id_rsa
- name: git checkout source
action: git repo=ssh://git@github.com:{your_git_repo}.git dest={{work_dir}} version=unstable
view raw ansible_php.yml hosted with ❤ by GitHub

No comments:

Post a Comment