This is
brief tutorial on how to install wordpress on your remote Ubuntu server using
Ansible.
Well before
starting work, I will assume you have already installed Ansible on your local
machine and have a password less entry to the remote server using SSH keys.
Well then on
with work:
# cd /home
# mkdir wordpress-full-install
&& cd wordpress-full-install
# touch site.yml
# touch hosts
# mkdir roles && cd roles
Staying in
the same folder initialize four different roles using Ansible galaxy:
# ansible-galaxy init common
# ansible-galaxy init php
# ansible-galaxy init mysql
# ansible-galaxy init wordpress
# nano ~/wordpress-full-install/hosts (This will tell Ansible on which
server to deploy the role)
[wpserver]
wp.remoteserver.com
# nano ~/wordpress-full-install/site.yml (This is where we make our roles are
executed sequentially when the role is executed)
- hosts: wpserver
roles:
- common
- php
- mysql
- wordpress
# nano roles/common/tasks/main.yml ( First we set out a complete lamp
stack )
---
- name: Update apt cache
apt: update_cache=yes
cache_valid_time=3600
sudo: yes
- name: Install required software
apt: name={{ item }}
state=present
sudo: yes
with_items:
- apache2
- mysql-server
- php5-mysql
- php5
- libapache2-mod-php5
- php5-mcrypt
- python-mysqldb
# nano roles/php/tasks/main.yml ( Here we install the required php
dependencies )
---
- name: Install php extensions
apt: name={{ item }}
state=present
sudo: yes
with_items:
- php5-gd
- libssh2-php
# nano roles/mysql/defaults/main.yml ( Sets up some default variable for
DB )
---
wp_mysql_db: wordpress
wp_mysql_user: wordpress
wp_mysql_password: mysecretpassword
# nano roles/mysql/tasks/main.yml ( Installs and configures the DB on the
targeted server )
---
- name: Create mysql database
mysql_db: name={{ wp_mysql_db }}
state=present
- name: Create mysql user
mysql_user:
name={{ wp_mysql_user }}
password={{ wp_mysql_password
}}
priv=*.*:ALL
# nano roles/wordpress/tasks/main.yml
( This is where the
entire install takes place for wordpress and different configurations are made
to adjust to lamp stack )
---
- name: Download WordPress
get_url:
url=https://wordpress.org/latest.tar.gz
dest=/tmp/wordpress.tar.gz
validate_certs=no
sudo: yes
- name: Extract WordPress
unarchive:
src=/tmp/wordpress.tar.gz
dest=/var/www/
copy=no
sudo: yes
- name: Update default Apache site
sudo: yes
lineinfile:
dest=/etc/apache2/sites-enabled/000-default.conf
regexp="(.)+DocumentRoot
/var/www/html"
line="DocumentRoot
/var/www/wordpress"
notify:
- restart apache
- name: Copy sample config file
command: mv
/var/www/wordpress/wp-config-sample.php /var/www/wordpress/wp-config.php
creates=/var/www/wordpress/wp-config.php
sudo: yes
- name: Update WordPress config file
lineinfile:
dest=/var/www/wordpress/wp-config.php
regexp="{{ item.regexp
}}"
line="{{ item.line
}}"
with_items:
- {'regexp':
"define\\('DB_NAME', '(.)+'\\);", 'line': "define('DB_NAME',
'{{wp_mysql_db}}');"}
- {'regexp':
"define\\('DB_USER', '(.)+'\\);", 'line': "define('DB_USER',
'{{wp_mysql_user}}');"}
- {'regexp':
"define\\('DB_PASSWORD', '(.)+'\\);", 'line': "define('DB_PASSWORD',
'{{wp_mysql_password}}');"}
sudo: yes
# nano
roles/wordpress/handlers/main.yml ( This is the action listener, when apache configuration is
changed while installing wordpress a service restart is required for apache –
This Handler is just a task but only executes when the previous task has been
executed successfully )
---
- name: restart apache
service: name=apache2
state=restarted
sudo: yes
# cd /home/wordpress-full-install
# ansible-playbook -i hosts site.yml
That’s it!
With just the above command your entire wordpress will be deployed onto the
remote server.
If needed to
be installed on different servers just change the entries in the hosts file. Or
just give the –I path to the specific file which contains the entries to the
different server list.
No comments:
Post a Comment